Pages

Create a blank page:

Create a new 8.5x11 blank page and add it to the end of the document page sequence.

Note that after a page is created, it does not yet belong to the page sequence of the document. In order for the page to become "visible," it must be placed within the page sequence; PagePushBack() inserts page x at the last page position in the document.

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		// This property is necessary only for licensed version.
		//SautinSoft.Pdf.Serial = "XXXXXXXXXXX";

		using (var document = new PdfDocument())
		{
			// Create a new page
			var page = document.Pages.Add();
			document.PagePushBack(x);
		}
		document.Save("Empty.pdf");
	}
}

            

Download.

To create a new page, use the document.Pages.Add (media_box) method. Pages.Add() takes an optional Rect argument that can be used to specify page size. This Rect is called a media box.

The media box is a rectangle, represented in default user space units, that defines the boundaries of the physical medium on which the page will be displayed or printed. The user space unit is 1/72 inch; if media_box is not specified, the default dimensions of the page are 8.5 x 11 inches (or 8.5*72, 11*72 units).


Split PDF pages:

To split a PDF document into multiple pages.

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		// This property is necessary only for licensed version.
		//SautinSoft.Pdf.Serial = "XXXXXXXXXXX";

		int page_num = doc.GetPageCount();
		for (int i = 1; i <= page_num; ++i)
		{
			var new_document = new PdfDocument();
			new_document.InsertPages(0, document, i, i, PdfDocument.InsertFlag.e_none);
			new_document.Save(output_filename + "_split_page_" + i + ".pdf", SaveOptions.e_linearized);
		}
	}
}

            

Download.


Merge, copy, delete and rearrange PDF pages in C# and VB.NET:

Sample C# code for using PDF Focus .Net to copy pages from one document to another, delete and rearrange pages, and use ImportPages() method for very efficient copy and merge operations

Merge several PDF documents into one

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		// This property is necessary only for licensed version.
		//SautinSoft.Pdf.Serial = "XXXXXXXXXXX";

		new_doc.InitSecurityHandler();
		int page_num = 15;
		for (int i = 1; i <= page_num; ++i)
		{
			Console.WriteLine("Opening newsletter_split_page_" + i + ".pdf");
			using (PdfDocument in_doc = new PdfDocument(output_path + "newsletter_split_page_" + i + ".pdf"))
			{
				new_doc.InsertPages(i, in_doc, 1, in_doc.GetPageCount(), PdfDocument.InsertFlag.e_none);
			}
		}
		new_doc.Save(output_path + "newsletter_merge_pages.pdf", SaveOptions.e_remove_unused);
	}
}

            

Download.

Delete every second page

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		// This property is necessary only for licensed version.
		//SautinSoft.Pdf.Serial = "XXXXXXXXXXX";

	using (PdfDocument in_doc = new PdfDocument(input_path + "newsletter.pdf")))
	{
		in_doc.InitSecurityHandler();
		int page_num = in_doc.GetPageCount();
		PageIterator itr;
		while (page_num>=1)
		{
			itr = in_doc.GetPageIterator(page_num);
			in_doc.PageRemove(itr);
			page_num -= 2;
		}

		in_doc.Save(output_path + "newsletter_page_remove.pdf", 0);
	}
}

            

Download.

Inserting a page from one document into a different position in another document

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		// This property is necessary only for licensed version.
		//SautinSoft.Pdf.Serial = "XXXXXXXXXXX";

		{
			using (PdfDocument in1_doc = new PdfDocument(input_path + "newsletter.pdf"))
			using (PdfDocument in2_doc = new PdfDocument(input_path + "fish.pdf"))
			{
				in1_doc.InitSecurityHandler();
				in2_doc.InitSecurityHandler();

				Page src_page = in2_doc.GetPage(1);
				PageIterator dst_page = in1_doc.GetPageIterator(1);
				int page_num = 1;
				while (dst_page.HasNext()) {
					if (page_num++ % 3 == 0) {
						in1_doc.PageInsert(dst_page, src_page);
					}
					dst_page.Next();
				}

				in1_doc.Save(output_path + "newsletter_page_insert.pdf", 0);
			}
		}
	}
}

            

Download.

Replicate pages within a single document

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		// This property is necessary only for licensed version.
		//SautinSoft.Pdf.Serial = "XXXXXXXXXXX";

		{
			using (PdfDocument doc = new PdfDocument(input_path + "newsletter.pdf"))
			{
				doc.InitSecurityHandler();

				// Replicate the cover page three times (copy page #1 and place it before the
				// seventh page in the document page sequence)
				Page cover = doc.GetPage(1);
				PageIterator p7 = doc.GetPageIterator(7);
				doc.PageInsert(p7, cover);
				doc.PageInsert(p7, cover);
				doc.PageInsert(p7, cover);

				// Replicate the cover page two more times by placing it before and after
				// existing pages.
				doc.PagePushFront(cover);
				doc.PagePushBack(cover);

				doc.Save(output_path + "newsletter_page_clone.pdf", 0);
				Console.WriteLine("Done. Result saved in newsletter_page_clone.pdf...");
			}
    	}
	}
}

            

Download.

Use ImportPages() in order to copy multiple pages at once in order to preserve shared resources between pages (e.g. images, fonts, colorspaces, etc.)

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		// This property is necessary only for licensed version.
		//SautinSoft.Pdf.Serial = "XXXXXXXXXXX";

		{
			using (PdfDocument in_doc = new PdfDocument(input_path + "newsletter.pdf"))
			{
				in_doc.InitSecurityHandler();
				using (PdfDocument new_doc = new PdfDocument())
				{
					ArrayList copy_pages = new ArrayList();
					for (PageIterator itr = in_doc.GetPageIterator(); itr.HasNext(); itr.Next()) {
						copy_pages.Add(itr.Current());
					}

					ArrayList imported_pages = new_doc.ImportPages(copy_pages);
					for (int i=0; i!=imported_pages.Count; ++i) {
						new_doc.PagePushFront((Page)imported_pages[i]); // Order pages in reverse order.
						// Use PagePushBack() if you would like to preserve the same order.
					}

					new_doc.Save(output_path + "newsletter_import_pages.pdf", 0);
					Console.WriteLine("Done. Result saved in newsletter_import_pages.pdf...");

				}
			}
		}
	}
}

            

Download.


Replicate and reorder pages:

Reverse the order of pages and rearrange pages in a PDF document. Given the page copy and delete operations described in other sections, rearranging and reordering pages is straightforward.

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		// This property is necessary only for licensed version.
		//SautinSoft.Pdf.Serial = "XXXXXXXXXXX";

		PdfDocument doc = new PdfDocument(filename);

		int page_num = doc.GetPageCount();
		for (int i=1; i<=page_num; ++i)
		{
			PageIterator itr = doc.GetPageIterator(i);
			Page page = itr.Current();
			doc.PageRemove(itr);
			doc.PagePushFront(page);
		}
	}
}

            

Download.

Remove existing pages:

To remove pages from a PDF document.

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		// This property is necessary only for licensed version.
		//SautinSoft.Pdf.Serial = "XXXXXXXXXXX";

		PdfDocument doc = new PdfDocument(filename);

		// Remove the fifth page from the page sequence.
		doc.PageRemove(doc.GetPageIterator(5));

		// Remove the third page.
		PageIterator i = doc.GetPageIterator();
		i.Next();
		i.Next();
		doc.PageRemove(i);
	}
}

            

Download.

Cropping PDF pages:

To crop a page in a PDF document.

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		// This property is necessary only for licensed version.
		//SautinSoft.Pdf.Serial = "XXXXXXXXXXX";

		PdfDocument doc = new PdfDocument(filename);

		// Access a PDF page
		Page page = doc.GetPage(page_num);

		// Crop the page
		page.SetCropBox(new Rect(0, 0, 500, 600));
	}
}

            

Download.

The crop box defines the region to which the contents of the page are to be clipped (cropped) when displayed or printed. Unlike other types of boxes, the crop box has no defined meaning in terms of physical page geometry or intended use; it merely imposes clipping on the page contents. The default value is the page's media box. A new crop box can be imposed on a page with Page.SetCropBox().

Rotate pages:

To rotate a page in a PDF document.

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		// This property is necessary only for licensed version.
		//SautinSoft.Pdf.Serial = "XXXXXXXXXXX";

		PdfDocument doc = new PdfDocument(filename);

		// Rotate the first page 90 degrees clockwise.
		Page page = doc.GetPage(1);
		Page.Rotate originalRotation = page.GetRotation();
		Page.Rotate rotation;
		switch (originalRotation)
		{
			case Page.Rotate.e_0: rotation = Page.Rotate.e_90; break;
			case Page.Rotate.e_90: rotation = Page.Rotate.e_180; break;
			case Page.Rotate.e_180: rotation = Page.Rotate.e_270; break;
			case Page.Rotate.e_270: rotation = Page.Rotate.e_0; break;
			default: rotation = Page.Rotate.e_0; break;
		}
		page.SetRotation(rotation);
	}
}

            

Download.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.