Add a New Page Into Runtime Generated PDF

Content

I will explain how to add a new page into a runtime generated PDF functionality using Visual Studio Ultimate 2015 Preview.

I will briefly generate the runtime PDF using ITextSharp.

Note: I will use the "iTextSharp.dll" as a PDF generator library. You can download it from itextsharp and iTextSharp.

You can find it on the attached file in "bin" folder of this article but the best way to add the ItextSharp is in Step B.

Step A

Create a new Website named "Website1".

Website1

Step B

Add the ItextSharp library from NuGet package using the following sub-steps:

  1. Click on Tools
  2. NuGet Package Manager
  3. Manage NuGet Packages for Solution.

ItextSharp

Then a popup window will open like:

popup windows

Type "ItextSharp" into the Search TextBox. Then you will get the "ItextSharp" library in the left pane. To add it to the website just click on the Install button of the right pane.

Search textbox

After the installation, you see the "Right" sign in the left pane that indicates that the library has been installed.

library

You can also check it from the Solution Explorer.

solution explorer

Step C

  1. Add a button to the default page named "Default.aspx" and change the text to "Generate PDF".

    Generate PDF

  2. Double-click on the button to generate an Onclick event (to generate the PDF) on the Form.

  3. Add the following 2 namespaces to the top of the ".cs" file:
    1. using iTextSharp.text;  
    2. using iTextSharp.text.pdf;  
    3. using System.IO;  
  4. Write the code to generate the PDF file on the click event of the button:
    1. protected void Button1_Click(object sender, EventArgs e)  
    2. {  
    3.     try  
    4.     {  
    5.         Document document = new Document();  
    6.         PdfWriter.GetInstance(document, new FileStream("E:/Rahul/test.pdf", FileMode.Create));  
    7.         document.Open();  
    8.         Paragraph p1 = new Paragraph("This is First Page");  
    9.         document.Add(p1);//Add the paragarh to the document  
    10.         document.NewPage();  
    11.         Paragraph p2 = new Paragraph("This is Second Page");  
    12.         document.Add(p2);//Add the paragarh to the document  
    13.         document.Close();  
    14.     }  
    15.     catch (Exception ex)  
    16.     {  
    17.     }  
    18. }  
Here "document.NewPage()" is used for adding a new page.

document

Note

You can provide any name for the generated file and any text that you want to print in the PDF file. For example here I provided "test.pdf" and path also.

Step D
  1. Run the page that will be like:

    Run the page

  2. Click on the the "Generate PDF" button.

Result

  1. Follow the specified path and open the folder, you will see the PDF file.

    Generate PDF button

  2. Open the PDF file and see the both pages in the PDF file.

    open the Folder
    Page 1

    second page
                                                                                  Page 2

Up Next
    Ebook Download
    View all
    FileInfo in C#
    Read by 9.7k people
    Download Now!
    Learn
    View all