iTextSharp将PDFP表与文档边距内的段落对齐

时间:2016-12-11 11:55:05

标签: c# pdf alignment itext

我正在生成这样的PDF文档:

  string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            Document doc = new Document(iTextSharp.text.PageSize.A4,50,50,120,40);


            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path+"\\Invoices\\Test.pdf", FileMode.Create));
            PdfPTable table = new PdfPTable(2);
            table.WidthPercentage = 100;
            table.DefaultCell.BorderWidth = 0;
            table.DefaultCell.HasBorder(iTextSharp.text.Rectangle.NO_BORDER);
            table.AddCell(new PdfPCell() { HorizontalAlignment = PdfPCell.ALIGN_LEFT, Phrase = new Phrase("Text to the left"), BorderColor = BaseColor.WHITE, BorderWidth = 0 });
            table.AddCell(new PdfPCell() { HorizontalAlignment = PdfPCell.ALIGN_RIGHT, Phrase = new Phrase("Text to the right"), BorderColor = BaseColor.WHITE, BorderWidth = 0 });

            //   table.AddCell(new PdfPCell("Text to the left", PdfPCell.ALIGN_LEFT));
            //  table.AddCell(new PdfPCell("Text to the left", PdfPCell.ALIGN_RIGHT));
            document.add(table);

            doc.Open();
            Paragraph p = new Paragraph("This is a first text above \n \n", FontFactory.GetFont("Arial", 8.0f, BaseColor.BLACK));
            doc.Add(p);
            doc.Add(table);
            doc.Close();

生成如下文档:

enter image description here

你们是否注意到paragrap和桌子内的文字没有完全对齐左边?

如果有或没有将第一个文本添加到表格中,我怎么能解决这个问题(这两个选项都适合我)

有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

将单元格填充设置为零以删除左侧的小空格,如:

new PdfPCell { PaddingLeft = 0 }

顺便说一句,你可以删除这个BorderColor和BorderWidth和DefaultCell的东西。只需设置:

Border = Rectangle.NO_BORDER

在单元格属性上删除边框。