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 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: Two Pages on One
Modified on 2015/09/14 10:20
by
Thomas Hövel
Categorized as
PDFsharp Samples
,
Samples
{s:navigationPrevUpNext|TextLayout-sample|PDFsharpSamples|Unicode-sample} This sample shows how to place two pages of an existing document on one landscape orientated page of a new document. ==PDF Output File== See the PDF file created by this sample: {s:PdfLink|TwoPagesOnOne-sample%2fTwoPagesOnOne_output.pdf|output.pdf} (37 kB) ==Screen Shots== Here is a sample screen shot: {s:ImageThumbLink|Two Pages on One screen shot|TwoPagesOnOne-sample%2fTwoPagesOnOneth.png|TwoPagesOnOne-sample%2fTwoPagesOnOne.png} ==Source Code== This is the whole source code needed to create the PDF file: {s:beginCsharp} // Get a fresh copy of the sample PDF file string filename = "Portable Document Format.pdf"; File.Copy(Path.Combine("../../../../../PDFs/", filename), Path.Combine(Directory.GetCurrentDirectory(), filename), true); // Create the output document PdfDocument outputDocument = new PdfDocument(); // Show single pages // (Note: one page contains two pages from the source document) outputDocument.PageLayout = PdfPageLayout.SinglePage; XFont font = new XFont("Verdana", 8, XFontStyle.Bold); XStringFormat format = new XStringFormat(); format.Alignment = XStringAlignment.Center; format.LineAlignment = XLineAlignment.Far; XGraphics gfx; XRect box; // Open the external document as XPdfForm object XPdfForm form = XPdfForm.FromFile(filename); for (int idx = 0; idx < form.PageCount; idx += 2) { // Add a new page to the output document PdfPage page = outputDocument.AddPage(); page.Orientation = PageOrientation.Landscape; double width = page.Width; double height = page.Height; int rotate = page.Elements.GetInteger("/Rotate"); gfx = XGraphics.FromPdfPage(page); // Set page number (which is one-based) form.PageNumber = idx + 1; box = new XRect(0, 0, width / 2, height); // Draw the page identified by the page number like an image gfx.DrawImage(form, box); // Write document file name and page number on each page box.Inflate(0, -10); gfx.DrawString(String.Format("- {1} -", filename, idx + 1), font, XBrushes.Red, box, format); if (idx + 1 < form.PageCount) { // Set page number (which is one-based) form.PageNumber = idx + 2; box = new XRect(width / 2, 0, width / 2, height); // Draw the page identified by the page number like an image gfx.DrawImage(form, box); // Write document file name and page number on each page box.Inflate(0, -10); gfx.DrawString(String.Format("- {1} -", filename, idx + 2), font, XBrushes.Red, box, format); } } // Save the document... filename = "TwoPagesOnOne_tempfile.pdf"; outputDocument.Save(filename); // ...and start a viewer. Process.Start(filename); {s:endCsharp} {s:sampleSourceCode}
Meta Keywords:
Meta Description:
Change Comment:
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