开发者

iTextSharp: table in landscape

开发者 https://www.devze.com 2023-02-28 03:50 出处:网络
I\'m using iTextSharp to generate a large document. In this document I want some s开发者_开发知识库pecific pages in landscape. All the rest is portrait. Does anyone know how I can do this?

I'm using iTextSharp to generate a large document. In this document I want some s开发者_开发知识库pecific 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!


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


try this code :

using System;
using System.IO;
using iText.Kernel.Events;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Samples.Sandbox.Events
{
    public class PageOrientations
    {
        public static readonly String DEST = "results/sandbox/events/page_orientations.pdf";
    public static readonly PdfNumber PORTRAIT = new PdfNumber(0);
    public static readonly PdfNumber LANDSCAPE = new PdfNumber(90);
    public static readonly PdfNumber INVERTEDPORTRAIT = new PdfNumber(180);
    public static readonly PdfNumber SEASCAPE = new PdfNumber(270);

    public static void Main(String[] args)
    {
        FileInfo file = new FileInfo(DEST);
        file.Directory.Create();

        new PageOrientations().ManipulatePdf(DEST);
    }

    protected void ManipulatePdf(String dest)
    {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

        // The default page orientation is set to portrait in the custom event handler.
        PageOrientationsEventHandler eventHandler = new PageOrientationsEventHandler();
        pdfDoc.AddEventHandler(PdfDocumentEvent.START_PAGE, eventHandler);
        Document doc = new Document(pdfDoc);

        doc.Add(new Paragraph("A simple page in portrait orientation"));

        eventHandler.SetOrientation(LANDSCAPE);
        doc.Add(new AreaBreak());
        doc.Add(new Paragraph("A simple page in landscape orientation"));

        eventHandler.SetOrientation(INVERTEDPORTRAIT);
        doc.Add(new AreaBreak());
        doc.Add(new Paragraph("A simple page in inverted portrait orientation"));

        eventHandler.SetOrientation(SEASCAPE);
        doc.Add(new AreaBreak());
        doc.Add(new Paragraph("A simple page in seascape orientation"));

        doc.Close();
    }

    private class PageOrientationsEventHandler : IEventHandler
    {
        private PdfNumber orientation = PORTRAIT;

        public void SetOrientation(PdfNumber orientation)
        {
            this.orientation = orientation;
        }

        public void HandleEvent(Event currentEvent)
        {
            PdfDocumentEvent docEvent = (PdfDocumentEvent) currentEvent;
            docEvent.GetPage().Put(PdfName.Rotate, orientation);
        }
    }
}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消