C# tutorial: import pages from PDF document

Import pages from a PDF document

In this tutorial, I am going to show how you can import pages from an existing PDF document to another PDF document. Suppose that you want to read pages from an existing PDF document and display pages in the new PDF document in a table layout as shown below. To acchieve the goal, you will use the GetImportedPage method of the PdfWriter class. This method returns a page object in type of PdfImportedPage. You can create an image from the page object and add it to a cell of a table. Below is the general form of the GetImportedPage method:

GetImportedPage(PdfReader reader, int index)

The reader argument allows you to pass the PdfReader object that is used to read pages from the existing document. The index argument is an integer number representing the page number that will be imported.

Now let's go through the detail steps to do the task. To import pages from an existing document to a new one, first you need to create a Document and PdfWriter objects as you did when you create a new PDF file from scratch. You will pass the path of the output or new PDF file to the PdfWriter class' constructor. Then create PdfReader object and pass the path of the existing PDF file to its constructor. After that you will use the GetImportedPage method of the PdfWriter class to import every page of the existing document. A for loop is used at this time to import every page, create an image object for each page, and add the image object to a cell of the table. The for loop will start from 1 to the number of pages found in the existing document. You can use the NumberOfPages property of the PdfReader class to get the number of pages. After the loop did its job, add the table object to the document. Then close the document.


display pages in table layout


Here is the complete C# code to import all pages from the existing PDF document (d:/iTextAction.pdf) to a new PDF document (d:/pdfcopied.pdf) that shows the pages in table layout.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace createpdf

class Program
static void Main(string[] args)
String pathin = "d:/iTextAction.pdf";
String pathout = "d:/pdfcopied.pdf";
PdfImportedPage page;
//create a document object
var doc = new Document(PageSize.A4);
//create PdfReader object to read from the existing document
PdfReader reader = new PdfReader(pathin);
//create PdfWriter object to write the pages to a new document
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(pathout, FileMode.Create));
//open the document for writing
//get number of pages
int pageNum = reader.NumberOfPages;
//create a two-column table
PdfPTable table = new PdfPTable(2);
for (int i = 1; i <= pageNum; i += 1)

//import the pages
page = writer.GetImportedPage(reader, i);

//create image objects from the pages
Image img = Image.GetInstance(page);

//add the images to cells of the table
PdfPCell cell = new PdfPCell(img, true);
//add the table to document
//close the document







This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.