How to Rotate a Table's Cells Into Runtime Generated PDF

Content

I will explain how to rotate a table's cell into a runtime generated PDF 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 projects and iTextSharp packages.

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

Step A

Create a new website named "Website1".

Website1

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.

    NuGet Packages

    Then a popup windows will open like:

    popup windows

    Type the "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.

    ItextSharp

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

    installation

    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.   
    9.         PdfPTable tbl = new PdfPTable(1);  
    10.         PdfPCell cell = new PdfPCell(new Phrase("This is 90 degrees rotated text"));  
    11.         cell.Rotation = 90;  
    12.         tbl.AddCell(cell);  
    13.   
    14.         cell = new PdfPCell(new Phrase("This is 180 degrees rotated text"));  
    15.         cell.Rotation = 180;  
    16.         tbl.AddCell(cell);  
    17.           
    18.         cell = new PdfPCell(new Phrase("This is 360 degrees rotated text"));  
    19.         cell.Rotation = 360;  
    20.         tbl.AddCell(cell);  
    21.   
    22.         document.Add(tbl);  
    23.   
    24.         document.Close();  
    25.     }  
    26.     catch (Exception ex)  
    27.     {  
    28.   
    29.     }  
    30. }  
    Here the "cell.Rotation" property is used to rotate the table cell.

    Note 1: You can only rotate the cell in multiples of 90.

    rotate the cell

    Note 2:

    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:

    open the Folder

  2. Click on the "Generate PDF" button.

Result:

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

    Open the PDF

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

    PDF file