Right Align part of a Chunk with iTextSharp

有些话、适合烂在心里 提交于 2019-12-11 01:27:12

问题


I´m new to iTextSharp and I´m trying to create a PDF. Just a simple example. If I do something like this:

Paragraph p = new Paragraph();

p.Add(new Chunk("789456|Test", f5));
newDocument.Add(p);
p.Add(new Chunk("456|Test", f5));
newDocument.Add(p);
p.Add(new Chunk("12345|Test", f5));
newDocument.Add(p);

I get this result:

789456|Test
456|Test
12345|Test

What can I do to right align part of the text in my chunk. Like this:

789456|Test
   456|Test
 12345|Test

Thanks in advance.


回答1:


Please take a look at the following examples: chapter 4. They introduce the concept of a PdfPTable. Instead of creating Chunk objects like this "789456|Test", and then do the impossible to have the separate parts of the content of these Chunks align correctly, you'll discover that it's much easier to create a simple PdfPTable with 2 columns, adding "789456|" and "Test" as content of borderless cells. All other workarounds will inevitably lead to code that is more complex and error-prone.

The answer provided by Karl Anderson is much more complex; the answer provided by Manish Sharma is wrong. Although I don't know C#, I've tried to write an example (based on how I would achieve this in Java):

PdfPTable table = new PdfPTable(2);
table.DefaultCell.Border = PdfPCell.NO_BORDER;
table.DefaultCell.VerticalAlignment = Element.ALIGN_RIGHT;
table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
table.addCell(new Phrase("789456|", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.addCell(new Phrase("Test", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
table.addCell(new Phrase("456|", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.addCell(new Phrase("Test", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
table.addCell(new Phrase("12345|", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.addCell(new Phrase("Test", f5));
doc.Add(table);

Note that the default width of a table is 80% of the available width (the horizontal space between the margins) and that the table is center aligned by default. You may want to change these defaults using WidthPercentage and HorizontalAlignment




回答2:


Unfortunately, you can only apply the alignment to the Paragraph object and not the Chunks, so you will need to use a page layout using columns.

Read iTextSharp - Page Layout with Columns for details on getting layout closer to what you are asking for.




回答3:


try this, this is sample code for you.

private void CreatePdf()
{
        using (FileStream msReport = new FileStream(Server.MapPath("~") + "/App_Data/" + DateTime.Now.Ticks + ".pdf", FileMode.Create))
        {
            Document doc = new Document(PageSize.LETTER, 10, 10, 20, 10);

            PdfWriter pdfWriter = PdfWriter.GetInstance(doc, msReport);
            doc.Open();

            PdfPTable pt = new PdfPTable(1);
            PdfPCell _cell;

            _cell = new PdfPCell(new Phrase("789456|Test"));
            _cell.VerticalAlignment = Element.ALIGN_RIGHT;
            _cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            _cell.Border = 0;
            pt.AddCell(_cell);

            _cell = new PdfPCell(new Phrase("456|Test"));
            _cell.VerticalAlignment = Element.ALIGN_RIGHT;
            _cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            _cell.Border = 0;
            pt.AddCell(_cell);
            _cell = new PdfPCell(new Phrase("12345|Test"));
            _cell.VerticalAlignment = Element.ALIGN_RIGHT;
            _cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            _cell.Border = 0;
            pt.AddCell(_cell);

            doc.Open();
            doc.Add(pt);
            doc.Close();
      }
}


来源:https://stackoverflow.com/questions/18088097/right-align-part-of-a-chunk-with-itextsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!