Create and design PDF using iTextSharp or similar

tl; dr: Basically, I'm just wondering what is the best / easiest way to create a PDF document? Is it remotely legal to actually create an entire PDF document with iTextSharp with code (i.e. Do not upload external files)? I want the end result to look like a web page with various colors, borders, images and everything else. Or do you need to rely on other documents such as .doc, .html files to achieve good design?

I initially thought that I would use HTML markup to create a PDF file, however, seeing how poor the support is when it comes to styling / CSS, I rethink the whole situation.

It made me wonder why even use HTML markup or a .doc (x) file to create a PDF design, when I can just do it directly in PDF, without relying on various files that do not serve the real purpose.

I started to study this manual (this is a few parts) http://www.c-sharpcorner.com/uploadfile/f2e803/basic-pdf-creation-using-itextsharp-part-i/

Part of the manual:

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

And I got basic information about creating PDF files and developed them a bit.

But is it possible to generate and design large PDF documents in this way and are there more correct manuals or similar with all the different commands for generating texts, images, borders and everything, since I have no real idea about creating PDF with code.

+4
source share
2 answers

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

Option 1: you create your layout using high-level iText objects. There are many applications that use PdfPTable to create complex reports. For example: temporary tables for a German railway company are created from scratch through a code; The invoices for the Belgian company Telco are created in this way ... The advantage of this approach is that you can fine-tune the layout. The downside is that you need to change the source code as soon as you want to change the layout.

Option 2: you create your layout by creating an AcroForm template. Each field in this template has a name and is visualized in the exact position (determined by its coordinates) on certain pages. The code to fill out this form consists of several lines. Whenever you need to change the layout, you change the AcroForm template. You do not need to change your code. The downside is that AcroForms are very static. Compare this to a paper form: you cannot insert a string into a paper form.

Option 3: you create your data in XHTML format and styles in CSS. The Belgian printing company responsible for creating invoices for its customers, streams data into very simple HTML files containing sequences of tables that never span more than a few pages. These files are then transferred to the iText XML working file along with CSS, which is different for each of its clients. The advantage of this approach is that additional programming is not required when connecting a new client. It's just a matter of creating new CSS. The downside is that you are limited to the HTML format. Elementary logic also tells you that you should not expect URL2PDF: have you ever tried to print a website? Well, the poor quality of this print should indicate problems you will encounter when trying to convert HTML to PDF. If you expect them, you can get good results. If you do not ... this is a bad master who blames his tools ...

Option 4: Define your template using the XML Forms architecture. Such templates are usually created using Adobe LiveCycle Designer. XSD is submitted to LC Designer, and the result is a blank form in which the PDF format acts as a container for the XML stream. You can then use iText to insert your own XML data that corresponds to the XSD in the PDF, and you can use the XFA Worker to smooth this shape. An XFA employee is only available as a closed source product (divers should set limits because they rarely do).

Option 5:. XML Worker is used to convert XHTML + CSS and XFA to PDF (regular PDF, PDF / A, PDF / UA). You can use the generic XML Worker engine to support your own XML format. An advantage would be a very powerful engine that you can customize to meet your specific needs. The disadvantage is that this is due to serious upfront investments in development.

Option 6: use a third-party tool to determine the template and a third-party server that uses iText under the hood to create PDF files 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 an iText client, and we know that they use iText correctly, while we do not have this guarantee from other suppliers.

+5
source

I am sure you can achieve your goal with iText. But imho, you should first research things like a report generator:

  • Sql Server Reporting Services or
  • Crystal Report or
  • ...

Invest at the same time, you will have several formats: HTML, DOCX, PDF, XLSX ...

Of course, the relevance of the answer may vary depending on the nature of the document to be created: when it comes to 1000-page documents, a report generator is not always the best way.

0
source

Source: https://habr.com/ru/post/980299/


All Articles