XDoc.PDF
Features
Tech Specs
How-to C#
How-to VB.NET
Pricing
C# PDF: How to HTML5 Viewer & Editor PDF Create PDF Export File and Page Process PDF Read PDF Write Form Process Document Protect Annotation & Drawing PDF Print WPF Viewer & Editor Work with Other SDKs Barcode Read Barcode Create OCR Twain
Page: Extract, Copy and Paste PDF Pages
  |  
Home ›› XDoc.PDF ›› C# PDF: Extract, Copy and Paste PDF Pages

How to extract, copy, paste PDF document pages using C#.net


Easy to Use C# Code to Extract PDF Pages, Copy Pages from One PDF File and Paste into Others in C#.NET Program. Free Online Trial Download.




Free PDF document processing SDK supports PDF page extraction, copying and pasting in Visual Studio .NET project


Free .NET PDF manipulation library for quick integration in both Visual C# .NET WinForms and ASP.NET application


Online C# source code for extracting, copying and pasting PDF pages in C#.NET console class


Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint


Able to extract PDF pages and save changes to original PDF file in C#.NET


Able to extract single or multiple pages from adobe PDF file and save into a new PDF file in .NET framework


Ability to copy selected PDF pages and paste into another PDF file


The portable document format, known as PDF document, is a widely-used form of file that allows users to open & read documents even though they are using different types of word processors. Besides, the capacity to be locked against editing or processing by others makes PDF file become increasingly popular among large enterprises and organizations.


With the purpose of helping C#.NET developers manipulate and edit PDF document file in a programming way, RasterEdge expressly designs this C#.NET PDF document imaging SDK, XDoc.PDF, which offers mature and easy to use APIs for programmers to handle & process PDF file at a page level. In this online article, we will address the robust functionality to copy and paste PDF page(s) in C#.NET document imaging application.





C# Clone a PDF Page


Note: When you get the error "Could not load file or assembly 'RasterEdge.Imaging.Basic' or any other assembly or one of its dependencies. An attempt to load a program with an incorrect format", please check your configure as follows:

       

       If you are using x64 libraries/dlls, Right click the project -> Properties -> Build -> Platform target: x64.

       

       If using x86, the platform target should be x86.




        #region clone one pdf page
        internal static void cloneOnePage()
        {
            String filepath = @"";
            String outPutFilePath = @"";
            PDFDocument doc = new PDFDocument(filepath);

            // Copy the first page of PDF document.
            PDFPage page = (PDFPage)doc.DuplicatePage(1);

            // Do further manipulations ...

        }
        #endregion




C# Copy and Paste PDF Pages





        #region Copy and Paste PDF Pages
        internal static void cloneAndPastePages()
        {
            // Copy three pages from test1.pdf and paste into test2.pdf.
            PDFDocument pdf = new PDFDocument(@"C:\test1.pdf");
            PDFDocument pdf2 = new PDFDocument(@"C:\test2.pdf");
            int[] pageindexes = new int[] { 1, 2, 4 };
            BasePage[] pages = pdf.DuplicatePages(pageindexes);
            pdf2.InsertPages(pages, 2);
        }
        #endregion




C# Copy and Replace PDF Pages





        #region Copy and Replace PDF Pages
        internal static void cloneAndReplacePages()
        {
            // Load the PDF file that provides the page object.
            String resFilePath = @"C:\2.pdf";
            PDFDocument resDoc = new PDFDocument(resFilePath);

            // Get the 1st page in the document.
            PDFPage page = (PDFPage)resDoc.GetPage(0);

            // Get PDFDocument object from a source file.
            String inputFilePath = @"C:\1.pdf";
            PDFDocument doc = new PDFDocument(inputFilePath);

            // Replace the 3rd page by the PDFPage object.
            int pageIndex = 2;
            doc.UpdatePage(page, pageIndex);

            // Save the PDFDocument.
            String outputFilePath = @"C:\Output.pdf";
            doc.Save(outputFilePath);
        }
        #endregion




C# Extract PDF Pages and Save into a New PDF File





        #region Extract PDF Pages and Save into a New PDF File
        internal static void extractPagesAndSaveToNewFile()
        {
            // Get PDFDocument object from a source file.
            String inputFilePath = @"C:\1.pdf";
            PDFDocument doc = new PDFDocument(inputFilePath);

            // Select pages.
            List<int> pageIndexes = new List<int>();
            pageIndexes.Add(2);   // The 3rd page.
            pageIndexes.Add(0);   // The 1st page.
            pageIndexes.Add(3);   // The 4th page.

            // Create the new document with 3 pages.
            PDFDocument newDoc = (PDFDocument)doc.GetMultiDocument(pageIndexes);

            // Save the PDFDocument.
            String outputFilePath = @"C:\Output.pdf";
            newDoc.Save(outputFilePath);
        }
        #endregion




C# Extract PDF Pages and Overwrite the Original PDF File





        #region Extract PDF Pages and Overwrite the Original PDF File
        internal static void extractPagesAndOverrideOriginalFile()
        {
            // Get PDFDocument object from a source file.
            String inputFilePath = @"C:\1.pdf";
            PDFDocument doc = new PDFDocument(inputFilePath);

            // Select pages.
            List<int> pageIndexes = new List<int>();
            pageIndexes.Add(2);   // The 3rd page.
            pageIndexes.Add(0);   // The 1st page.
            pageIndexes.Add(3);   // The 4th page.

            // Create the new document with 3 pages.
            PDFDocument newDoc = (PDFDocument)doc.GetMultiDocument(pageIndexes);

            // Save the PDFDocument.
            String outputFilePath = @"C:\1.pdf";
            newDoc.Save(outputFilePath);
        }
        #endregion




.NET Class Namespace Required



In order to run the sample code, the following steps would be necessary.


Add necessary references:


  RasterEdge.Imaging.Basic.dll


  RasterEdge.Imaging.Basic.Codec.dll


  RasterEdge.Imaging.Drawing.dll


  RasterEdge.Imaging.Font.dll


  RasterEdge.Imaging.Processing.dll


  RasterEdge.XImage.Raster.dll


  RasterEdge.XImage.Raster.Core.dll


  RasterEdge.XDoc.PDF.dll


Use corresponding namespaces;


  using RasterEdge.Imaging.Basic;


  using RasterEdge.XDoc.PDF;





public override void ExtractPages(int[] pageIndexes, String outputFilePath)

Description:
     Extract specified pages and save them into a new file.

Parameters:



public override void ExtractPages(int[] pageIndexes, Stream outputStream)

Description:
     Extract specified pages and save them into a new stream.

Parameters:



public override BasePage DuplicatePage(int pageIdx)

Description:
     Copy specified page from the input PDF file.

Parameters:

Return:
     BasePage object, null if failed.



public BasePage[] DuplicatePages(int[] pageIndexes)

Description:
     Copy specified page from the input PDF file.

Parameters:

Return:
     An array of BasePage object, null if failed.