How to Set Orientation of Runtime Generated PDF

Content

I will explain how to generate a runtime PDF with both Landscape and Portrait page mode functionality using Visual Studio Ultimate 2015 Preview.

In brief, I will generate the runtime PDF using ITextSharp.

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

You can find it in the attached file in the "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".

new website

Step B

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

  1. Click on Tools

  2. NuGet Package Manager

  3. Manage NuGet Packages for Solution.
manage nuget package

Then a popup windows will open like:

pop window

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

add and install

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

library installed

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 te2xt to "Generate PDF".

    add button
  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 pdfDoc = new Document(PageSize.A4.Rotate(), 0, 0, 5, 0);  
    6.         PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);  
    7.         pdfDoc.Open();  
    8.         pdfDoc.Add(new Paragraph("This is Landscape Page"));  
    9.   
    10.         pdfDoc.SetPageSize(new Rectangle(850f, 1100f));            
    11.         pdfDoc.NewPage();  
    12.         pdfDoc.Add(new Paragraph("This is Portrait Page"));  
    13.         pdfWriter.CloseStream = false;  
    14.         pdfDoc.Close();  
    15.         Response.Buffer = true;  
    16.         Response.ContentType = "application/pdf";  
    17.         Response.AddHeader("content-disposition""attachment;filename=Test.pdf");  
    18.         Response.Cache.SetCacheability(HttpCacheability.NoCache);  
    19.     }  
    20.     catch (Exception ex)  
    21.     {  
    22.     }  
    23. }  
    Here "PageSize.A4.Rotate()" is used for landscape page mode and SetPageSize(new Rectangle(850f, 1100f)) is used for converting it to the Portrait page mode.

    Note: You can provide any name of the generated file and any text that you want to print in the PDF file. For example here I provided "test.pdf".
Step D
  1. Run the page that will be like:

    run page

  2. Click on the "Generate PDF" button and open it.

    open pdf
Result

resulted pdf


Similar Articles