简体   繁体   English

使用iTextSharp C#创建列(如Word)并对齐pdf文档中的文本

[英]Creating columns (like Word) and justify the text in pdf document using iTextSharp c#

I'm working in a "study" project which perform the function of cash register. 我正在一个“研究”项目中,该项目执行收银机的功能。 It have the basics, add products with name and price, can bill to customers. 它具有基本知识,添加名称和价格的产品,可以向客户收费。 I created a method that takes all created products (loaded in a collection) and creates a price list in a PDF document. 我创建了一种方法,该方法可以接收所有创建的产品(加载到集合中)并在PDF文档中创建价格表。 The final result is something like this: 最终结果是这样的: price_list.pdf

A bit ugly. 有点丑。 Now, how can I create three columns that limit the characters in each one? 现在,如何创建三列来限制每列中的字符? Like Office Word: 像Office Word: Office Word中的价目表 As you can see, in Word we can create columns (in my case three, with the lines visible). 如您所见,在Word中,我们可以创建列(在本例中为三行,可见的行)。 It is possible using iTextSharp? 可以使用iTextSharp吗? I searched but I couldn't find anything. 我搜索了但找不到任何东西。 And how to align the name on the left and the price on the right regardless of the length of the name, again as Word? 又如何将名称左对齐,价格右对齐,而不管名称的长度如何,还是Word?

If this is a school project (I'm not sure what you meant when you wrote "I'm working in a "study" project" ), you can show off to your teacher by using iText 7 instead of the outdated iTextSharp. 如果这是一个学校项目(我不确定您写“我正在从事“研究”项目”时的意思 ),则可以使用iText 7代替过时的iTextSharp向老师炫耀。

By the way: we don't call it iTextSharp anymore: it's now called iText for .NET, which explains why there is no iTextSharp tag on Stack Overflow anymore. 顺便说一句:我们不再称它为iTextSharp:现在称为.NET的iText,这解释了为什么Stack Overflow上不再有iTextSharp标签。

iText 7 comes with a ColumnDocumentRenderer that allows you to define nice columns (see chapter 2 of the tutorial). iText 7带有ColumnDocumentRenderer ,可用于定义漂亮的列(请参阅本教程的第2章 )。 Most of the tutorials are in Java, because a good C# developer is supposed to know how to read Java, but there's also a .NET tutorial with a columns example. 大多数教程都是用Java编写的,因为一个好的C#开发人员应该知道如何阅读Java,但是还有一个带有列示例的.NET教程。 See chapter 3 of the .NET version of the iText 7 jump-start tutorial: 请参阅iText 7 .NET版快速入门教程的第3章

Document document = new Document(pdf, ps);
//Set column parameters
float offSet = 36;
float columnWidth = (ps.GetWidth() - offSet * 2 + 10) / 3;
float columnHeight = ps.GetHeight() - offSet * 2;
//Define column areas
Rectangle[] columns = new Rectangle[] {
    new Rectangle(offSet - 5, offSet, columnWidth, columnHeight),
    new Rectangle(offSet + columnWidth, offSet, columnWidth, columnHeight),
    new Rectangle(offSet + columnWidth * 2 + 5, offSet, columnWidth, columnHeight)
};
document.SetRenderer(new ColumnDocumentRenderer(document, columns));

If you want the nice tabbing, you can use the TabStop class as explained in chapter 3 of the building blocks tutorial. 如果您希望使用漂亮的制表符,则可以按照构建模块教程第3章中的说明使用TabStop类。

However, the iText 5 code is much less intuitive than the iText 7 code. 但是,iText 5代码不如iText 7代码直观。 I can expand my answer if you need an iText 5 solution, but be aware that if I were your teacher, I would fail you for using iText 5. So before I give you an iText 5 answer, I'd need some written proof that your teacher insists on an iText 5 solution. 如果您需要 iText 5解决方案,我可以扩大答案,但是请注意,如果我是您的老师,那么使用iText 5会让您失败。因此,在给我iText 5答案之前,我需要一些书面证明,以证明您的老师坚持使用iText 5解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM