[英]Finding the starting x coordinate of text in a right-aligned PdfPCell
[英]Right aligning text in PdfPCell
我有一個生成 PDF 發票的 C# 應用程序。 在這張發票中是一個項目和價格表。 這是使用PdfPTable
和PdfPCell
生成的。
我希望能夠右對齊價格列,但我似乎無法做到 - 文本總是在單元格中左對齊。
這是我創建表的代碼:
PdfPTable table = new PdfPTable(2);
table.TotalWidth = invoice.PageSize.Width;
float[] widths = { invoice.PageSize.Width - 70f, 70f };
table.SetWidths(widths);
table.AddCell(new Phrase("Item Name", tableHeadFont));
table.AddCell(new Phrase("Price", tableHeadFont));
SqlCommand cmdItems = new SqlCommand("SELECT...", con);
using (SqlDataReader rdrItems = cmdItems.ExecuteReader())
{
while (rdrItems.Read())
{
table.AddCell(new Phrase(rdrItems["itemName"].ToString(), tableFont));
double price = Convert.ToDouble(rdrItems["price"]);
PdfPCell pcell = new PdfPCell();
pcell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
pcell.AddElement(new Phrase(price.ToString("0.00"), tableFont));
table.AddCell(pcell);
}
}
任何人都可以幫忙嗎?
我是 iText 的原始開發人員,您遇到的問題在我的書中有解釋。
您正在混合文本模式和復合模式。
在文本模式下,您使用Phrase
作為構造函數的參數創建PdfPCell
,並在單元格級別定義對齊方式。 但是,您在復合模式下工作。 只要您使用addElement()
方法,就會觸發此模式。 在復合模式下,單元格級別定義的對齊將被忽略(這解釋了您的問題)。 相反,使用單獨元素的對齊方式。
如何解決您的問題?
通過以不同的方式將Phrase
添加到單元格中,可以在文本模式下工作。 或者在復合模式下工作並使用您定義對齊的Paragraph
。
復合模式相對於文本模式的優勢在於,同一單元格中的不同段落可以有不同的對齊方式,而在文本模式下只能有一種對齊方式。 另一個優點是您可以添加的不僅僅是文本:您還可以添加圖像、列表、表格……文本模式的一個優點是速度:處理單元格內容所需的處理時間更少。
private static PdfPCell PhraseCell(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.BorderColor = BaseColor.WHITE;
// cell.VerticalAlignment = PdfCell.ALIGN_TOP;
//cell.VerticalAlignment = align;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
return cell;
}
這是我對 user2660112 答案的推導 - 一種返回單元格以插入帶邊框和背景色表格的方法,以及類似但無邊框/無色的變體:
private static PdfPCell GetCellForBorderedTable(Phrase phrase, int align, BaseColor color)
{
PdfPCell cell = new PdfPCell(phrase);
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
cell.BackgroundColor = color;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
return cell;
}
private static PdfPCell GetCellForBorderlessTable(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
cell.BorderWidth = PdfPCell.NO_BORDER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
return cell;
}
然后可以像這樣調用它們:
Font timesRoman9Font = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 9, BaseColor.BLACK);
Font timesRoman9BoldFont = FontFactory.GetFont(FontFactory.TIMES_BOLD, 9, BaseColor.BLACK);
Phrase phrasesec1Heading = new Phrase("Duckbills Unlimited", timesRoman9BoldFont);
PdfPCell cellSec1Heading = GetCellForBorderedTable(phrasesec1Heading, Element.ALIGN_LEFT, BaseColor.YELLOW);
tblHeadings.AddCell(cellSec1Heading);
Phrase phrasePoisonToe = new Phrase("Poison Toe Toxicity Level (Metric Richter Scale, adjusted for follicle hue)", timesRoman9Font);
PdfPCell cellPoisonToe = GetCellForBorderlessTable(phrasePoisonToe, Element.ALIGN_LEFT);
tblFirstRow.AddCell(cellPoisonToe);
我最終在這里搜索了 PdfPCell 中的 java 右對齊文本。 因此,如果您使用的是 Java,請不要冒犯,請使用給定的代碼片段來實現正確對齊。
private PdfPCell getParagraphWithRightAlignCell(Paragraph paragraph) {
PdfPCell cell = new PdfPCell(paragraph);
cell.setBorderColor( BaseColor.WHITE);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
return cell;
}
在getParagraphWithRightAlignCell
傳遞paragraph
謝謝
也許是因為您正在混合添加單元格的不同方法? 您是否嘗試過明確創建一個單元格對象,根據需要對其進行按摩,然后為每個單元格添加它?
您可以嘗試的另一件事是設置垂直對齊和水平對齊。
cell.HorizontalAlignment = Element.ALIGN_RIGHT;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.