【问题标题】:Right aligning text in PdfPCell右对齐 PdfPCell 中的文本
【发布时间】:2012-11-16 11:24:15
【问题描述】:

我有一个生成 PDF 发票的 C# 应用程序。在这张发票中是一个项目和价格表。这是使用PdfPTablePdfPCells 生成的。

我希望能够右对齐价格列,但我似乎无法做到 - 单元格中的文本总是左对齐。

这是我创建表格的代码:

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);
    }
}

谁能帮忙?

【问题讨论】:

  • 我遇到了同样的问题,但没有找到Phrase 对象的可行解决方案。我可以建议的最好的方法是您使用 Paragraph 而不是 Phrase 并设置段落本身的对齐方式。

标签: c# itextsharp


【解决方案1】:

我是 iText 的原始开发者,您遇到的问题在我的book 中有说明。

您正在混合使用文本模式复合模式

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

如何解决您的问题?

通过以不同的方式将Phrase 添加到单元格,可以在文本模式下工作。 或者在复合模式下工作并使用您定义对齐方式的Paragraph

复合模式相对于文本模式的优势在于同一单元格中的不同段落可以有不同的对齐方式,而文本中只能有一种对齐方式模式。另一个优点是您可以添加的不仅仅是文本:您还可以添加图像、列表、表格……文本模式的一个优点是速度:处理内容所需的处理时间更少一个细胞。

【讨论】:

  • 谢谢!通过段落和单元格的正确组合,我已经成功地完成了这项工作。几个月来一直让我发疯!
  • “以不同的方式?”
  • 这是否意味着我可以.AddCell(new Paragraph("txt"){ Alignment = Element.ALIGN_RIGHT}) 并在单元格中右对齐?
【解决方案2】:
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;
}

【讨论】:

  • 是的,这是一种可能的解决方案。请参阅我的答案以获得更详细的解释。
【解决方案3】:

这是我对 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);

【讨论】:

    【解决方案4】:

    我最终在 PdfPCell 中搜索 java 右对齐文本。因此,如果您使用的是 java,请使用给定的 sn-p 来实现正确对齐。

    private PdfPCell getParagraphWithRightAlignCell(Paragraph paragraph) {
    
     PdfPCell cell = new PdfPCell(paragraph);
     cell.setBorderColor( BaseColor.WHITE);
     cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
     return cell;
    
    }   
    

    getParagraphWithRightAlignCell 传递paragraph
    谢谢

    【讨论】:

      【解决方案5】:

      也许是因为您混合了不同的方式来添加单元格?您是否尝试过显式创建一个单元格对象,根据需要对其进行按摩,然后为每个单元格添加它?

      您可以尝试的另一件事是设置垂直对齐和水平对齐。

      【讨论】:

        【解决方案6】:
        cell.HorizontalAlignment = Element.ALIGN_RIGHT;
        

        【讨论】:

          猜你喜欢
          • 2013-08-14
          • 2012-04-06
          • 1970-01-01
          • 2011-09-02
          • 2014-07-23
          • 2012-06-09
          相关资源
          最近更新 更多