RubyPDF Blog .NET,English,iText in Action,iTextSharp(iText#),PDF Burst.cs–split a PDF in several separate PDF files (1 per page)

Burst.cs–split a PDF in several separate PDF files (1 per page)

Update:

  • use PdfSmartCopy and simplify the job.
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace chapter01
{
	/// 
	/// This example was written by Steven Lee.
	/// https://blog.rubypdf.com
	/// This tool lets you split a PDF in several separate PDF files (1 per page).
	/// 
	public class Burst
	{
		/// 
		/// split a PDF in several separate PDF files (1 per page)
		/// 
		public static void Main(string[] args) 
		{

			try 
			{
				FileInfo file=new FileInfo(args[0]);
				string name =file.Name.Substring(0,file.Name.LastIndexOf("."));				
				// we create a reader for a certain document
				PdfReader reader = new PdfReader(args[0]);
				// we retrieve the total number of pages
				int n=reader.NumberOfPages;
				int digits = 1 + (n / 10);
				System.Console.WriteLine("There are " + n + " pages in the original file.");
				Document document;
				int pagenumber;
				string filename;
                                PdfSmartCopy copy;

				for (int i = 0; i < n; i++) 
				{
					pagenumber = i + 1;
					filename =pagenumber.ToString();
					while (filename.Length< digits) filename = "0" + filename;
					filename = "_" + filename + ".pdf";
					// step 1: creation of a document-object
					document = new Document(reader.GetPageSizeWithRotation(pagenumber));
					// step 2: we create a writer that listens to the document
					copy = new PdfSmartCopy(document, new FileStream(name+filename,FileMode.Create));
					// step 3: we open the document
					document.Open();
                                        copy.AddPage(copy.GetImportedPage(reader, i));
					// step 5: we close the document
					document.Close();
				}
			} 
			catch (DocumentException de) 
			{
				System.Console.Error.WriteLine(de.Message);
			} 
			catch (IOException ioe) 
			{
				System.Console.Error.WriteLine(ioe.Message);
			}
		}
	}
}

the following source code is too old, so just ignore it, otherwise you are using the odd library of iTextSharp.

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace chapter01
{
	/// 
	/// This example was written by Steven Lee.
	/// https://blog.rubypdf.com
	/// This tool lets you split a PDF in several separate PDF files (1 per page).
	/// 
	public class Burst
	{
		/// 
		/// split a PDF in several separate PDF files (1 per page)
		/// 
		public static void Main(string[] args) 
		{

			try 
			{
				FileInfo file=new FileInfo(args[0]);
				string name =file.Name.Substring(0,file.Name.LastIndexOf("."));				
				// we create a reader for a certain document
				PdfReader reader = new PdfReader(args[0]);
				// we retrieve the total number of pages
				int n=reader.NumberOfPages;
				int digits = 1 + (n / 10);
				System.Console.WriteLine("There are " + n + " pages in the original file.");
				Document document;
				int pagenumber;
				string filename;

				for (int i = 0; i < n; i++) 
				{
					pagenumber = i + 1;
					filename =pagenumber.ToString();
					while (filename.Length< digits) filename = "0" + filename;
					filename = "_" + filename + ".pdf";
					// step 1: creation of a document-object
					document = new Document(reader.GetPageSizeWithRotation(pagenumber));
					// step 2: we create a writer that listens to the document
					PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(name+filename,FileMode.Create));
					// step 3: we open the document
					document.Open();
					PdfContentByte cb = writer.DirectContent;
					PdfImportedPage page = writer.GetImportedPage(reader, pagenumber);
					int rotation = reader.GetPageRotation(pagenumber);
					if (rotation == 90 || rotation == 270) 
					{
						cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pagenumber).Height);
					}
					else 
					{
						cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
					}
					// step 5: we close the document
					document.Close();
				}
			} 
			catch (DocumentException de) 
			{
				System.Console.Error.WriteLine(de.Message);
			} 
			catch (IOException ioe) 
			{
				System.Console.Error.WriteLine(ioe.Message);
			}
		}
	}
}

4 thoughts on “Burst.cs–split a PDF in several separate PDF files (1 per page)”

  1. Dear Team,

    I can split the PDF successfully with your code. At the same time, i findone issue. If the Main PDF file contains any links(Hyperlink to new site), the splitted PDF does not the have the hyperlink. Hyerlinks are removed in the splitted PDF.

    I want to stay the hyperlink in the pdf also.

    Please help me to resolver this error.

  2. To the author of this example: please remove this page from your site because it’s totally wrong.

    To the readers of this example: please read the following questions and answers to understand why it’s totally wrong:

    http://stackoverflow.com/questions/15290361/how-to-keep-original-rotate-page-in-itextsharp-dll/
    http://stackoverflow.com/questions/15309651/itextsharp-unexpected-elements-on-copied-pages/

    Examples that mislead iTextSharp users cost plenty of time to the people who use these examples as well as to the people helping out people who are misled.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.