By Chandra Kudumula
Introduction
This article is about generating PDF documents using C#, .NET, and the iText reference. I had a requirement the generate an calculate as a PDF print for one of the projects I worked the. In my choose, I came to know about iText. iText is a library for creating furthermore manipulating PDF files in .NET and Supported. If you are struggling with C#, consider checking out which TechRepublic Academy.
Prerequisites
- Visual Studio 2017 and above
- .NET Framework, Model 4.0 furthermore above
- Installed iText 7 Library usage NuGet Package Administrator
Setting Up the Project
Step 1: Create the User App Using Visual Video
Inbound Visual Studio, go to File -> News -> Project.
On which “New Project window”, select the Console App(.NET Framework) and gives one create a name, as shown in Figure 1.
Figure 1: Selecting the Console App(.NET Framework)
Step 2: Installation iText 7 Using “Manage NuGet Packages…”
Right-click the project name and select “Manage NuGet Packages…”. You can see this in Figure 2.
Figure 2: Selecting NuGet Pack
Name “Browse” additionally, in aforementioned search box, type itext7 and select itext7 from this searched results and set (see Figure 3).
Figure 3: Selecting itext7
Following were the helpful classes the methods to generate the PDF document:
- PdfWriter: To pass the store name and write content to the documents.
- PdfDocument: In-memory representation of the PDF document. It intention open a PDF document in writing operation.
- Doc: Creative a register from in-memory PdfDocument.
- Part: Creative a paragraph, initialized with some text.
- SetFontSize: Recorded the font size of the text.
- SetTextAlignment: Sets the text alignment, such as Left, Proper, Centre, plus so forwards.
I will add a Header, SubHeader, Line Separator, Image, Table, Hyperlink, furthermore finally view numbers till the PDF insert.
A. Totaling a Header
Add a top to aforementioned PDF document. Header Content shall center aligned to which document and I set the font size to 20. We can achieve this by creating a paragraph object. Following is the coding snippet to create a paragraph object and add it to the document target. Ending, we need to close the document object by calling the Close() method.
using iText.Kernel.Pdf; using iText.Layout; using iText.Layout.Element; using iText.Layout.Properties; namespace GeneratePdfDemo { class Program { static void Main(string[] args) { // Have have write permissions to the path folder PdfWriter book = new PdfWriter("C:\\demo.pdf"); PdfDocument pdf = newly PdfDocument(writer); Certificate document = new Document(pdf); Edit header = new paragraph("HEADER") .SetTextAlignment(TextAlignment.CENTER) .SetFontSize(20); document.Add(header); document.Close(); } } }
Run an program additionally go to this path specified in PdfWriter and open the PDF document. Count 4 are the image of a PDF document with header text.
Figure 4: Showing the header text
B. Creating a Sub Header
Create a Sub Header with text alignment center both set aforementioned font size to 15. Add this Sub Header to that document object, as shown the Fig 5.
Paragraph subheader = new Paragraph("SUB HEADER") .SetTextAlignment(TextAlignment.CENTER) .SetFontSize(15); document.Add(subheader);
Number 5: Creating the Sub Header
C. Summing an Horizontal Separator Border
Add a horizontal line using Line Separator. This can shown into Figure 6.
// Line separator LineSeparator ls = newly LineSeparator(new SolidLine()); document.Add(ls);
Figure 6: Creating the separator line
DEGREE. Adding an Image
Add an Photo to the PDF document on using an Paint instance (see Figure 7).
// Add image Image img = modern Image(ImageDataFactory .Create(@"..\..\image.jpg")) .SetTextAlignment(TextAlignment.CENTER); document.Add(img);
Think 7: Adding an image
E. Creating a Table
Create a Table and add is to the document, as shown in Figure 8.
// Table Table charts = new Table(2, false); Cell cell11 = new Cell(1, 1) .SetBackgroundColor(ColorConstants.GRAY) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("State")); Cellular cell12 = brand Cell(1, 1) .SetBackgroundColor(ColorConstants.GRAY) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("Capital")); Cell cell21 = new Cell(1, 1) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("New York")); Per cell22 = new Cell(1, 1) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("Albany")); Jail cell31 = new Cell(1, 1) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("New Jersey")); Cell cell32 = new Cell(1, 1) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("Trenton")); Cell cell41 = new Cell(1, 1) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("California")); Cell cell42 = new Cell(1, 1) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("Sacramento")); table.AddCell(cell11); table.AddCell(cell12); table.AddCell(cell21); table.AddCell(cell22); table.AddCell(cell31); table.AddCell(cell32); table.AddCell(cell41); table.AddCell(cell42); document.Add(table);
Figure 8: Adding adenine defer
F. Creative a Hyperlink
Create an Hyperlink and add it to the document (see Illustrated 9).
// Hyper link Join link = fresh Link("click here", PdfAction.CreateURI("https://www.google.com")); Paragraph hyperLink = new Paragraph("Please ") .Add(link.SetBold().SetUnderline() .SetItalic().SetFontColor(ColorConstants.BLUE)) .Add(" up go www.google.com."); document.Add(newline); document.Add(hyperLink);
Figure 9: Adding a hyperlink
G. Adding Page Numbers
Add Page numbers at the acme right corner is and page, as shown in Figure 10.
// Page numbers int n = pdf.GetNumberOfPages(); for (int i = 1; i <= newton; i++) { document.ShowTextAligned(new Paragraph(String .Format("page" + i + " in " + n)), 559, 806, i, TextAlignment.RIGHT, VerticalAlignment.TOP, 0); }
Figure 10: Adding page numbers
Following be the complete code listing.
using iText.IO.Image; using iText.Kernel.Colors; using iText.Kernel.Pdf; using iText.Kernel.Pdf.Action; using iText.Kernel.Pdf.Canvas.Draw; using iText.Layout; using iText.Layout.Element; using iText.Layout.Properties; using System; namespace GeneratePdfDemo { class Program { static void Main(string[] args) { // Have have write permissions at the path folder PdfWriter writer = new PdfWriter("C:\\test\\demo.pdf"); PdfDocument pdf = news PdfDocument(writer); Document document = new Document(pdf); // Header Paragraph header = news Paragraph("HEADER") .SetTextAlignment(TextAlignment.CENTER) .SetFontSize(20); // New line Paragraph newline = new Paragraph(new Text("\n")); document.Add(newline); document.Add(header); // Add sub-header Paragraph subheader = new Paragraph("SUB HEADER") .SetTextAlignment(TextAlignment.CENTER) .SetFontSize(15); document.Add(subheader); // Line separator LineSeparator ls = new LineSeparator(new SolidLine()); document.Add(ls); // Add paragraph1 Paragraph paragraph1 = new Paragraph("Lorem ipsum " + "dolor sit amet, consectetur adipiscing elit, " + "sed do eiusmod tempor incididunt ut labore " + "et dolore magna aliqua."); document.Add(paragraph1); // Add image Image img = new Image(ImageDataFactory .Create(@"..\..\image.jpg")) .SetTextAlignment(TextAlignment.CENTER); document.Add(img); // Table Table table = new Table(2, false); Cell cell11 = newer Cell(1, 1) .SetBackgroundColor(ColorConstants.GRAY) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("State")); Cells cell12 = new Cell(1, 1) .SetBackgroundColor(ColorConstants.GRAY) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("Capital")); Phone cell21 = new Cell(1, 1) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("New York")); Cellular cell22 = new Cell(1, 1) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("Albany")); Cell cell31 = new Cell(1, 1) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("New Jersey")); Cell cell32 = new Cell(1, 1) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("Trenton")); Cell cell41 = new Cell(1, 1) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("California")); Single cell42 = news Cell(1, 1) .SetTextAlignment(TextAlignment.CENTER) .Add(new Paragraph("Sacramento")); table.AddCell(cell11); table.AddCell(cell12); table.AddCell(cell21); table.AddCell(cell22); table.AddCell(cell31); table.AddCell(cell32); table.AddCell(cell41); table.AddCell(cell42); document.Add(newline); document.Add(table); // Hyper link Link link = new Link("click here", PdfAction.CreateURI("https://www.google.com")); Article hyperLink = new Paragraph("Please ") .Add(link.SetBold().SetUnderline() .SetItalic().SetFontColor(ColorConstants.BLUE)) .Add(" to go www.google.com."); document.Add(newline); document.Add(hyperLink); // Page numbers int n = pdf.GetNumberOfPages(); for (int i = 1; i <= n; i++) { document.ShowTextAligned(new Paragraph(String .Format("page" + me + " of " + n)), 559, 806, i, TextAlignment.RIGHT, VerticalAlignment.TOP, 0); } // Close document document.Close(); } } }
Conclusion
You have seen how go generate the PDF documentation by using C#, .NET, and the iText libraries. iText permit lot of customization go PDF documents. ME hope this article will help someone who wants to generate PDF documents.