C# PdfPCell中的右对齐文本

C# PdfPCell中的右对齐文本,c#,itextsharp,C#,Itextsharp,我有一个生成PDF发票的C#应用程序。此发票中有一个项目和价格表。这是使用PdfPTable和PdfPCells生成的 我希望能够右对齐价格列,但我似乎不能-文本在单元格中总是左对齐 以下是我创建表的代码: PdfPTable table = new PdfPTable(2); table.TotalWidth = invoice.PageSize.Width; float[] widths = { invoice.PageSize.Width - 70f, 70f }; table.SetWi

我有一个生成PDF发票的C#应用程序。此发票中有一个项目和价格表。这是使用
PdfPTable
PdfPCell
s生成的

我希望能够右对齐价格列,但我似乎不能-文本在单元格中总是左对齐

以下是我创建表的代码:

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的原始开发人员,您遇到的问题在我的

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;
}
您正在混合文本模式复合模式

文本模式下,您使用
短语
作为构造函数的参数创建
PdfPCell
,并在单元格级别定义对齐方式。但是,您正在复合模式下工作。只要使用
addElement()
方法,就会触发此模式。在复合模式下,忽略在单元级别定义的对齐(这解释了您的问题)。相反,使用单独图元的对齐方式

如何解决你的问题

文本模式下工作,以不同的方式将
短语
添加到单元格中。 或者在复合模式下工作并使用定义对齐方式的
段落

private PdfPCell getParagraphWithRightAlignCell(Paragraph paragraph) {

 PdfPCell cell = new PdfPCell(paragraph);
 cell.setBorderColor( BaseColor.WHITE);
 cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
 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
pass
paragraph


谢谢

我遇到了同样的问题,并且没有为
短语
对象找到可行的解决方案。我建议你最好使用
段落
而不是
短语
,并设置段落本身的对齐方式。是的,这是一种可能的解决方案。请参阅我的答案以获得更详细的解释。谢谢!通过对段落和单元格的正确组合,我成功地实现了这一目标。这几个月来一直让我发疯!“以不同的方式?”这是否意味着我可以在单元格中执行
.AddCell(新段落(“txt”){Alignment=Element.ALIGN\u RIGHT})
和右对齐?