Duplicate a Page inside a PDF Document in C#

This post focuses on providing a solution in C# to duplicate a page within a PDF document. In order to better show the effect, I create a PDF page from scratch and insert some visible materials (text and image) into it, then duplicate the page with 5 copies within the document.

Before start, please download .NET PDF component and add the DLL to visual studio as reference.

Code Snippets:

Step 1: Initialize a new instance of PdfDocument class and add a blank page to it.

PdfDocument pdf = new PdfDocument();

PdfPageBase page = pdf.Pages.Add();

Step 2: Insert text.

PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f);

PdfSolidBrush brush = new PdfSolidBrush(Color.SlateGray);

PdfStringFormat leftAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

page.Canvas.DrawString(“janewdaisy.wordpress.com”, font, brush, 0, 20, leftAlignment);

Step 3: Insert image.

PdfImage image = PdfImage.FromFile(@”..\..\Hello World.jpg”);

float width = image.Width * 0.8f;

float height = image.Height * 0.5f;

page.Canvas.DrawImage(image, 0, 40, width, height);

Step 4: Add 5 pages into the document based on the template created with the first page.

SizeF size = page.Size;

PdfTemplate template = page.CreateTemplate();

for (int i = 0; i < 5; i++)

{

    page = pdf.Pages.Add(size, new PdfMargins(0));

    page.Canvas.DrawTemplate(template, new PointF(0, 0));

}

Step 5: Save the file.

pdf.SaveToFile(“Result.pdf”);

Output

DuplicatePage

Full Code

using Spire.Pdf;

using Spire.Pdf.Graphics;

using System.Drawing;

using Spire.Pdf.Graphics;

 

namespace DuplicatePdfPage

{

    class Program

    {

        static void Main(string[] args)

        {

            PdfDocument pdf = new PdfDocument();        

            PdfPageBase page = pdf.Pages.Add();

            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f);

            PdfSolidBrush brush = new PdfSolidBrush(Color.SlateGray);

            PdfStringFormat leftAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

            page.Canvas.DrawString(“janewdaisy.wordpress.com”, font, brush, 0, 20, leftAlignment);

            PdfImage image = PdfImage.FromFile(@”..\..\Hello World.jpg”);

            float width = image.Width * 0.8f;

            float height = image.Height * 0.5f;

page.Canvas.DrawImage(image, 0, 40, width, height);

            SizeF size = page.Size;

            PdfTemplate template = page.CreateTemplate();

            for (int i = 0; i < 5; i++)

            {

               page = pdf.Pages.Add(size, new PdfMargins(0));

               page.Canvas.DrawTemplate(template, new PointF(0, 0));

            }

            pdf.SaveToFile(“Result.pdf”);

            System.Diagnostics.Process.Start(“Result.pdf”);

        }

    }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s