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".
Step B
Add the ItextSharp library from the NuGet package using the following sub-steps:
- Click on Tools
- NuGet Package Manager
- Manage NuGet Packages for Solution.
Then a popup windows will open like:
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.
After the installation, you see the "Right" sign in the left pane that indicaters that the library has been installed.
You can also check it from the Solution Explorer.
Step C
- Add a button to the default page named "Default.aspx" and change the text to "Generate PDF".
- Double-click on the button to generate an Onclick event (to generate the PDF) on the Form.
- Add the following 2 namespaces to the top of the ".cs" file:
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using System.IO;
- Write the code to generate the PDF file on the click event of the button:
- protected void Button1_Click(object sender, EventArgs e)
- {
- try
- {
- Document document = new Document();
- PdfWriter.GetInstance(document, new FileStream("E:/Rahul/test.pdf", FileMode.Create));
- document.Open();
-
- PdfPTable tbl = new PdfPTable(1);
- PdfPCell cell = new PdfPCell(new Phrase("This is 90 degrees rotated text"));
- cell.Rotation = 90;
- tbl.AddCell(cell);
-
- cell = new PdfPCell(new Phrase("This is 180 degrees rotated text"));
- cell.Rotation = 180;
- tbl.AddCell(cell);
-
- cell = new PdfPCell(new Phrase("This is 360 degrees rotated text"));
- cell.Rotation = 360;
- tbl.AddCell(cell);
-
- document.Add(tbl);
-
- document.Close();
- }
- catch (Exception ex)
- {
-
- }
- }
Here the "cell.Rotation" property is used to rotate the table cell.
Note 1: You can only rotate the cell in multiples of 90.
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
- Run the page that will be like:
- Click on the "Generate PDF" button.
Result:
- Follow the specified path and open the folder, you will see the PDF file.
- Open the PDF file and see both of the pages in the PDF file.