iTextsharp、PdfPCell.VerticalAlignment 和 PdfPCell.HorizontalAlignment
Posted
技术标签:
【中文标题】iTextsharp、PdfPCell.VerticalAlignment 和 PdfPCell.HorizontalAlignment【英文标题】:iTextsharp, PdfPCell.VerticalAlignment and PdfPCell.HorizontalAlignment 【发布时间】:2011-05-05 00:48:27 【问题描述】:我试图弄清楚如何让我的文本在 PdfPCell 中显示在中间。我尝试了许多不同的选项,例如:
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();
【问题讨论】:
【参考方案1】:我认为您遇到的基本问题是您将文本添加到 iTextSharp Paragraph
对象,然后尝试使用包含它的 PdfPCell
对象设置此文本的对齐方式。我不确定PdfPCell.VerticalAlignment
属性是否仅适用于PdfPCell
的文本,或者在PdfPCell
内对齐Paragraph
对象是否不会对您在测试中看到的任何影响。
您还将myCell.HorizontalAlignment
和myCell.VerticalAlignment
设置为for
循环中的索引值。我认为您的意思是使用 i
的 1 次插入。
无论如何,设置 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();
【讨论】:
谢谢,当我开始使用短语而不是段落时效果更好【参考方案2】:请使用给定的代码,我希望我对那些想要在单元格中打印文本的人最有帮助 protected void Page_Load(object sender, EventArgs e) 可获取();
void gettable()
using (FileStream fs = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Vedic-Chart-Life-Report.pdf"), FileMode.Create, FileAccess.Write, FileShare.Read))
Document doc = new Document(PageSize.LETTER);
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
doc.NewPage();
BaseFont bf = BaseFont.CreateFont("C:/WINDOWS/Fonts/krdv010.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font f = new Font(bf, 16, Font.BOLD);
PdfContentByte cb = writer.DirectContent;
cb.MoveTo(0, doc.PageSize.Height - 20);
cb.LineTo(doc.PageSize.Width, doc.PageSize.Height - 20);
cb.Stroke();
cb.ClosePathStroke();
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell(new Phrase("eaxynks'k foospu", f));
// Give our rows some height so we can see test vertical alignment.
cell.FixedHeight = 15f;
cell.HorizontalAlignment = 1;
cell.VerticalAlignment = Element.ALIGN_TOP;
table.AddCell(cell);
doc.Add(table);
//cb.RoundRectangle(10f, 550f, 592f, 200f, 20f);
//cb.Stroke();
//doc.Add(new Phrase("eaxynks'k foospu", f));
doc.Close();
【讨论】:
【参考方案3】:当我添加时,Jay Riggs 解决方案也开始适用于垂直对齐:
cell.UseAscender = true;
http://www.afterlogic.com/mailbee-net/docs-itextsharp/html/0602b79e-ea9c-0c7d-c4b2-bc4b5f976f15.htm
【讨论】:
【参考方案4】:我尝试给出这里提到的直接整数和其他解决方案。但没有什么对我有用。这在组合方法中对我有用。
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
【讨论】:
以上是关于iTextsharp、PdfPCell.VerticalAlignment 和 PdfPCell.HorizontalAlignment的主要内容,如果未能解决你的问题,请参考以下文章