pdf links

PDF Rendering
Convert PDF to Image (.NET)
Convert PDF to image on Android (Xamarin)
Convert PDF to image on iOS (Xamarin)
Convert PDF to image in Windows Store apps (.NET)
Convert PDF to image in Windows Phone apps (.NET)
PDF to image in Universal Windows Store apps (.NET)
Free PDF Viewer control for Windows Forms (.NET)
How to integrate PDF Viewer control in WPF app (.NET)
Creating WPF PDF Viewer supporting bookmarks (.NET)
Cross-platform PDF Viewer using GTK# (MONO)
Silverlight PDF viewer control (Silverlight 5)
Multithreaded PDF rendering (.NET)
Convert pdf to image in Silverlight app (C# sample)
How to set fallback fonts for PDF rendering (C#)
Avoiding the out-of-memory exception on rendering (C#)
PDF viewer single page application (WebAPI, AngularJS)
PDF viewer control for Windows 10 universal applications
Use custom ICC profile for CMYK to RGB conversion
PDF layers - separate images, text, annotations, graphics

PDF Forms Creation PDF Security
Conversion to PDF/A
Other topics
PDF Document Manipulation PDF Content Generation
Fixed and Flow layout document API (.NET)
Creation of grids and tables in PDF (C# sample)
How to create interactive documents using Actions (C# sample)
Text flow effects in PDF (C# sample)
How to generate ordered and bulleted lists in PDF (C# sample)
Convert HTML to PDF using flow layout API (C# sample)
How to use custom fonts for PDF generation (.NET)
Create document with differently sized pages (C#)
Create PDF documents using MONO (C#/MONO/Windows/OSX)
How to use background images for content elements (C#/PDF Kit/FlowLayout)
Add transparent images to PDF document (C#)
Draw round rect borders in PDF documents(C#)
ICC color profiles and and ICC based colors in PDF (C#)
How to use bidirectional and right to left text in PDF (C#)
Create PDF documents from XML templates (C# sample)
How to resize PDF pages and use custom stamps (C#)
Add header and footer to PDF page (.NET sample)
How to use clipping mask for drawing on PDF page
Fill graphics path with gradient brushes in PDF (Shadings)
Apitron PDF Kit and Rasterizer engine settings
Add layers to PDF page (optional content, C# sample)
How to create free text annotation with custom appearance
PDF Content Extraction PDF Navigation
PDF to TIFF conversion
Contact us if you have a PDF related question and we'll cover it in our blog.

2015-07-31

How to create PDF document containing pages of different size

Introduction


The vast majority of the PDF files we encounter has all pages sized equally, so they use some predefined paper size, e.g. A4, Letter, or any custom size.  But what if you’d like to mix page sizes within the same document? Using Apitron PDF Kit for .NET component, you’ll able to produce documents containing pages sized any way you want.

There are three possible scenarios we’d like to cover:
  1. Sizing pages using fixed layout API.
  2. Sizing pages using flow layout API.
  3. Producing PDF document using pages from separate documents. Using this method you create two or more documents containing pages of different size (or you already have them created), and combine them using merging technique or copying technique described in our blog.
The flow and fixed layout APIs are described in this article.

Sizing PDF pages using fixed layout API


For the fixed layout API it’s quite straightforward, you create new PDF page and specify its size in constructor. See the code below:

using (Stream outputStream = File.Create("document.pdf"))
{      
    // create new PDF document      
    using (FixedDocument pdfDocument = new FixedDocument())
    {                  
        // create page with width=300 and height=450
        pdfDocument.Pages.Add(new Page(new PageBoundary(new Boundary(0,0,300,450))));
        // create page with width=450 and height=300
        pdfDocument.Pages.Add(new Page(new PageBoundary(new Boundary(0, 0, 450, 300))));

        // save the result
        pdfDocument.Save(outputStream);                  
    }
}

Resulting PDF file is shown below:

Pic. 1 PDF document with mixed page sizes (fixed layout)

Pic. 1 PDF document with mixed page sizes (fixed layout)


Sizing PDF pages using flow layout API



The code below shows how to handle page sizing in case of flow layout:

using (Stream outputStream = File.Create("document.pdf"))
{
    // output page size
    Boundary mediaBox = new Boundary(250, 350);
    // create document
    FlowDocument doc = new FlowDocument(){Margin = new Thickness(10)};
    // add style for text blocks
    doc.StyleManager.RegisterStyle("textblock",
new Style(){Font = new Font(StandardFonts.HelveticaBold,20)});
    // add new page event handler to handle page sizing
    doc.NewPage += (newPageArgs) =>{
                        // change size for every even page
                        if ((newPageArgs.Context.CurrentPage & 1) == 1)
                        {
                            newPageArgs.PageBoundary =
                            new PageBoundary(new Boundary(mediaBox.Height, mediaBox.Width));
                        }
                   };
    // add some content
    doc.Add(new TextBlock("page 1"));
    doc.Add(new PageBreak());
    doc.Add(new TextBlock("page 2"));
    // save the result
    doc.Write(outputStream, new ResourceManager(), new PageBoundary(mediaBox));
}


The resulting document looks as follows:

Pic. 2 PDF document with mixed page sizes (flow layout)

Pic. 2 PDF document with mixed page sizes (flow layout)

Conclusion


Using the techniques described in this article you can easily create documents with pages of any desired size. Apitron PDF Kit for .NET component is flexible and powerful enough to help you implement any PDF processing task without much effort. You can download it by the following link. This library is cross-platform, so you can create web, mobile and desktop applications not only for Windows-based devices, but also for iOS and Android using Xamarin and Mono.

The product is extensively documented, and its download package includes many ready to use samples demonstrating how to deal with most common PDF processing tasks. Read our free book, browse documentation or ask us a question if you need any help.

Downloadable version of this article can be found by the following link [PDF].

No comments:

Post a Comment