生成和设计PDF文件使用iTextSharp的或类似的类似、文件、PDF、iTextSharp

2023-09-02 10:33:00 作者:落花无意

TL;博士: 基本上,我只是想知道什么是设计一个PDF文档的最佳/最简单的方法是什么?它是合法的远程实际设计一个整体的PDF文档与iTextSharp的与code(即不加载外部文件)?我想最后的结果类似于用不同的颜色,边框,图像和一切的网页。 还是你必须依靠其他文件,如doc和.html文件,以达到良好的设计?

tl;dr : Basically I'm just wondering what is the best/easiest way to design a PDF document? Is it remotely legit to actually design a whole PDF document with iTextSharp with code(i.e not loading external files)? I want the final result to look similar to a webpage with various colours, borders, images and everything. Or do you have to rely on other documents like .doc, .html files to achieve a good design?

原本我以为我会用HTML标记来生成PDF,但是看到多么糟糕的支持似乎是,当涉及到定型/ CSS所以我会重新考虑整个形势。

Originally I thought that I would use HTML markup to generate a PDF, however seeing how bad the support seems to be when it comes to styling/CSS so am I rethinking the whole situation.

这使我想到,为什么还要使用HTML标记或一个.doc(x)的文件创建时,我可以做到这一点在PDF中正确的PDF设计,而不必依赖于该服务没有真正的各种文件目的。

Which led me to think, why even use a HTML markup or a .doc(x) file to create the PDF design when I could just do it right within the PDF without having to rely on on various files that serves no real purpose.

我开始寻找这个指南上(这是几个部分) 的http://www.c-sharpcorner.com/uploadfile/f2e803/basic-pdf-creation-using-itextsharp-part-i/

I started looking on this guide(it is several parts) http://www.c-sharpcorner.com/uploadfile/f2e803/basic-pdf-creation-using-itextsharp-part-i/

导的一部分:

using (MemoryStream ms = new MemoryStream())
{
   Document document = new Document(PageSize.A4, 25, 25, 30, 30);
   PdfWriter writer = PdfWriter.GetInstance(document, ms);
   document.Open();
   document.Add(new Paragraph("Hello World"));
   document.Close();
   writer.Close();
   Response.ContentType = "pdf/application";
   Response.AddHeader("content-disposition", 
   "attachment;filename=First PDF document.pdf");
   Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
}

,并得到了基本挂有关生成PDF和设计他们一点。

And got a basic hang about generating PDFs and design them a bit.

但是,它可以生成并设计大PDF文档这种方式有什么比较合适的导游或类似的与所有的各种命令生成的文字,图片,边框和一切,因为我有产生PDF文件使用$没有真正的线索C $℃。

But is it possible to generate and design big PDF documents this way and are there any more proper guides or similar with all the various commands to generate texts, images, borders and everything since I have no real clue about generating PDF with code.

推荐答案

现在的问题是太广,所以我只能给你一个非常宽泛的答案。

The question is too broad, so I can only give you a very broad answer.

选项1:您通过利用iText的高级对象布局。有无数的应用程序在那里,使用 PdfPTable 来生成复杂的报表。例如:从头至code创建了德国铁路公司的时间表;发票为比利时电信公司创建这样一来,......这种方法的好处是,你真的可以微调布局。缺点是,你需要,只要你想改变布局改变源$ C ​​$ C。

Option 1: you create your layout by using iText's high-level objects. There are countless applications out there that are using PdfPTable to generate complex reports. For instance: the time tables for a German Railway company are created from scratch through code; the invoices for a Belgian Telco company are created this way,... The advantage of this approach is that you can really fine-tune the layout. The disadvantage is that you need to change source code as soon as you want to change the layout.

选项2:您可以通过创建一个AcroForm模板创建布局。在此模板的每个领域都有一个名称,并在可视化的特定页面上的准确位置(由它的坐标确定)。在$ C $三来一补出这样的形式包括只有少数行的。当你需要改变布局,你改变了AcroForm模板。你不需要改变你的code。缺点是AcroForms是非常静态的。它比作纸张形式:你不能在纸质表格中插入一行或者

Option 2: you create your layout by creating an AcroForm template. Every field in this template has a name and is visualized at exact positions (defined by its coordinates) on specific pages. The code to fill out such a form consists of only a handful of lines. Whenever you need to change the layout, you alter the AcroForm template. You do not need to change your code. The disadvantage is that AcroForms are very static. Compare it to a paper form: you can't insert a row in a paper form either.

选项3:您创建XHTML格式的数据,并在CSS您的样式。比利时的印刷公司负责为客户开具发票的数据流涉及到从未跨越超过少数页表的顺序非常简单的HTML文件。然后这些文件被馈送到的iText的XML工人连同一个CSS即对于每个客户的不同。这种方法的优点是,当一个新的客户加入没有额外的编程是必要的。这是创建一个新的CSS只是一个问题。其缺点是,你的HTML格式的限制。基本逻辑还告诉你,你不应该期待URL2PDF:你曾经试过打印网站?好吧,那打印质量差,应该给你想要的HTML转换为PDF文件时,你会遇到的问题的指示。如果你预计它们,你可以得到很好的效果。如果你不这样做......这是一个可怜的工匠谁指责他的工具...

Option 3: you create your data in XHTML format and your styles in CSS. A Belgian printing company responsible for creating invoices for its customers is streaming data into very simple HTML files involving a sequences of tables that never span more than a handful of pages. These files are then fed to iText's XML worker along with a CSS that is different for each of its customers. The advantage of this approach is that no extra programming is needed when a new customer joins. It's just a matter of creating a new CSS. The disadvantage is that you are limited by the HTML format. Elementary logic also tells you that you shouldn't expect URL2PDF: have you ever tried printing a website? Well, the bad quality of that print should give you an indication of the problems you'll encounter when trying to convert HTML to PDF. If you anticipate them, you can get good results. If you don't... it's a poor craftsman who blames his tools...

方法4:使用XML表单体系结构定义的模板。这样的模板使用Adobe LiveCycle Designer中通常被创建。一个XSD被送入的LC设计,其结果是一个空的形式,其中的PDF格式充当容器为XML流。然后,您可以使用iText的注入您的自定义XML包含的数据与XSD符合到PDF,您可以使用XFA工人扁平化这样的形式。 XFA工人只能作为一个封闭源代码的产品(度外需要设置限制,因为考生很少这样做)。

Option 4: define your template using the XML Forms Architecture. Such templates are usually created using Adobe LiveCycle Designer. An XSD is fed into LC Designer and the result is an empty form where the PDF format acts as a container for an XML stream. You can then use iText to inject your custom XML containing data that conforms with the XSD into the PDF and you can use XFA Worker to flatten such a form. XFA Worker is only available as a closed source product (givers need to set limits because takers rarely do).

方法5:现在XML工人用来XHTML + CSS和XFA转换为PDF(普通PDF,PDF / A,PDF / UA)。您可以使用通用的XML工人引擎来支持自己的XML格式。其优点是一个非常强大的引擎,你可以调整,以满足您的需要。其缺点是,这涉及到一个严肃的前期开发投入。

Option 5: right now XML Worker is used to convert XHTML+CSS and XFA to PDF (ordinary PDF, PDF/A, PDF/UA). You could use the generic XML Worker engine to support your own XML format. The advantage would be a very powerful engine that you can tune to meet your exact needs. The disadvantage is that this involves a serious up-front development investment.

方法6:使用第三方工具来定义模板,使用iText的引擎盖下创建基于模板的PDF第三方服务器。这样的第三方工具的例子圣经开发的创意的设计师。还有其他的工具,但创意的设计师是iText的一个客户,我们知道他们正在使用的iText,而正确的,我们没有这种保证来自其他供应商。

Option 6: use a third party tool to define the template and a third party server that uses iText under the hood to create PDFs based on the template. An example of such a third party tool is Scriptura developed by Inventive Designers. There are other tools, but Inventive Designers is a customer of iText and we know that they are using iText correctly whereas we don't have this guarantee from other vendors.