C # Copy a PDF page to another PDF document

Source: Internet
Author: User

C # Copy a PDF page to another PDF document

Sometimes we may have a need to copy a PDF page from one PDF document to another PDF document. Because PDF documents are not as well edited as Word documents, copying is also relatively less easy. Writing this article is primarily about sharing a simple and easy-to-implement approach-using C # to copy the pages of a PDF document, including text, pictures, and backgrounds, to a specific location in another PDF document.

Here are the two PDF files I prepared:


Goal: Copy the first page of the PDF document on the left to the location on the second page of the PDF document on the right.

Code implementation:

Step 1: Initialize an object of the Pdfdocument class Doc1 and load the first PDF document.

Pdfdocument Doc1 = new Pdfdocument ();d Oc1. LoadFromFile ("Fairy tale. pdf");


Step 2: Initialize an object of the Pdfdocument class DOC2 and load the second PDF document.

Pdfdocument doc2 = new Pdfdocument ();d oc2. LoadFromFile ("A variety of dim sum practices. pdf");


Step 3: get the first page of the first PDF document and its page size, and create a PDF template based on the first page.

Pdfpagebase page =doc1. Pages[0]; SizeF size = page. Size; Pdftemplate template =page. Createtemplate ();


Step 4: Copy the first page of the first PDF document into a second PDF document.

Call the Insert (Intindex, SizeF size, pdfmargins margins) method, and in the second PDF document, insert a new page with the same size as the first page to the document's specified location (this is the second page), Then apply the template you created in step 3 to the page.

Doc2. Pages.insert (1, size, newpdfmargins (0, 0));d oc2. PAGES[1]. Canvas.drawtemplate (template, New PointF (0, 0));


If you want to copy the first page to the last page of a second document, use the following code to add a new page to the end of the second document, and then apply the template to the page.

Doc2. Pages.Add (Size, new Pdfmargins (0, 0));

Step 5: Save the file and reopen it.

Doc2. SaveToFile ("copy. pdf"); System.Diagnostics.Process.Start ("copy. pdf");

The effect after copying:


All code:

Using system;using system.collections.generic;using system.linq;using system.text;using Spire.Pdf;using Spire.pdf.graphics;using System.Drawing; Namespace copy PDF page to another PDF document {    class program    {        static void Main (string[] args)        {            Pdfdocument Doc1 = Newpdfdocument ();           Doc1. LoadFromFile ("Fairy tale. pdf");             Pdfdocument doc2 = Newpdfdocument ();           Doc2. LoadFromFile ("A variety of dim sum practices. pdf");             Pdfpagebase page = Doc1. Pages[0];            SizeF size = page. Size;            Pdftemplate template = page. Createtemplate ();            Doc2. Pages.insert (1, size, new Pdfmargins (0, 0));           Doc2. PAGES[1]. Canvas.drawtemplate (template, newpointf (0, 0));            Doc2. SaveToFile ("copy. pdf");           System.Diagnostics.Process.Start ("copy. pdf");}}    

Note: Here I am using a third-party software spire.pdf, if your PDF document is not more than 10 pages, you can go to CodePlex download the free version of it.

Thank you for reading!

C # Copy a PDF page to another PDF document

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.