![](/img/trans.png)
[英]HorizontalAlignment on image not working in PdfPCell in itextsharp
[英]iTextsharp, PdfPCell.VerticalAlignment and PdfPCell.HorizontalAlignment
我試圖弄清楚如何讓我的文本在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();
我認為您遇到的基本問題是您正在向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();
}
}
當我添加時,Jay Riggs解決方案也開始用於垂直對齊:
cell.UseAscender = true;
http://www.afterlogic.com/mailbee-net/docs-itextsharp/html/0602b79e-ea9c-0c7d-c4b2-bc4b5f976f15.htm
我試着給出直接整數和這里提到的其他解決方案。 但沒有什么對我有用。 這種組合在Composition方法中對我有用。
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
請使用給定的代碼,我希望我對那些想在中間和頂部對齊的單元格中打印文本的人最有幫助的保護void Page_Load(object sender,EventArgs e){gettable(); }
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.