iTextSharp:横向表格

发布于 2024-11-02 16:02:01 字数 94 浏览 0 评论 0原文

我正在使用 iTextSharp 生成一个大文档。在本文档中,我想要一些横向的特定页面。其余的都是肖像。有谁知道我该怎么做? 开始新文档不是一个选择。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天赋异禀 2024-11-09 16:02:01

您可以设置文档大小,它将影响下一页。一些片段:

在某处设置文档(您已经知道了):

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

现在循环您的页面(您可能也已经这样做了)并决定每页所需的页面大小:

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

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();
    }
  }
千纸鹤 2024-11-09 16:02:01

试试这个代码:

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

}

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文