与iTextSharp的一大块右对齐一部分 [英] Right Align part of a Chunk with iTextSharp

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

问题描述

I'm新iTextSharp的和我正尝试创建一个PDF文件。
只是一个简单的例子。如果我做这样的事情:

I´m new to iTextSharp and I´m trying to create a PDF. Just a simple example. If I do something like this:

Paragraph p = new Paragraph();

p.Add(new Chunk("789456|Test", f5));
newDocument.Add(p);
p.Add(new Chunk("456|Test", f5));
newDocument.Add(p);
p.Add(new Chunk("12345|Test", f5));
newDocument.Add(p);



我得到这样的结果:

I get this result:

789456|Test
456|Test
12345|Test

我能做些什么,以右对齐在我的块中的文本的一部分。像这样的:

What can I do to right align part of the text in my chunk. Like this:

789456|Test
   456|Test
 12345|Test

先谢谢了。

推荐答案

请看看下面的例子:第4章。他们引进了 PdfPTable 的概念。而不是创建这样的对象的789456 |测试,然后执行不可能有单独的部分这些■对齐正确的内容,你会发现它更容易创建一个简单的 PdfPTable 与2列,加入789456 |测试无边界单元格的内容。所有其他的解决方法将不可避免地导致代码是更复杂且容易出错的

Please take a look at the following examples: chapter 4. They introduce the concept of a PdfPTable. Instead of creating Chunk objects like this "789456|Test", and then do the impossible to have the separate parts of the content of these Chunks align correctly, you'll discover that it's much easier to create a simple PdfPTable with 2 columns, adding "789456|" and "Test" as content of borderless cells. All other workarounds will inevitably lead to code that is more complex and error-prone.

卡尔安德森提供的答案是复杂得多;通过马尼什·夏尔马提供的答案是错的。虽然我不知道C#中,我试图写一个例子(根据我将如何做到这一点在Java中):

The answer provided by Karl Anderson is much more complex; the answer provided by Manish Sharma is wrong. Although I don't know C#, I've tried to write an example (based on how I would achieve this in Java):

PdfPTable table = new PdfPTable(2);
table.DefaultCell.Border = PdfPCell.NO_BORDER;
table.DefaultCell.VerticalAlignment = Element.ALIGN_RIGHT;
table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
table.addCell(new Phrase("789456|", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.addCell(new Phrase("Test", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
table.addCell(new Phrase("456|", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.addCell(new Phrase("Test", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
table.addCell(new Phrase("12345|", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.addCell(new Phrase("Test", f5));
doc.Add(table);

请注意,一个表的默认宽度为80%的可用宽度(之间的水平空间边距)并且该表是中心默认对齐。您可能需要更改使用的 widthPercentage宽度百分比 和的 的Horizo​​ntalAlignment

Note that the default width of a table is 80% of the available width (the horizontal space between the margins) and that the table is center aligned by default. You may want to change these defaults using WidthPercentage and HorizontalAlignment

这篇关于与iTextSharp的一大块右对齐一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆