- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我试图弄清楚如何让 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.VerticalAlignment
设置为 for
循环中的索引值。我认为您打算使用 1 个 i
。
无论如何,设置 PdfPCell 的 HorizontalAlignment
和 VerticalAlignment
属性确实有效。下面是一个演示这一点的小方法。我根据你想做的事情写得非常松散;如果它与您尝试做的事情足够接近,也许您可以将其用作项目的起点。
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.HorizontalAlignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4096490/
我是一名优秀的程序员,十分优秀!