private string[] SplitPdf(string url, string splittedFilesDirectoryName, string prefixOfFileName) { PdfReader reader = LibPDFTools.CreatePdfReader(url, QueryPassword, false); try { reader.ConsolidateNamedDestinations(); IList <Dictionary <string, object> > bookmarks = SimpleBookmark.GetBookmark(reader); int numberOfPages = reader.NumberOfPages; string[] splittedFiles = new string[numberOfPages]; Directory.CreateDirectory(splittedFilesDirectoryName); for (int i = 1; i <= numberOfPages; i++) { string splittedFile = Path.Combine(splittedFilesDirectoryName, prefixOfFileName + i.ToString("D10") + ".pdf"); splittedFiles[i - 1] = splittedFile; //extract bookmarks on the page IList <Dictionary <string, object> > bookmark = null; if (bookmarks != null) { bookmark = new List <Dictionary <string, object> >(bookmarks); int[] rs; rs = new int[4]; rs[0] = 1; rs[1] = i - 1; rs[2] = i + 1; rs[3] = numberOfPages; SimpleBookmark.EliminatePages(bookmark, rs); rs = new int[2]; rs[0] = rs[1] = i; SimpleBookmark.ShiftPageNumbers(bookmark, 1 - i, rs); } using (var document = new Document(reader.GetPageSizeWithRotation(i))) { using (var outStream = new FileStream(splittedFile, FileMode.Create, FileAccess.Write)) { using (var writer = new PdfCopy(document, outStream)) { document.Open(); PdfImportedPage page = writer.GetImportedPage(reader, i); writer.AddPage(page); if (bookmark != null) { writer.Outlines = bookmark; } writer.Close(); } outStream.Close(); } document.Close(); } } return(splittedFiles); } finally { reader.Close(); } }
private void AppendMain(PdfReader reader, string title, PageRange[] pageRanges, PdfConcatenatorOption option) { bool addOutlines = option.AddOutlines; bool copyOutlines = option.CopyOutlines; int i; bool[] appendOrNots = new bool[reader.NumberOfPages + 1]; foreach (var pr in pageRanges) { for (int j = pr.StartPage; j <= pr.EndPage; j++) { appendOrNots[j] = true; } } int theFirstPage = 0; for (i = 1; i <= reader.NumberOfPages; i++) { if (appendOrNots[i]) { theFirstPage = i; break; } } if (theFirstPage == 0) { // There is no append paes. return; } if (!opened) { opened = true; document = new Document(reader.GetPageSizeWithRotation(theFirstPage)); writer = new PdfCopy(document, outStream); writer.SetMergeFields(); if (ownerPassword != null) { byte[] bytesUserPassword = userPassword == null ? null : Encoding.ASCII.GetBytes(userPassword); writer.SetEncryption(bytesUserPassword, Encoding.ASCII.GetBytes(ownerPassword), permissions, encryptionStrength); } writer.ViewerPreferences = this.viewerPreference; document.Open(); } if (bookmarks == null) { if (addOutlines || copyOutlines) { bookmarks = new List <Dictionary <string, object> >(); } } if (bookmarks != null) { Dictionary <string, object> m = null; if (addOutlines) { m = new Dictionary <string, object>(); m["Title"] = title; m["Action"] = "GoTo"; m["Page"] = currPageNum.ToString() + " " + option.FittingStyle; } if (copyOutlines) { var cpbookmarks = SimpleBookmark.GetBookmark(reader); if (cpbookmarks != null) { int[] elimPages = new int[2]; int[] shiftPages = new int[2]; shiftPages[1] = reader.NumberOfPages; for (int pageIndex = reader.NumberOfPages; pageIndex > 0; --pageIndex) { if (!appendOrNots[pageIndex]) { elimPages[0] = elimPages[1] = pageIndex; shiftPages[0] = pageIndex + 1; SimpleBookmark.EliminatePages(cpbookmarks, elimPages); SimpleBookmark.ShiftPageNumbers(cpbookmarks, -1, shiftPages); } } SimpleBookmark.ShiftPageNumbers(cpbookmarks, currPageNum - 1, null); if (m == null) { foreach (var c in cpbookmarks) { bookmarks.Add(c); } } else { m["Kids"] = cpbookmarks; } } } if (m != null) { bookmarks.Add(m); } } { var pages = new List <int>(); for (int ii = 1; ii < appendOrNots.Length; ii++) { if (appendOrNots[ii]) { pages.Add(ii); currPageNum++; } } reader.SelectPages(pages); writer.AddDocument(reader); } }