Search code examples
c#pdf-generationitext

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!


Solution

  • 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();
        }
      }