C# PdfPCell 中的右对齐文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13607970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:10:43  来源:igfitidea点击:

Right aligning text in PdfPCell

c#itextsharp

提问by colincameron

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 PdfPTableand PdfPCells.

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

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.

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

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

Can anyone help?

任何人都可以帮忙吗?

采纳答案by Bruno Lowagie

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

我是 iText 的原始开发人员,您遇到的问题在我的书中有解释。

You're mixing text modeand composite mode.

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

In text mode, you create the PdfPCellwith a Phraseas 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.

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

How to solve your problem?

如何解决您的问题?

Either work in text modeby adding your Phraseto the cell in a different way. Or work in composite modeand use a Paragraphfor which you define the alignment.

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

The advantage of composite modeover text modeis 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 modeis speed: it takes less processing time to deal with the content of a cell.

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

回答by aledford

Perhaps its because you are mixing the different ways to add the cells? Have you tried explicitly creating a cell object, massaging it however you want then adding it for every cell?

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

Another thing you could try is setting the vertical alignment as well as the horizontal.

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

回答by aledford

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

回答by B. Clay Shannon

Here is my derivation of user2660112's answer - one method to return a cell for insertion into a bordered and background-colored table, and a similar, but borderless/colorless variety:

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

These can then be called like so:

然后可以像这样调用它们:

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

回答by Carlos Ferreira

cell.HorizontalAlignment = Element.ALIGN_RIGHT;