Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iTextSharp: table in landscape

I'm using iTextSharp to generate a large document. In this document I want some specific pages in landscape. All the rest is portrait. Does anyone know how I can do this? Starting a new document is not an option.

Thanks!

like image 203
Ignace Avatar asked Mar 01 '26 08:03

Ignace


1 Answers

You can set the document size and it will affect the next pages. Some snippets:

Set up your document somewhere (you know that already):

  var document = new Document();
  PdfWriter pdfWriter = PdfWriter.GetInstance(
    document, new FileStream(destinationFile, FileMode.Create)
  );
  pdfWriter.SetFullCompression();
  pdfWriter.StrictImageSequence = true;
  pdfWriter.SetLinearPageMode();           

Now loop over your pages (you probably do that as well already) and decide what page size you want per page:

 for (int pageIndex = 1; pageIndex <= pageCount; pageIndex++) {
    // Define the page size here, _before_ you start the page.
    // You can easily switch from landscape to portrait to whatever
    document.SetPageSize(new Rectangle(600, 800));          

    if (document.IsOpen()) {
      document.NewPage();
    } else {
      document.Open();
    }
  }
like image 154
Benjamin Podszun Avatar answered Mar 03 '26 21:03

Benjamin Podszun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!