Ejemplo n.º 1
0
        public PdfReader CreatePdfReader(byte[] bytes, PdfConcatenatorOption option)
        {
            PdfReader reader;

            switch (LibPDFTools.GetImageFileType(bytes))
            {
            case ImageFileType.PDF:
                reader = LibPDFTools.CreatePdfReader(bytes, QueryPassword, true);
                break;

            case ImageFileType.TIFF:
                reader = LibPDFTools.CreatePdfReaderFromTiff(bytes, option.GetBounds(), option.Margins);
                break;

            default:
                reader = LibPDFTools.CreatePdfReaderFromImage(bytes, option.GetBounds(), option.Margins);
                break;
            }
            return(reader);
        }
Ejemplo n.º 2
0
        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();
            }
        }