DsPDF offers a variety of ways to merge and append PDF documents together. It can even merge multiple PDFs onto the same individual page. For example, we could merge four PDF pages onto one new page. This can be used to create a shrunken-down outline for your document.
First, we’ll load in our sample document. We are aiming to merge four pages onto each page of our new document.
GcPdfDocument doc = new GcPdfDocument();
FileStream fs = new FileStream("Sample.pdf", FileMode.Open);
doc.Load(fs);
Once this is done, we will loop through and draw four pages of our original document onto each page of our new document, and save the result.
GcPdfDocument newDoc = new GcPdfDocument();
if(doc.Pages.Count > 0)
{
for (int i = 0; i <= (doc.Pages.Count - 1) / 4; i++)
newDoc.NewPage();
for (var i = 0; i < doc.Pages.Count; i++)
{
var page = newDoc.Pages[i / 4];
var size = page.Size;
var newGraphics = page.Graphics;
var formXObject = new FormXObject(newDoc, doc.Pages[i]);
int pos = i % 4;
switch (pos)
{
case 0:
newGraphics.DrawForm(formXObject, new RectangleF(0, 0, size.Width / 2, size.Height / 2), null, ImageAlign.StretchImage);
break;
case 1:
newGraphics.DrawForm(formXObject, new RectangleF(size.Width / 2, 0, size.Width / 2, size.Height / 2), null, ImageAlign.StretchImage);
break;
case 2:
newGraphics.DrawForm(formXObject, new RectangleF(0, size.Height / 2, size.Width / 2, size.Height / 2), null, ImageAlign.StretchImage);
break;
case 3:
newGraphics.DrawForm(formXObject, new RectangleF(size.Width / 2, size.Height / 2, size.Width / 2, size.Height / 2), null, ImageAlign.StretchImage);
break;
}
}
}
newDoc.Save("Result.pdf");
Process.Start(new ProcessStartInfo() { UseShellExecute = true, FileName = "Result.pdf" });
Here is the first page of our resulting document:
Tye Glenz