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:
- Sizing pages using fixed layout API.
- Sizing pages using flow layout API.
- 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.
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)
|
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)
|
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].
Downloadable version of this article can be found by the following link [PDF].
No comments:
Post a Comment