Welcome
Guest
•
Login
PDFsharp and MigraDoc Wiki
Navigation
¶
Main Page
Random Page
Create a new Page
All Pages
Categories
Navigation Paths
File Management
Create Account
Quick Search
»
Visit our new
Tailored Solutions & Support
site and find out how we can assist you.
Visit the new
Website for PDFsharp & MigraDoc Foundation 6.0 for .NET 6
and find information about the new version for Windows, Linux, and other platforms.
Back
PDFsharp Sample: Booklet
Modified on 2015/09/14 10:24
by
Thomas Hövel
Categorized as
PDFsharp Samples
,
Samples
{s:navigationPrevUpNext|Annotations-sample|PDFsharpSamples|Bookmarks-sample} This sample shows how to produce a booklet by placing two pages of an existing document on one landscape orientated page of a new document (e. g. for duplex printing). ==PDF Output File== See the PDF file created by this sample: {s:PdfLink|Booklet-sample%2fBooklet_output.pdf|output.pdf} (36 kB) ==Source Code== Here is the code that does the work: {s:beginCsharp} // Create the output document PdfDocument outputDocument = new PdfDocument(); // Show single pages // (Note: one page contains two pages from the source document. // If the number of pages of the source document can not be // divided by 4, the first pages of the output document will // each contain only one page from the source document.) outputDocument.PageLayout = PdfPageLayout.SinglePage; XGraphics gfx; // Open the external document as XPdfForm object XPdfForm form = XPdfForm.FromFile(filename); // Determine width and height double extWidth = form.PixelWidth; double extHeight = form.PixelHeight; int inputPages = form.PageCount; int sheets = inputPages / 4; if (sheets * 4 < inputPages) sheets += 1; int allpages = 4 * sheets; int vacats = allpages - inputPages; for (int idx = 1; idx <= sheets; idx += 1) { // Front page of a sheet: // Add a new page to the output document PdfPage page = outputDocument.AddPage(); page.Orientation = PageOrientation.Landscape; page.Width = 2 * extWidth; page.Height = extHeight; double width = page.Width; double height = page.Height; gfx = XGraphics.FromPdfPage(page); // Skip if left side has to remain blank XRect box; if (vacats > 0) vacats -= 1; else { // Set page number (which is one-based) for left side form.PageNumber = allpages + 2 * (1 - idx); box = new XRect(0, 0, width / 2, height); // Draw the page identified by the page number like an image gfx.DrawImage(form, box); } // Set page number (which is one-based) for right side form.PageNumber = 2 * idx - 1; box = new XRect(width / 2, 0, width / 2, height); // Draw the page identified by the page number like an image gfx.DrawImage(form, box); // Back page of a sheet page = outputDocument.AddPage(); page.Orientation = PageOrientation.Landscape; page.Width = 2 * extWidth; page.Height = extHeight; gfx = XGraphics.FromPdfPage(page); // Set page number (which is one-based) for left side form.PageNumber = 2 * idx; box = new XRect(0, 0, width / 2, height); // Draw the page identified by the page number like an image gfx.DrawImage(form, box); // Skip if right side has to remain blank if (vacats > 0) vacats -= 1; else { // Set page number (which is one-based) for right side form.PageNumber = allpages + 1 - 2 * idx; box = new XRect(width / 2, 0, width / 2, height); // Draw the page identified by the page number like an image gfx.DrawImage(form, box); } } // Save the document... filename = "Booklet.pdf"; outputDocument.Save(filename); // ...and start a viewer. Process.Start(filename); {s:endCsharp} {s:sampleSourceCode}
Meta Keywords:
Meta Description:
Change Comment:
Visit our new
Tailored Solutions & Support
site and find out how we can assist you.
Visit the new
Website for PDFsharp & MigraDoc Foundation 6.0 for .NET 6
and find information about the new version for Windows, Linux, and other platforms.
Miscellaneous
Home
PDFsharp
FAQ
Samples
Articles
MigraDoc
FAQ
Samples
Articles
ScrewTurn Wiki version 3.0.5.600. Some of the icons created by
FamFamFam
.
Impressum - Privacy Policy, Data Protection Declaration, Legal Notice