8

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Shreyas Achar
  • 1,407
  • 5
  • 36
  • 63

3 Answers3

12

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);
mkl
  • 90,588
  • 15
  • 125
  • 265
  • Haris' answer is simpler – Ricardo Appleton May 22 '17 at 11:39
  • @RicardoAppleton *"Haris' answer is simpler"* - that may be, but if you look at the *question*, you'll see that Haris' proposal is identical to one line the OP had tried and (for which reasons ever) was not happy with. – mkl May 22 '17 at 12:17
7

Change

Doc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
Haris N I
  • 6,474
  • 6
  • 29
  • 35
0

maybe the problem is when you are giving it 10,10,10,10. how can you see it as landscape?? change it to as per you margins and try this link

Community
  • 1
  • 1
user3458227
  • 93
  • 1
  • 1
  • 10
  • remove this Document doc = new Document(PageSize.A4, 88f, 88f, 10f, 10f); and do Document document = new Document(PageSize.A4, 25, 25, 30, 30); try it – user3458227 Apr 01 '14 at 06:08