ASP.NET - ITextsharp landscape PDF using asp.net

Asked By asif hameed on 06-Apr-12 02:52 AM
Hi all,


I am trying to create Landscape PDF using iTextSharp but It is still showing portrait. I am using following code with rotate:


           Document document = new Document(PageSize.A4, 0, 0, 150, 20);
            FileStream msReport = new FileStream(Server.MapPath("~/PDFS/") + "Sample1.pdf", FileMode.Create);
          
            try
            {
                // creation of the different writers
                PdfWriter writer = PdfWriter.GetInstance(document, msReport);


                document.Open();


                PdfPTable PdfTable = new PdfPTable(1);
                PdfTable.SpacingBefore = 30f;


                PdfPCell PdfPCell = null;


                Font fontCategoryheader = new Font(Font.HELVETICA, 10f, Font.BOLD, Color.BLACK);


                for (int i = 0; i < 20; i++)
                {
                    PdfPCell = new PdfPCell(new Phrase(new Chunk("Sales Manager: ", fontCategoryheader)));
                    PdfPCell.BorderWidth = 0;
                    PdfPCell.HorizontalAlignment = Element.ALIGN_LEFT;




                    if (i % 2 == 0)
                        PdfPCell.BackgroundColor = Color.LIGHT_GRAY;




                    PdfPCell.PaddingBottom = 5f;
                    PdfPCell.PaddingLeft = 2f;
                    PdfPCell.PaddingTop = 4f;
                    PdfPCell.PaddingRight = 4f;
                    PdfTable.AddCell(PdfPCell);
                }




                document.Add(PdfTable);
                document.NewPage();




            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
            }




            finally
            {
                // we close the document 
                document.Close();
            }




Please suggest solution.




Thanks. 

dipa ahuja replied to asif hameed on 06-Apr-12 03:02 AM
This is the simple example
 
Try this way:
 
Download the dll of itextSharp.dll
 
http://sourceforge.net/projects/itextsharp/
 
and try this code
 
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
 
private void btnExport_Click(object sender, EventArgs e)
{
   Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
   PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("d:\\Test.pdf", FileMode.Create));
 
   doc.Open();//Open Document to write
 
 
   Paragraph paragraph = new Paragraph("data Exported From DataGridview!");
 
   doc.Add(paragraph);
   doc.Add(t1);
   doc.Close(); //Close document
   //
   MessageBox.Show("PDF Created!");
}
 
 
 
 
 
asif hameed replied to dipa ahuja on 06-Apr-12 03:07 AM
Hi:

I need landscape not Portrait pages

Regards,
Asif Hameed
Somesh Yadav replied to asif hameed on 06-Apr-12 03:25 AM
Call the Document object's SetPageSize() method:

document.Open();
document.Add(new Paragraph("Page 1: PORTRAIT"));
document.SetPageSize(new Rectangle(842, 595)); // A4
document.NewPage();
document.Add(new Paragraph("Page 2: LANDSCAPE"));