iTextSharp Split on Bookmark

StuartProgramming Blog

If you've ever needed to work with PDF files in .NET the best library without a doubt is iTextSharp. For the problem below, I needed to take a PDF file that was generated by another process and split it up by the bookmarks. Each section was to become a new PDF. The solution below is translated to the iTextSharp library from the original JAVA code posted on ViralPatel's forum attributed to Goblin_Queen.

private void button1_Click(object sender, EventArgs e) {
	string filename = textBox1.Text;
	string outputFolder = textBox2.Text;
	splitPDFByBookmarks(filename, outputFolder);	
}

public static void splitPDFByBookmarks(String pdf, String outputFolder) {
	try {
		PdfReader reader = new PdfReader(pdf);
		//List of bookmarks: each bookmark is a map with values for title, page, etc
		System.Collections.Generic.IList<dictionary object="">> bookmarks = SimpleBookmark.GetBookmark(reader);
		//Loop through all fo the bookmarks
		for (int i = 0; i  bm = bookmarks[i];
			Dictionary<string object=""> nextBM = i == bookmarks.Count - 1 ? null : bookmarks[i + 1];
			//Get the title value from the bookmark
			String title = (String)bm["Title"];
			//for some reason these are stored like '1 XYZ null null null' the page is the first value of an array split by spaces
			String startPage = ((String)bm["Page"]).Split(' ')[0];
			String startPageNextBM = nextBM == null ? "" + (reader.NumberOfPages + 1) : ((String)nextBM["Page"]).Split(' ')[0];
			Console.WriteLine("Page: " + startPage);
			Console.WriteLine("------------------");
			extractBookmarkToPDF(reader, int.Parse(startPage), int.Parse(startPageNextBM), title + ".pdf", outputFolder);
		}
	}
	catch (IOException e) {
		Console.WriteLine(e.Message);
	}
}


private static void extractBookmarkToPDF(PdfReader reader, int pageFrom, int pageTo, String outputName, String outputFolder) {
	Document document = new Document();
	try {
		// Create a writer for the outputstream
		PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outputFolder + outputName, FileMode.Create));
		document.Open();
		PdfContentByte cb = writer.DirectContent; // Holds the PDF data
		PdfImportedPage page;

		while (pageFrom </string></dictionary>
Tags: 
StuartiTextSharp Split on Bookmark