public static void Run() { // ExStart:ExtractPagesBetweenNumbers // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdfFacades_Pages(); // Create PdfFileEditor object PdfFileEditor pdfEditor = new PdfFileEditor(); // Extract pages pdfEditor.Extract(dataDir + "MultiplePages.pdf", 1, 3, dataDir + "ExtractPagesBetweenNumbers_out.pdf"); // ExEnd:ExtractPagesBetweenNumbers }
public static void Run() { // ExStart:ExtractArrayOfPages // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdfFacades_Pages(); // Create PdfFileEditor object PdfFileEditor pdfEditor = new PdfFileEditor(); int[] pagesToExtract = new int[] { 1, 2 }; // Extract pages pdfEditor.Extract(dataDir + "Extract.pdf", pagesToExtract, dataDir + "ExtractArrayOfPages_out.pdf"); // ExEnd:ExtractArrayOfPages }
public static void Run() { // ExStart:ExtractPagesBetweenTwoNumbersUsingStreams // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdfFacades_Pages(); // Create PdfFileEditor object PdfFileEditor pdfEditor = new PdfFileEditor(); // Create streams FileStream inputStream = new FileStream(dataDir + "MultiplePages.pdf", FileMode.Open); FileStream outputStream = new FileStream(dataDir + "ExtractPagesBetweenTwoNumbers_out.pdf", FileMode.Create); // Extract pages pdfEditor.Extract(inputStream, 1, 3, outputStream); // ExEnd:ExtractPagesBetweenTwoNumbersUsingStreams }
public static void Run() { // ExStart:ExtractArrayOfPagesUsingStreams // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdfFacades_Pages(); // Create PdfFileEditor object PdfFileEditor pdfEditor = new PdfFileEditor(); // Create streams FileStream inputStream = new FileStream(dataDir + "MultiplePages.pdf", FileMode.Open); FileStream outputStream = new FileStream(dataDir + "ExtractArrayOfPagesUsingStreams_out_.pdf", FileMode.Create); int[] pagesToExtract = new int[] { 1, 2 }; // Extract pages pdfEditor.Extract(inputStream, pagesToExtract, outputStream); // ExEnd:ExtractArrayOfPagesUsingStreams }
public MemoryStream ConvertToPdf(DocumentInfo file) { var fileName = file.FileName; var fileType = _fileTypeDetector.Detect(new MemoryStream(file.Body)); var streamToConvert = new MemoryStream(file.Body) {Position = 0}; var resultStream = new MemoryStream(); switch (fileType) { case EnumFileType.Word: case EnumFileType.Word2007: try { var word2007Doc = new Document(streamToConvert); word2007Doc.Unprotect(); word2007Doc.Save(resultStream, WordSaveFormat.Pdf); } catch (FileNotFoundException e) { _log.ErrorFormat("Errore nella conversione di un file - {0} - fileName:{1}", e, Library.Utility.GetMethodDescription(), fileName); } break; case EnumFileType.Excel: case EnumFileType.Excel2007: var cells = new Workbook(streamToConvert); try { cells.Save(resultStream, CellsSaveFormat.Pdf); } catch (CellsException e) { _log.ErrorFormat("Errore nella conversione di un file - {0} - fileName:{1}", e, Library.Utility.GetMethodDescription(), fileName); } catch (FileNotFoundException e) { _log.ErrorFormat("Errore nella conversione di un file - FILE NON TROVATO - {0} - fileName:{1}", e, Library.Utility.GetMethodDescription(), fileName); } catch (Exception e) { // Here we have the workaround for "Unsupported sfnt version" exception. if (resultStream.Length == 0) cells.Save(resultStream, CellsSaveFormat.Pdf); _log.ErrorFormat("Errore nella conversione di un file - WORKAROUND for 'Unsupported sfnt version' - {0} - fileName:{1}", e, Library.Utility.GetMethodDescription(), fileName); } break; case EnumFileType.PowerPoint: case EnumFileType.PowerPoint2007: try { var slides = new Presentation(streamToConvert, new SlidesLoadOptions { LoadFormat = SlidesLoadFormat.Ppt }); slides.Save(resultStream, SlidesSaveFormat.Pdf); } catch (PptException e) { _log.ErrorFormat("Errore nella conversione di un file - {0} - fileName:{1}", e, Library.Utility.GetMethodDescription(), fileName); } catch (FileNotFoundException e) { _log.ErrorFormat("Errore nella conversione di un file - FILE NON TROVATO - {0} - fileName:{1}", e, Library.Utility.GetMethodDescription(), fileName); } break; case EnumFileType.Pdf: var pdf = new PdfFileInfo(streamToConvert); var numberOfPage = pdf.NumberOfPages; var pdfEditor = new PdfFileEditor(); pdfEditor.Extract(streamToConvert, 1, numberOfPage, resultStream); break; case EnumFileType.Png: imageToPdf(streamToConvert, resultStream, ImageFileType.Png); break; case EnumFileType.Bmp: imageToPdf(streamToConvert, resultStream, ImageFileType.Bmp); break; case EnumFileType.Jpg: imageToPdf(streamToConvert, resultStream, ImageFileType.Jpeg); //streamToConvert.CopyTo(resultStream); break; default: _log.ErrorFormat("Unknown file type, conversion failed - {0} - fileName:{1}", Library.Utility.GetMethodDescription(), fileName); break; } _log.InfoFormat("File converted successfully - {0} - fileName:{1}", Library.Utility.GetMethodDescription(), fileName); resultStream.Position = 0; return resultStream; }
public static void Run() { // ExStart:PdfFileEditorFeatures // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdfFacades_TechnicalArticles(); // Create instance of PdfFileEditor class PdfFileEditor pdfEditor = new PdfFileEditor(); // Append pages from input file to the port file and save in output file int start = 1; int end = 3; pdfEditor.Append(dataDir + "inFile.pdf", dataDir + "portFile.pdf", start, end, dataDir + "outFile.pdf"); // Concatenate two files and save in the third one pdfEditor.Concatenate(dataDir + "inFile1.pdf", dataDir + "inFile2.pdf", dataDir + "outFile.pdf"); // Delete specified number of pages from the file int[] pages = new int[] { 1, 2, 4, 10 }; pdfEditor.Delete(dataDir + "inFile.pdf", pages, dataDir + "outFile.pdf"); // Extract any pages from the file start = 0; end = 3; pdfEditor.OwnerPassword = "******"; pdfEditor.Extract(dataDir + "inFile.pdf", start, end, dataDir + "outFile.pdf"); // Insert pages from another file into the output file at a specified position start = 2; end = 5; pdfEditor.Insert(dataDir + "inFile.pdf", 4, dataDir + "portFile.pdf", start, end, dataDir + "outFile.pdf"); // Make booklet pdfEditor.MakeBooklet(dataDir + "inFile.Pdf", dataDir + "outFile.Pdf"); // Make N-Ups pdfEditor.MakeNUp(dataDir + "inFile.pdf", dataDir + "nupOutFile.pdf", 3, 2); // Split the front part of the file pdfEditor.SplitFromFirst(dataDir + "inFile.pdf", 3, dataDir + "outFile.pdf"); // Split the rear part of the file pdfEditor.SplitToEnd(dataDir + "inFile.pdf", 3, dataDir + "outFile.pdf"); // Split to individual pages int fileNum = 1; MemoryStream[] outBuffer = pdfEditor.SplitToPages(dataDir + "inFile.pdf"); foreach (MemoryStream aStream in outBuffer) { FileStream outStream = new FileStream("oneByone" + fileNum.ToString() + ".pdf", FileMode.Create); aStream.WriteTo(outStream); outStream.Close(); fileNum++; } // Split to several multi-page pdf documents fileNum = 1; int[][] numberofpage = new int[][] { new int[] { 1, 4 } }; MemoryStream[] outBuffer2 = pdfEditor.SplitToBulks(dataDir + "inFile.pdf", numberofpage); foreach (MemoryStream aStream in outBuffer2) { FileStream outStream = new FileStream("oneByone" + fileNum.ToString() + ".pdf", FileMode.Create); aStream.WriteTo(outStream); outStream.Close(); fileNum++; } // ExEnd:PdfFileEditorFeatures }
public static void Run() { // ExStart:PdfFileEditorFeatures // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdfFacades_TechnicalArticles(); // Create instance of PdfFileEditor class PdfFileEditor pdfEditor = new PdfFileEditor(); // Append pages from input file to the port file and save in output file int start = 1; int end = 3; pdfEditor.Append(dataDir + "inFile.pdf", dataDir + "portFile.pdf", start, end, dataDir + "outFile.pdf"); // Concatenate two files and save in the third one pdfEditor.Concatenate(dataDir + "inFile1.pdf", dataDir + "inFile2.pdf", dataDir + "outFile.pdf"); // Delete specified number of pages from the file int[] pages = new int[] { 1, 2, 4, 10 }; pdfEditor.Delete(dataDir + "inFile.pdf", pages, dataDir + "outFile.pdf"); // Extract any pages from the file start = 0; end = 3; pdfEditor.OwnerPassword = "******"; pdfEditor.Extract(dataDir + "inFile.pdf", start, end, dataDir + "outFile.pdf"); // Insert pages from another file into the output file at a specified position start = 2; end = 5; pdfEditor.Insert(dataDir + "inFile.pdf", 4, dataDir + "portFile.pdf", start, end, dataDir + "outFile.pdf"); // Make booklet pdfEditor.MakeBooklet(dataDir + "inFile.Pdf", dataDir + "outFile.Pdf"); // Make N-Ups pdfEditor.MakeNUp(dataDir + "inFile.pdf", dataDir + "nupOutFile.pdf", 3, 2); // Split the front part of the file pdfEditor.SplitFromFirst(dataDir + "inFile.pdf", 3, dataDir + "outFile.pdf"); // Split the rear part of the file pdfEditor.SplitToEnd(dataDir + "inFile.pdf", 3, dataDir + "outFile.pdf"); // Split to individual pages int fileNum = 1; MemoryStream[] outBuffer = pdfEditor.SplitToPages(dataDir + "inFile.pdf"); foreach (MemoryStream aStream in outBuffer) { FileStream outStream = new FileStream("oneByone" + fileNum.ToString() + ".pdf", FileMode.Create); aStream.WriteTo(outStream); outStream.Close(); fileNum++; } // Split to several multi-page pdf documents fileNum = 1; int[][] numberofpage = new int[][] { new int[] { 1, 4 }}; MemoryStream[] outBuffer2 = pdfEditor.SplitToBulks(dataDir + "inFile.pdf", numberofpage); foreach (MemoryStream aStream in outBuffer2) { FileStream outStream = new FileStream("oneByone" + fileNum.ToString() + ".pdf", FileMode.Create); aStream.WriteTo(outStream); outStream.Close(); fileNum++; } // ExEnd:PdfFileEditorFeatures }