【问题标题】:With iTextSharp, how to rightalign a container of text (but keeping the individual lines leftaligned)使用 iTextSharp,如何右对齐文本容器(但保持各行左对齐)
【发布时间】:2012-01-12 13:31:36
【问题描述】:

我事先不知道文本块中文本的宽度,我希望文本块像这样向右对齐,同时仍然让各行左对齐:

                                Mr. Petersen      |
                                Elmstreet 9       |
                                888 Fantastic City|

(| 表示文档的右边缘)

应该很简单,但我想不通。

我尝试将所有文​​本放在一个段落中并设置段落.Alignment = Element.ALIGN_RIGHT,但这会使各个行右对齐。

我尝试将段落放在表格内的单元格中,并右对齐单元格,但单元格只占用表格的整个宽度。

如果我可以创建一个只占用所需宽度的容器,我可以简单地右对齐这个容器,所以也许这真的是我的问题。

【问题讨论】:

  • 根据您的标题,我实际上并不了解您要做什么。你不想正常的段落对齐(左,右,对齐)?您是否要对齐多个段落的边缘?

标签: c# .net itext


【解决方案1】:

只需设置Paragraph.Align 属性:

using (Document document = new Document()) {
  PdfWriter.GetInstance(
    document, STREAM
  );
  document.Open();
  for (int i = 1; i < 11; ++i) {
    Paragraph p = new Paragraph(string.Format(
      "Paragraph {0}", i
    ));
    p.Alignment = Element.ALIGN_RIGHT;
    document.Add(p);
  } 
}

它甚至可以处理这样的长字符串:

string longString = @"
iText ® is a library that allows you to create and manipulate PDF documents. It enables developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation.
";
Paragraph pLong = new Paragraph(longString);
pLong.Alignment = Element.ALIGN_RIGHT;
document.Add(pLong);

编辑: 看了你画的“图”之后……

它与标题不匹配。 唯一您可以像图片一样对齐单个 Paragraph 对象的方式是,如果“段落”超过 Document 对象的“内容”框(因为缺少更好的术语)。换句话说,如果文本数量超过放在一行中,您将无法获得这种类型的对齐方式。

话虽如此,如果您想要这种类型的对齐方式,您需要:

  1. 从您打算使用的字符串集合中计算最宽的值。
  2. 使用该值为Paragraphs 设置一个通用的左缩进值。

类似这样的:

using (Document document = new Document()) {
  PdfWriter.GetInstance(
    document, STREAM
  );
  document.Open();
  List<Chunk> chunks = new List<Chunk>();
  float widest = 0f;
  for (int i = 1; i < 5; ++i) {
    Chunk c = new Chunk(string.Format(
      "Paragraph {0}", Math.Pow(i, 24)
    ));
    float w = c.GetWidthPoint();
    if (w > widest) widest = w;
    chunks.Add(c);
  } 
  float indentation = document.PageSize.Width
    - document.RightMargin
    - document.LeftMargin
    - widest
  ;
  foreach (Chunk c in chunks) {
    Paragraph p = new Paragraph(c);
    p.IndentationLeft = indentation;
    document.Add(p);
  }
}

更新 2

阅读您更新的问题后,这里有另一个选项可让您将文本添加到“容器”的左侧:

string textBlock = @"
Mr. Petersen
Elmstreet 9
888 Fantastic City  
".Trim();
// get the longest line to calcuate the container width  
var widest = textBlock.Split(
    new string[] {Environment.NewLine}
    , StringSplitOptions.None
  )
  .Aggregate(
    "", (x, y) => x.Length > y.Length ? x : y
  )
;
// throw-away Chunk; used to set the width of the PdfPCell containing
// the aligned text block
float w = new Chunk(widest).GetWidthPoint();
PdfPTable t = new PdfPTable(2);
float pageWidth = document.PageSize.Width 
  - document.LeftMargin 
  - document.RightMargin
;
t.SetTotalWidth(new float[]{ pageWidth - w, w });
t.LockedWidth = true;  
t.DefaultCell.Padding = 0;
// you can add text in the left PdfPCell if needed
t.AddCell("");
t.AddCell(textBlock);
document.Add(t);

【讨论】:

  • 感谢您的回答。我不敢相信它必须这么难。无论如何,您测量宽度然后缩进的建议对我有用。
  • 很遗憾,由于事先不知道文本块的宽度,所以必须计算出最长的行。您可以硬编码一个宽度并将文本块放在PdfPTable 的第二列中,但是您可能会冒 [1] 右侧空间过多或 [2] 空间太小和一行溢出 @ 987654332@ 宽度。在阅读了您编辑的问题后,我用另一个更符合创建“容器”的选项更新了我的答案。更新后的代码的一个区别是您可以在文本块的左侧添加文本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-13
  • 2012-06-09
  • 1970-01-01
相关资源
最近更新 更多