Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert itextsharp PDF from portrait to Landscape mode

I have a PDF generation code which was previously downloaded in Portait mode and the code behind is shown below.

Document doc = new Document(PageSize.A4, 88f, 88f, 10f, 10f);

which was working properly.

Now I need the same PDF to be converted to Landscape mode, I googled it and found this code.

Document doc = new Document(new Rectangle(288f, 144f), 10, 10, 10, 10);
doc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());

But still its displaying in Portrait mode.Any help appreciated.

like image 513
Shreyas Achar Avatar asked Apr 01 '14 05:04

Shreyas Achar


2 Answers

You use

Document doc = new Document(PageSize.A4, 88f, 88f, 10f, 10f);

for portrait PDF. The PageSize.A4 is defined as

Rectangle A4 = new RectangleReadOnly(595,842);

Thus, one way to create a landscape PDF would be to use a RectangleReadOnly with switched width and height values:

Document doc = new Document(new RectangleReadOnly(842,595), 88f, 88f, 10f, 10f);

Alternatively a rotated version of the original rectangle should work, too:

Document doc = new Document(new RectangleReadOnly(595,842,90), 88f, 88f, 10f, 10f);
like image 191
mkl Avatar answered Nov 10 '22 00:11

mkl


Change

Doc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
like image 34
Haris N I Avatar answered Nov 09 '22 23:11

Haris N I