Recently, I was working on a project. In the project I would like to merge bookmarks of two or more PDF Files by iTextSharp. So I tried looking at tutorials around the web. But I can't find a good solution to this. This article will explain how to merge bookmarks of two or more PDF Files by iTextSharp. To do that here we use the ITextSharp library to create PDF documents. It provides all of the primitive functions necessary to create a PDF document.
This article is for merging of bookmarks of two or more PDF files where each file contains bookmarks.
For example PDF file A that contains some bookmark and file B that contains other bookmarks.
I will merge two PDF files using iTextSharp. After merging the two files A & B then the new File C will be created where all bookmarks will be in File C.
File A bookmark:
File B bookmark:
After merging File A and File B then File C contains the bookmark.
With this code section you can get a PDF file's bookmarks:
- iTextSharp.text.Document document = new iTextSharp.text.Document();
- MemoryStream output = new MemoryStream();
- PdfWriter writer = PdfWriter.GetInstance(document, output);
- writer.PageEvent = new FilePageEvent();
-
- document.Open();
- PdfContentByte content = writer.DirectContent;
- int counter = 0;
- int fileCount = 0;
- int pageno = 0;
-
- foreach (FileInformation file in sourceFiles)
- {
- filesByte.Add(File.ReadAllBytes(file.FilePath));
- }
- PdfOutline objRootOutLine;
- List<PDFFIleManager> FileBookmarkInfo;
- objRootOutLine = content.RootOutline;
- PdfOutline objfolderoutline = content.RootOutline;
- string folderName = string.Empty;
- for (int fileCounter = 0; fileCounter < filesByte.Count; fileCounter++)
- {
-
- PdfReader reader = new PdfReader(filesByte[fileCounter]);
- int numberOfPages = reader.NumberOfPages;
- IList<Dictionary<string, object>> book_mark = SimpleBookmark.GetBookmark(reader);
- }
- By this code section you can merge PDF file bookmarks
-
-
- if (!object.Equals(book_mark, null))
- {
- int index = 0;
- int j = 0;
- foreach (Dictionary<string, object> bk in book_mark)
- {
- int i = 0;
- IList<Dictionary<string, object>> kids = null;
- IList<Dictionary<string, object>> list = null;
- string Title = string.Empty;
- int page = 0;
- foreach (string key in bk.Keys)
- {
- if (key.Equals("Title"))
- {
- Title = Convert.ToString(bk[key]);
- i = i + 1;
- CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
- TextInfo textInfo = cultureInfo.TextInfo;
- Title = textInfo.ToTitleCase(Title);
-
- }
- if (key.Equals("Kids"))
- {
- kids = (IList<Dictionary<string, object>>)bk[key];
- }
- if (key.Equals("Page"))
- {
-
- string[] words = Convert.ToString(bk[key]).Split(' ');
- if (!object.Equals(words, null))
- {
- page = Convert.ToInt32(words[0]);
- }
- }
- }
- if (!string.IsNullOrEmpty(Title))
- {
-
- PdfAction obja3lvl = PdfAction.GotoLocalPage(counter + page -1, new iTextSharp.text.pdf.PdfDestination(iTextSharp.text.pdf.PdfDestination.FITH), writer);
- PdfOutline objoutline3lvl = new PdfOutline(objoutline, obja3lvl, Title.ToString(), true);
-
- if (!object.Equals(kids, null))
- {
-
- foreach (Dictionary<string, object> innerkids in kids)
- {
- IList<Dictionary<string, object>> kids2nd = null;
- string Title2nd = string.Empty;
- int page2 = 0;
- foreach (string key in innerkids.Keys)
- {
- if (key.Equals("Title"))
- {
- Title2nd = Convert.ToString(innerkids[key]);
- i = i + 1;
- CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
- TextInfo textInfo = cultureInfo.TextInfo;
- Title2nd = textInfo.ToTitleCase(Title2nd);
- }
-
- if (key.Equals("Kids"))
- {
- kids2nd = (IList<Dictionary<string, object>>)innerkids[key];
- }
- if (key.Equals("Page"))
- {
- string[] words = Convert.ToString(innerkids[key]).Split(' ');
-
- if (!object.Equals(words, null))
- {
- page2 = Convert.ToInt32(words[0]);
- }
- }
- }
-
- if (!string.IsNullOrEmpty(Title2nd))
- {
- PdfAction obja4lvl = PdfAction.GotoLocalPage(counter + page2 - 1, new iTextSharp.text.pdf.PdfDestination(iTextSharp.text.pdf.PdfDestination.FITH), writer);
- PdfOutline objoutline4lvl = new PdfOutline(objoutline3lvl, obja4lvl, Title2nd.ToString(), true);
-
- if (!object.Equals(kids2nd, null))
- {
-
- foreach (Dictionary<string, object> innerkids3rd in kids2nd)
- {
- IList<Dictionary<string, object>> kids3rd = null;
- string Title3rd = string.Empty;
- int page3 = 0;
- foreach (string key in innerkids3rd.Keys)
- {
- if (key.Equals("Title"))
- {
- Title3rd = Convert.ToString(innerkids3rd[key]);
- i = i + 1;
- CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
- TextInfo textInfo = cultureInfo.TextInfo;
- Title3rd = textInfo.ToTitleCase(Title3rd);
- }
- if (key.Equals("Kids"))
- {
- kids3rd = (IList<Dictionary<string, object>>)innerkids3rd[key];
- }
- if (key.Equals("Page"))
- {
-
- string[] words = Convert.ToString(innerkids3rd[key]).Split(' ');
- if (!object.Equals(words, null))
- {
- page3 = Convert.ToInt32(words[0]);
- }
- }
- }
- if (!string.IsNullOrEmpty(Title2nd))
- {
- PdfAction obja5lvl = PdfAction.GotoLocalPage(counter + page3 - 1, new iTextSharp.text.pdf.PdfDestination(iTextSharp.text.pdf.PdfDestination.FITH), writer);
- PdfOutline objoutline5lvl = new PdfOutline(objoutline4lvl, obja5lvl, Title3rd.ToString(), true);
- }
- }
- }
- }
- }
- }
- }
- }
-
-
The MergePDF Method that accepts a collection of PDF files that have Bookmarks where you can merge 3 levels of bookmarks.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace Common.Interface
- {
-
-
-
- public interface IFileInformation
- {
- #region Properties
-
-
-
- string FileID
- {
- get;
- set;
- }
-
-
-
- string FileName
- {
- get;
- set;
- }
-
-
-
- string FilePath
- {
- get;
- set;
- }
-
-
-
- string FolderName
- {
- get;
- set;
- }
-
-
-
- bool IsDeleted
- {
- get;
- set;
- }
-
-
-
- string FileBookMark
- {
- get;
- set;
- }
- #endregion
- }
- }
-
-
-
-
-
-
- public byte[] MergePDF(List<IFileInformation> sourceFiles)
- {
- if (sourceFiles.Count == 0)
- {
- throw new ArgumentNullException("File list");
- }
- iTextSharp.text.Document document = new iTextSharp.text.Document();
- MemoryStream output = new MemoryStream();
- List<byte[]> filesByte = new List<byte[]>();
- bool checkCoverBookmark = true;
- try
- {
-
- PdfWriter writer = PdfWriter.GetInstance(document, output);
- writer.PageEvent = new FilePageEvent();
-
- document.Open();
- PdfContentByte content = writer.DirectContent;
- int counter = 0;
- int fileCount = 0;
- int pageno = 0;
-
- foreach (FileInformation file in sourceFiles)
- {
- filesByte.Add(File.ReadAllBytes(file.FilePath));
- }
- PdfOutline objRootOutLine;
- List<PDFFIleManager> FileBookmarkInfo;
- objRootOutLine = content.RootOutline;
- PdfOutline objfolderoutline = content.RootOutline;
- string folderName = string.Empty;
- for (int fileCounter = 0; fileCounter < filesByte.Count; fileCounter++)
- {
-
- PdfReader reader = new PdfReader(filesByte[fileCounter]);
- int numberOfPages = reader.NumberOfPages;
- IList<Dictionary<string, object>> book_mark = SimpleBookmark.GetBookmark(reader);
-
- if (!object.Equals(book_mark, null))
- {
- IList<Dictionary<string, object>> list = IterateBookmark(book_mark);
- }
- #region bookmarking of member and decision vote
-
- for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
- {
- counter = counter + 1;
- pageno = pageno + 1;
-
- document.SetPageSize(reader.GetPageSizeWithRotation(currentPageIndex));
-
- document.NewPage();
- if (currentPageIndex == 1)
- {
- fileCount = fileCount + 1;
- string fileName = (from l in sourceFiles where l.FileID == fileCount.ToString() select l.FileName).FirstOrDefault();
- string folderName1 = (from l in sourceFiles where l.FileID == fileCount.ToString() select l.FolderName).FirstOrDefault();
- string bookmarkIndividualName = (from l in sourceFiles where l.FileID == fileCount.ToString() select l.FileBookMark).FirstOrDefault();
- if (folderName != folderName1)
- {
- PdfAction objFolderAction = PdfAction.GotoLocalPage(counter, new iTextSharp.text.pdf.PdfDestination(iTextSharp.text.pdf.PdfDestination.FITH), writer);
- objfolderoutline = new PdfOutline(objRootOutLine, objFolderAction, folderName1.ToUpper(), true);
- folderName = folderName1;
- }
-
- PdfAction objaction = PdfAction.GotoLocalPage(counter, new iTextSharp.text.pdf.PdfDestination(iTextSharp.text.pdf.PdfDestination.FITH), writer);
-
- if (string.IsNullOrEmpty(bookmarkIndividualName))
- bookmarkIndividualName = fileName;
- PdfOutline objoutline = new PdfOutline(objfolderoutline, objaction, bookmarkIndividualName.ToUpper(), true);
-
- if (!object.Equals(book_mark, null))
- {
- int index = 0;
- int j = 0;
- foreach (Dictionary<string, object> bk in book_mark)
- {
- int i = 0;
- IList<Dictionary<string, object>> kids = null;
- IList<Dictionary<string, object>> list = null;
- string Title = string.Empty;
- int page = 0;
- foreach (string key in bk.Keys)
- {
- if (key.Equals("Title"))
- {
- Title = Convert.ToString(bk[key]);
- i = i + 1;
- CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
- TextInfo textInfo = cultureInfo.TextInfo;
- Title = textInfo.ToTitleCase(Title);
-
- }
-
- if (key.Equals("Kids"))
- {
- kids = (IList<Dictionary<string, object>>)bk[key];
- }
- if (key.Equals("Page"))
- {
-
-
- string[] words = Convert.ToString(bk[key]).Split(' ');
- if (!object.Equals(words, null))
- {
- page = Convert.ToInt32(words[0]);
- }
- }
- }
- if (!string.IsNullOrEmpty(Title))
- {
-
- PdfAction obja3lvl = PdfAction.GotoLocalPage(counter + page -1, new iTextSharp.text.pdf.PdfDestination(iTextSharp.text.pdf.PdfDestination.FITH), writer);
- PdfOutline objoutline3lvl = new PdfOutline(objoutline, obja3lvl, Title.ToString(), true);
- if (!object.Equals(kids, null))
- {
-
- foreach (Dictionary<string, object> innerkids in kids)
- {
- IList<Dictionary<string, object>> kids2nd = null;
- string Title2nd = string.Empty;
- int page2 = 0;
- foreach (string key in innerkids.Keys)
- {
- if (key.Equals("Title"))
- {
- Title2nd = Convert.ToString(innerkids[key]);
- i = i + 1;
- CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
- TextInfo textInfo = cultureInfo.TextInfo;
- Title2nd = textInfo.ToTitleCase(Title2nd);
- }
- if (key.Equals("Kids"))
- {
- kids2nd = (IList<Dictionary<string, object>>)innerkids[key];
- }
- if (key.Equals("Page"))
- {
- string[] words = Convert.ToString(innerkids[key]).Split(' ');
- if (!object.Equals(words, null))
- {
- page2 = Convert.ToInt32(words[0]);
- }
- }
- }
-
- if (!string.IsNullOrEmpty(Title2nd))
- {
- PdfAction obja4lvl = PdfAction.GotoLocalPage(counter + page2 - 1, new iTextSharp.text.pdf.PdfDestination(iTextSharp.text.pdf.PdfDestination.FITH), writer);
- PdfOutline objoutline4lvl = new PdfOutline(objoutline3lvl, obja4lvl, Title2nd.ToString(), true);
- if (!object.Equals(kids2nd, null))
- {
-
- foreach (Dictionary<string, object> innerkids3rd in kids2nd)
- {
-
- IList<Dictionary<string, object>> kids3rd = null;
- string Title3rd = string.Empty;
- int page3 = 0;
- foreach (string key in innerkids3rd.Keys)
- {
- if (key.Equals("Title"))
- {
- Title3rd = Convert.ToString(innerkids3rd[key]);
- i = i + 1;
- CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
- TextInfo textInfo = cultureInfo.TextInfo;
- Title3rd = textInfo.ToTitleCase(Title3rd);
- }
-
- if (key.Equals("Kids"))
- {
- kids3rd = (IList<Dictionary<string, object>>)innerkids3rd[key];
- }
- if (key.Equals("Page"))
- {
-
-
- string[] words = Convert.ToString(innerkids3rd[key]).Split(' ');
- if (!object.Equals(words, null))
- {
- page3 = Convert.ToInt32(words[0]);
- }
- }
- }
-
- if (!string.IsNullOrEmpty(Title2nd))
- {
- PdfAction obja5lvl = PdfAction.GotoLocalPage(counter + page3 - 1, new iTextSharp.text.pdf.PdfDestination(iTextSharp.text.pdf.PdfDestination.FITH), writer);
- PdfOutline objoutline5lvl = new PdfOutline(objoutline4lvl, obja5lvl, Title3rd.ToString(), true);
- }
- }
- }
- }
- }
- }
- }
- }
-
-
- }
-
- PdfImportedPage importedPage = writer.GetImportedPage(reader, currentPageIndex);
-
- int pageOrientation = reader.GetPageRotation(currentPageIndex);
- if ((pageOrientation == 90) || (pageOrientation == 270))
- {
- content.AddTemplate(importedPage, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(currentPageIndex).Height);
- }
- else
- {
- content.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
- }
- }
- #endregion
-
- }
- }
- catch (IOException ex)
- {
- throw new IOException(ex.Message);
- }
- catch (OutOfMemoryException ex)
- {
- throw new OutOfMemoryException(ex.Message);
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- finally
- {
- document.Close();
- }
- return output.GetBuffer();
- }