我试图弄清楚如何将我的文本放入PdfPCell中显示在中间。我尝试了许多不同的选择,例如:

myCell.VerticalAlignment = Element.ALIGN_MIDDLE;
myCell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
myCell.VerticalAlignment = Rectangle.ALIGN_MIDDLE;

None of that works for me. VerticalAlignment takes an int, so I tried to make a loop, to see if i could find the right number, but everything just get left bottom align..

   Document myDocument = new Document(PageSize.A4);

   PdfWriter myPdfWriter = PdfWriter.GetInstance(myDocument, new FileStream(strPDFFilePath, FileMode.Create));
   myDocument.Open();

   myDocument.NewPage();

   PdfContentByte myPdfContentByte = myPdfWriter.DirectContent;

   PdfPCell myCell;
   Paragraph myParagraph;

   PdfPTable myTable = new PdfPTable(4);
   myTable.WidthPercentage = 100;
   myTable.SetWidths(new int[4] { 25, 25, 25, 25 });

   myTable.DefaultCell.BorderWidth = 1;
   myTable.DefaultCell.BorderColor = BaseColor.RED;

   for (int i = -100; i < 100; i++)
   {
       myParagraph = new Paragraph(String.Format("Alignment: {0}", i));
       myParagraph.Font.SetFamily("Verdana");
       myParagraph.Font.SetColor(72, 72, 72);
       myParagraph.Font.Size = 11;

       myCell = new PdfPCell();
       myCell.AddElement(myParagraph);
       myCell.HorizontalAlignment = i;
       myCell.VerticalAlignment = i;
       myTable.AddCell(myCell);
   }

   myDocument.Add(myTable);
   myDocument.Add(new Chunk(String.Empty));
   myDocument.Close();

最佳答案

我认为您遇到的基本问题是要向iTextSharp Paragraph对象添加文本,然后尝试使用包含文本的PdfPCell对象设置文本的对齐方式。我不确定PdfPCell.VerticalAlignment属性是否仅适用于PdfPCell的文本,或者在Paragraph中对齐PdfPCell对象是否对您没有任何影响,您可以在测试中看到。

您还需要在myCell.HorizontalAlignment循环中将myCell.VerticalAlignmentfor设置为索引值。我认为您打算使用1 instread的i

无论如何,尽管可以设置PdfPCell的HorizontalAlignmentVerticalAlignment属性。下面是一个演示此方法的小方法。我根据您要尝试做的事情写得很松散。如果它与您要执行的操作足够接近,则可以将其用作项目的起点。

private void TestTableCreation() {
    using (FileStream fs = new FileStream("TableTest.pdf", FileMode.Create)) {
        Document doc = new Document(PageSize.A4);
        PdfWriter.GetInstance(doc, fs);
        doc.Open();

        PdfPTable table = new PdfPTable(4);

        for (int i = -100; i < 100; i++) {
            PdfPCell cell = new PdfPCell(new Phrase(String.Format("Alignment: {0}", i)));
            // Give our rows some height so we can see test vertical alignment.
            cell.FixedHeight = 30.0f;

            // ** Try it **
            //cell.HorizontalAlignment = Element.ALIGN_LEFT;
            //cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;

            cell.VerticalAlignment = Element.ALIGN_TOP;
            //cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            //cell.VerticalAlignment = Element.ALIGN_BOTTOM;

            table.AddCell(cell);
        }

        doc.Add(table);
        doc.Close();
    }
}

关于c# - iTextsharp,PdfPCell.VerticalAlignment和PdfPCell.Horizo​​ntalAlignment,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4096490/

10-09 06:05