在PdfPCell右对齐文本

在PdfPCell右对齐文本

本文介绍了在PdfPCell右对齐文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成的PDF发票C#应用程序。在这种发票项目和价格表。这是通过使用产生了 PdfPTable PdfPCell 秒。

I have a C# application that generates a PDF invoice. In this invoice is a table of items and prices. This is generated using a PdfPTable and PdfPCells.

我希望能够以右对齐的价格列,但我不能似乎能 - 文本永远是左对齐在细胞

I want to be able to right-align the price column but I cannot seem to be able to - the text always comes out left-aligned in the cell.

下面是我的code用于创建表:

Here is my code for creating the table:

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

I'm the original developer of iText, and the problem you're experiencing is explained in my book.

您是混合的文本模式复合模式

文本模式,创建 PdfPCell 短语作为参数构造的,并且可以定义在细胞水平的取向。但是,你在复合模式工作。这种模式只要你使用的addElement()方法所触发。在复合模式后,在细胞级别上定义的定位被忽略(这可以解释你的问题)。相反,单独的元件的取向被使用。

In text mode, you create the PdfPCell with a Phrase as the parameter of the constructor, and you define the alignment at the level of the cell. However, you're working in composite mode. This mode is triggered as soon as you use the addElement() method. In composite mode, the alignment defined at the level of the cell is ignored (which explains your problem). Instead, the alignment of the separate elements is used.

如何解决问题了吗?

无论是通过添加短语来的单元以不同的方式在文本模式的工作。
或者在工作的混合模式,并使用段落为您定义的对齐方式。

Either work in text mode by adding your Phrase to the cell in a different way.Or work in composite mode and use a Paragraph for which you define the alignment.

的优点复合模式文本模式是在同一小区不同的段落可以有不同的路线,而你只能在文本一次调整模式即可。另一个好处是,你可以添加的不仅仅是文本:您还可以添加图片,列表,表格,...的文本模式的优点是速度:它需要较少的处理时间,处理内容的细胞。

The advantage of composite mode over text mode is that different paragraphs in the same cell can have different alignments, whereas you can only have one alignment in text mode. Another advantage is that you can add more than just text: you can also add images, lists, tables,... An advantage of text mode is speed: it takes less processing time to deal with the content of a cell.

这篇关于在PdfPCell右对齐文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:25