private void init() { _document = new Document(); _writer = new PdfSmartCopy(_document, OutputFileStream); _writer.SetFullCompression(); _document.Open(); }
//======================================== // // //======================================== private void ExtractAndSaveFile(string sourcePdfFilePath, string outpuPdfFileName, int startPage, int numberOfPages) { using (PdfReader reader = new PdfReader(sourcePdfFilePath)) { Document document = new Document(); PdfSmartCopy partial = new PdfSmartCopy(document, new FileStream(outpuPdfFileName, FileMode.Create)); partial.SetPdfVersion(PdfWriter.PDF_VERSION_1_5); partial.CompressionLevel = PdfStream.BEST_COMPRESSION; partial.SetFullCompression(); document.Open(); for (int pagenumber = startPage; pagenumber < (startPage + numberOfPages); pagenumber++) { if (reader.NumberOfPages >= pagenumber) { partial.AddPage(partial.GetImportedPage(reader, pagenumber)); } else { break; } } document.Close(); } }
public static void ExtractPagePerSize(string sourcePdfPath, string outputPdfPath, int pageNumber, decimal maxFileSize) { PdfReader reader = null; FileInfo info = new FileInfo(sourcePdfPath); Dictionary <int, Tuple <Document, FileStream, PdfSmartCopy> > docs = new Dictionary <int, Tuple <Document, FileStream, PdfSmartCopy> >(); try { // Intialize a new PdfReader instance with the contents of the source Pdf file: reader = new PdfReader(sourcePdfPath); int docIndex = 0; int currentPage = 0; for (int i = 0; i < reader.NumberOfPages; i++) { currentPage = i + 1; Document document; FileStream fs; PdfSmartCopy pdfCopyProvider; if (docs.Count <= 0) { document = new Document(reader.GetPageSizeWithRotation(currentPage)); fs = new FileStream(Path.Combine(outputPdfPath, info.Name.Replace(info.Extension, "") + "_" + docIndex + ".pdf"), System.IO.FileMode.Create); pdfCopyProvider = new PdfSmartCopy(document, fs); pdfCopyProvider.SetFullCompression(); document.Open(); docs.Add(docIndex, new Tuple <Document, FileStream, PdfSmartCopy>(document, fs, pdfCopyProvider)); } else { decimal overhead = maxFileSize * 0.20m; if (docs[docIndex].Item2.Length > ((maxFileSize - overhead) * 1024 * 1024)) { docIndex++; document = new Document(reader.GetPageSizeWithRotation(currentPage)); fs = new FileStream(Path.Combine(outputPdfPath, info.Name.Replace(info.Extension, "") + "_" + docIndex + ".pdf"), System.IO.FileMode.Create); pdfCopyProvider = new PdfSmartCopy(document, fs); pdfCopyProvider.SetFullCompression(); document.Open(); docs.Add(docIndex, new Tuple <Document, FileStream, PdfSmartCopy>(document, fs, pdfCopyProvider)); } else { pdfCopyProvider = docs[docIndex].Item3; } } PdfImportedPage importedPage = pdfCopyProvider.GetImportedPage(reader, currentPage); pdfCopyProvider.AddPage(importedPage); //if (docs[docIndex].Item2.Length > 2000000) //{ //} } // Capture the correct size and orientation for the page: // Initialize an instance of the PdfCopyClass with the source // document and an output file stream: // Extract the desired page number: foreach (Tuple <Document, FileStream, PdfSmartCopy> i in docs.Values) { i.Item1.Close(); i.Item2.Close(); i.Item3.Close(); } reader.Close(); } catch (Exception ex) { throw ex; } }
public static void ExtractPagePerPage(string sourcePdfPath, string outputPdfPath, int pages) { PdfReader reader = null; FileInfo info = new FileInfo(sourcePdfPath); Dictionary <int, Tuple <Document, FileStream, PdfSmartCopy> > docs = new Dictionary <int, Tuple <Document, FileStream, PdfSmartCopy> >(); try { // Intialize a new PdfReader instance with the contents of the source Pdf file: reader = new PdfReader(sourcePdfPath); int docIndex = 1; int currentPage = 0; int pageCounter = 0; for (int i = 0; i < reader.NumberOfPages; i++) { currentPage = i + 1; Document document; FileStream fs; PdfSmartCopy pdfCopyProvider; if (docs.Count <= 0) { document = new Document(reader.GetPageSizeWithRotation(currentPage)); string fileName = GetTextFromPDF(reader, docIndex); fs = new FileStream(Path.Combine(outputPdfPath, fileName + ".pdf"), System.IO.FileMode.Create); pdfCopyProvider = new PdfSmartCopy(document, fs); pdfCopyProvider.SetFullCompression(); document.Open(); docs.Add(docIndex, new Tuple <Document, FileStream, PdfSmartCopy>(document, fs, pdfCopyProvider)); pageCounter = 1; } else { if (pageCounter >= pages) { docIndex++; document = new Document(reader.GetPageSizeWithRotation(currentPage)); string fileName = GetTextFromPDF(reader, docIndex); string destinationPath = Path.Combine(outputPdfPath, fileName + ".pdf"); if (!File.Exists(destinationPath)) { fs = new FileStream(destinationPath, System.IO.FileMode.Create); pdfCopyProvider = new PdfSmartCopy(document, fs); pdfCopyProvider.SetFullCompression(); document.Open(); docs.Add(docIndex, new Tuple <Document, FileStream, PdfSmartCopy>(document, fs, pdfCopyProvider)); } else { pdfCopyProvider = null; } pageCounter = 1; } else { pdfCopyProvider = docs[docIndex].Item3; pageCounter++; } } if (pdfCopyProvider != null) { PdfImportedPage importedPage = pdfCopyProvider.GetImportedPage(reader, currentPage); pdfCopyProvider.AddPage(importedPage); } } foreach (Tuple <Document, FileStream, PdfSmartCopy> i in docs.Values) { i.Item1.Close(); i.Item2.Close(); i.Item3.Close(); } reader.Close(); } catch (Exception ex) { throw ex; } }