我試圖弄清楚如何讓我的文本在一個PdfPCell裏面顯示在中間。我嘗試了很多不同的選項,例如:iTextsharp,PdfPCell.VerticalAlignment和PdfPCell.HorizontalAlignment
myCell.VerticalAlignment = Element.ALIGN_MIDDLE; myCell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; myCell.VerticalAlignment = Rectangle.ALIGN_MIDDLE;
這些都不適用於我。 VerticalAlignment需要一個int,所以我試圖做一個循環,來看看我是否能找到合適的數量,但一切都只是被甩在底部對齊..
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();
謝謝,它的工作更好,當我開始使用短語,而不是段落 – Jimmy 2010-11-05 10:25:24