Automated generation and processing of PDF documents have become a demanding feature in recent days. In various applications, PDF documents are generated dynamically such as invoices, receipts, and different types of reports. In accordance with that, this article covers the basic implementation of PDF automation within .NET applications. Ultimately, you will learn how to create PDF files and insert text, images, tables, and other components using C#.
From the File menu, select Print. In the Print dialog box, from the bottom left, click on PDF. A drop down menu will appear. From the drop-down menu, select “Save as PDF”. Short training video on how to create PDF documents (.pdf files) from any Windows application that normally sends output to a printer by installing the free. Right Click to Create PDF File Step 1. Right Click Target File Find your target file on the computer that you want to create to PDF, then right-click it to choose 'Create to PDF' in the menu. Open Acrobat and choose “Tools” “Create PDF”. Select the file type you want to create a PDF from: single file, multiple files, scan, or other option. Click “Create” or “Next” depending on the file type. Follow the prompts to convert to PDF and save to your desired location.
Follow these easy steps to turn Microsoft Word files into PDFs: Click the Select a file button above, or drag and drop a file into the drop zone. Select the RTF, TXT, DOCX, or DOC file you want to convert to.
C# PDF API – Free Download
Aspose.PDF for .NET is a PDF file manipulation API that lets you generate and process PDF files from within your .NET applications. Using the API you can generate various kinds of reports and business documents dynamically. The API is available as a downloadable DLL as well as hosted on NuGet.
Create PDF Files in C#
Let’s start by creating a simple PDF document containing a text fragment. The following are the steps along with the API references.
- Create an object of Document class.
- Add a page to the document using Document.Pages.Add() method.
- Create a new TextFragment object and set its text.
- Add TextFragment to the Paragraphs collection of the page.
- Save the PDF file using Document.Save(String) method.
The following code sample shows how to create a simple PDF file using C#.
View the code on Gist.
Read more about creating complex PDF documents.
Edit Existing PDF in C#
Modifying a PDF file is as simple as creating a new one. Simply load the file using the Document class, perform your desired operations, and save it. The following are the steps to modify a PDF.
- Create an object of the Document class and provide a PDF file’s path to its constructor.
- Manipulate the pages or content of the document.
- Save the document using Document.Save() method.
The following code sample shows how to modify a PDF using C#.
View the code on Gist.
Insert Image in PDF using C#
Let’s now check how to insert an image into the PDF document. The following are the steps to do so.
- Create an object of Document class to open a PDF document.
- Access the page you want to add an image to using Page class.
- Add the image to the page’s Resources collection.
- Use operators to place the image on the page:
- GSave operator to save the current graphical state.
- ConcatenateMatrix operator to specify where the image is to be placed.
- Do operator to draw the image on the page.
- Finally, use the GRestore operator to save the updated graphical state.
- Save the PDF file.
The following code sample shows how to add an image to a PDF document using C#.
View the code on Gist.
Read more about insert images in PDF.
Create a Table in PDF using C#
The tables are an essential component of the documents that are used to organize the data in the form of rows and columns. Aspose.PDF for .NET provides you with quite a simple way to create and insert tables in PDF documents. The following are the steps to perform this operation.
- Load the PDF file using Document class.
- Initialize a table and set its columns and rows using Table class.
- Set table’s settings (i.e. borders).
- Populate table by creating rows using Table.Rows.Add() method.
- Add the table to the page using Document.Pages[1].Paragraphs.Add(Table) method.
- Save the PDF file.
The following code sample shows how to create and add a table in a PDF document in C#.
View the code on Gist.
Read more about creating tables in PDF.
Create a Form in PDF in C#
Forms in PDFs are used to collect the data from the readers. You can insert textboxes, checkboxes, radio buttons, and other supported controls in PDF forms. PDF format supports two types of forms; Acro forms and XFA forms (see details). The following are the steps to create and add forms in a PDF.
- Load the PDF file using Document class.
- Create form controls such as TextBoxField.
- Add control to the form using Document.Form.Add(textBoxField, 1) method.
- Save the PDF document.
The following code sample shows how to add forms to the PDF document using C#.
View the code on Gist.
Read more about creating forms in PDF.
Conclusion
In this post, you have learned how to create PDF files from scratch using C#. Furthermore, you have come to know how to insert various types of components such as text, images, tables, and forms in a PDF document. You can learn more about the PDF API using the documentation.
See Also
PDF (Portable Document Format) is a file format that has replicate all the characteristics of a printed document that you can read, write, print or forward to someone else. You can create PDF file programmatically from C# applications very easily. When you create documents, graphics etc. they look just like they would if printed. PDFsharp is the Open Source library that easily creates PDF documents from c# language. PDFSharp library allows you to create PDF files directly from your C# application.
Free PDF Library
There are many PDF libraries available in the web like PDFsharp, iTextSharp etc. The following program uses the PDFsharp library for creating pdf file from C# application.
PDFsharp library
PDFsharp is the Open Source .NET library that easily creates and processes PDF documents on the fly from any .NET language. You can freely download the Assemblies version from the following link: Download PDFsharp Assemblies
Steps to create PDF file programmatically.
1. Download the Assemblies from the above mentioned url.
2. Extract the .zip file to your desired location (filename :PDFsharp-MigraDocFoundation-Assemblies-1_31.zip)
3. Create a New C# Project
4. Add pdfsharp reference in C# Project
5. In Solution Explorer, right-click the project node and click Add Reference. In this project we are using GDI+ libraries.
6. In the Add Reference dialog box, select the BROWSE tab and select the Assembly file location (step 2)
7. Select all files and click OK
After you add the reference files to your C# project , solution explorer look like the following image.
Now you can start programming to create a New PDF document.
First you should create a PDF document Object
Next step is to create a an Empty page.
Then create an XGraphics Object
Also create the Font object from XFont
Next step is that you should write the content to PDF File.
XStringFormats.Center will place the your content to the center of the PDF page.
Now you can save the document as .pdf
You can specify the file path in the pdf.save function.
After save the file , you can double click and open the pdf file. Then you can see the following content in your pdf file.
Drag a Button on the Form and copy and paste the following code in the button1_Click event.