【问题标题】:iTextSharp: table in landscapeiTextSharp:横向表格
【发布时间】:2010-01-07 09:15:41
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: c# pdf-generation itextsharp


    【解决方案1】:

    您可以设置文档大小,它会影响下一页。一些sn-ps:

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

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

    【讨论】:

    • 谢谢,之前真的帮了我大忙!
    • 很高兴它有帮助。去过那里之前.. ;)
    【解决方案2】:

    试试这个代码:

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

    }

    【讨论】:

      猜你喜欢
      • 2012-04-19
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      相关资源
      最近更新 更多