private void MergeExecute(object parameter) { var fi = new io::FileInfo(Model.OutputFile); if (fi.Exists) { if (MessageBox.Show("File already exists. Overwrite?", "Overwrite?", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { fi.Delete(); } else { return; } } using (var outputFile = new StreamWriter(fi.FullName, false, Encoding.UTF8)) { if (Model.AddLicense) { DocumentWriter.WriteLicense(outputFile); } DocumentWriter.WriteJQueryHeader(outputFile); foreach (var file in Model.Files) { if (!file.IsSelected) { continue; } outputFile.WriteLine(RegionInfoParser.GetFromFile(file.FullFilename)); } DocumentWriter.WriteJQueryFooter(outputFile); } MessageBox.Show("File's done"); }
public override void Run( ) { ActionEnum action = PromptAction(); // Opening the PDF file... string mainFilePath = PromptFileChoice("Please select a PDF file"); using (File mainFile = new File(mainFilePath)) { Document mainDocument = mainFile.Document; Pages mainPages = mainDocument.Pages; int mainPagesCount = mainPages.Count; switch (action) { case ActionEnum.PageDataSizeCalculation: { Console.WriteLine("\nThis algorithm calculates the data size (expressed in bytes) of the selected document's pages."); Console.WriteLine("Legend:"); Console.WriteLine(" * full: page data size encompassing all its dependencies (like shared resources) -- this is the size of the page when extracted as a single-page document;"); Console.WriteLine(" * differential: additional page data size -- this is the extra-content that's not shared with previous pages;"); Console.WriteLine(" * incremental: data size of the page sublist encompassing all the previous pages and the current one.\n"); // Calculating the page data sizes... HashSet <PdfReference> visitedReferences = new HashSet <PdfReference>(); long incrementalDataSize = 0; foreach (Page page in mainPages) { long pageFullDataSize = PageManager.GetSize(page); long pageDifferentialDataSize = PageManager.GetSize(page, visitedReferences); incrementalDataSize += pageDifferentialDataSize; Console.WriteLine( "Page " + (page.Index + 1) + ": " + pageFullDataSize + " (full); " + pageDifferentialDataSize + " (differential); " + incrementalDataSize + " (incremental)" ); } } break; case ActionEnum.PageAddition: { // Opening the source file... string sourceFilePath = PromptFileChoice("Select the source PDF file"); using (File sourceFile = new File(sourceFilePath)) { // Source page collection. Pages sourcePages = sourceFile.Document.Pages; // Source page count. int sourcePagesCount = sourcePages.Count; // First page to add. int fromSourcePageIndex = PromptPageChoice("Select the start source page to add", sourcePagesCount); // Last page to add. int toSourcePageIndex = PromptPageChoice("Select the end source page to add", fromSourcePageIndex, sourcePagesCount) + 1; // Target position. int targetPageIndex = PromptPageChoice("Select the position where to insert the source pages", mainPagesCount + 1); // Add the chosen page range to the main document! new PageManager(mainDocument).Add( targetPageIndex, sourcePages.GetSlice( fromSourcePageIndex, toSourcePageIndex ) ); } // Serialize the main file! Serialize(mainFile, action); } break; case ActionEnum.PageMovement: { // First page to move. int fromSourcePageIndex = PromptPageChoice("Select the start page to move", mainPagesCount); // Last page to move. int toSourcePageIndex = PromptPageChoice("Select the end page to move", fromSourcePageIndex, mainPagesCount) + 1; // Target position. int targetPageIndex = PromptPageChoice("Select the position where to insert the pages", mainPagesCount + 1); // Move the chosen page range! new PageManager(mainDocument).Move( fromSourcePageIndex, toSourcePageIndex, targetPageIndex ); // Serialize the main file! Serialize(mainFile, action); } break; case ActionEnum.PageRemoval: { // First page to remove. int fromPageIndex = PromptPageChoice("Select the start page to remove", mainPagesCount); // Last page to remove. int toPageIndex = PromptPageChoice("Select the end page to remove", fromPageIndex, mainPagesCount) + 1; // Remove the chosen page range! new PageManager(mainDocument).Remove( fromPageIndex, toPageIndex ); // Serialize the main file! Serialize(mainFile, action); } break; case ActionEnum.PageExtraction: { // First page to extract. int fromPageIndex = PromptPageChoice("Select the start page", mainPagesCount); // Last page to extract. int toPageIndex = PromptPageChoice("Select the end page", fromPageIndex, mainPagesCount) + 1; // Extract the chosen page range! Document targetDocument = new PageManager(mainDocument).Extract( fromPageIndex, toPageIndex ); // Serialize the target file! Serialize(targetDocument.File, action); } break; case ActionEnum.DocumentMerge: { // Opening the source file... string sourceFilePath = PromptFileChoice("Select the source PDF file"); using (File sourceFile = new File(sourceFilePath)) { // Append the chosen source document to the main document! new PageManager(mainDocument).Add(sourceFile.Document); } // Serialize the main file! Serialize(mainFile, action); } break; case ActionEnum.DocumentBurst: { // Split the document into single-page documents! IList <Document> splitDocuments = new PageManager(mainDocument).Split(); // Serialize the split files! int index = 0; foreach (Document splitDocument in splitDocuments) { Serialize(splitDocument.File, action, ++index); } } break; case ActionEnum.DocumentSplitByPageIndex: { // Number of splits to apply to the source document. int splitCount; try { splitCount = Int32.Parse(PromptChoice("Number of split positions: ")); } catch { splitCount = 0; } // Split positions within the source document. int[] splitIndexes = new int[splitCount]; { int prevSplitIndex = 0; for (int index = 0; index < splitCount; index++) { int splitIndex = PromptPageChoice("Position " + (index + 1) + " of " + splitCount, prevSplitIndex + 1, mainPagesCount); splitIndexes[index] = splitIndex; prevSplitIndex = splitIndex; } } // Split the document at the chosen positions! IList <Document> splitDocuments = new PageManager(mainDocument).Split(splitIndexes); // Serialize the split files! { int index = 0; foreach (Document splitDocument in splitDocuments) { Serialize(splitDocument.File, action, ++index); } } } break; case ActionEnum.DocumentSplitOnMaximumFileSize: { // Maximum file size. long maxDataSize; { long mainFileSize = new io::FileInfo(mainFilePath).Length; int kbMaxDataSize; do { try { kbMaxDataSize = Int32.Parse(PromptChoice("Max file size (KB): ")); } catch { kbMaxDataSize = 0; } } while(kbMaxDataSize == 0); maxDataSize = kbMaxDataSize << 10; if (maxDataSize > mainFileSize) { maxDataSize = mainFileSize; } } // Split the document on maximum file size! IList <Document> splitDocuments = new PageManager(mainDocument).Split(maxDataSize); // Serialize the split files! { int index = 0; foreach (Document splitDocument in splitDocuments) { Serialize(splitDocument.File, action, ++index); } } } break; } } }
private void MergeExecute(object parameter) { var fi = new io::FileInfo(Model.OutputFile); if (fi.Exists) { if (MessageBox.Show("File already exists. Overwrite?", "Overwrite?", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { fi.Delete(); } else { return; } } using (var outputFile = new StreamWriter(fi.FullName, false, Encoding.UTF8)) { if (Model.AddLicense) DocumentWriter.WriteLicense(outputFile); DocumentWriter.WriteJQueryHeader(outputFile); foreach (var file in Model.Files) { if (!file.IsSelected) continue; outputFile.WriteLine(RegionInfoParser.GetFromFile(file.FullFilename)); } DocumentWriter.WriteJQueryFooter(outputFile); } MessageBox.Show("File's done"); }
public override void Run( ) { ActionEnum action = PromptAction(); // Opening the PDF file... string mainFilePath = PromptFileChoice("Please select a PDF file"); using (var mainFile = new File(mainFilePath)) { Document mainDocument = mainFile.Document; Pages mainPages = mainDocument.Pages; int mainPagesCount = mainPages.Count; switch (action) { case ActionEnum.PageDataSizeCalculation: { Console.WriteLine("\nThis algorithm calculates the data size (expressed in bytes) of the selected document's pages."); Console.WriteLine("Legend:"); Console.WriteLine(" * full: page data size encompassing all its dependencies (like shared resources) -- this is the size of the page when extracted as a single-page document;"); Console.WriteLine(" * differential: additional page data size -- this is the extra-content that's not shared with previous pages;"); Console.WriteLine(" * incremental: data size of the page sublist encompassing all the previous pages and the current one.\n"); // Calculating the page data sizes... HashSet<PdfReference> visitedReferences = new HashSet<PdfReference>(); long incrementalDataSize = 0; foreach (Page page in mainPages) { long pageFullDataSize = PageManager.GetSize(page); long pageDifferentialDataSize = PageManager.GetSize(page, visitedReferences); incrementalDataSize += pageDifferentialDataSize; Console.WriteLine( "Page " + page.Number + ": " + pageFullDataSize + " (full); " + pageDifferentialDataSize + " (differential); " + incrementalDataSize + " (incremental)" ); } } break; case ActionEnum.BlankPageDetection: { Console.WriteLine( "\nThis algorithm makes a simple guess about whether a page should be considered empty:" + "\nit evaluates the middle portion (70%) of a page assuming that possible contents" + "\noutside this area would NOT qualify as actual (informative) content (such as" + "\nredundant patterns like footers and headers). Obviously, this assumption may need" + "\nsome fine-tuning as each document features its own layout ratios. Alternatively," + "\nan adaptive algorithm should automatically evaluate the content role based on its" + "\ntypographic attributes in relation to the other contents existing in the same page" + "\nor document.\n"); int blankPageCount = 0; foreach (Page page in mainPages) { SKRect pageBox = page.Box; SKSize margin = new SKSize(pageBox.Width * .15f, pageBox.Height * .15f); SKRect contentBox = SKRect.Create(margin.Width, margin.Height, pageBox.Width - margin.Width * 2, pageBox.Height - margin.Height * 2); if (PageManager.IsBlank(page, contentBox)) { blankPageCount++; Console.WriteLine("Page " + page.Number + " is blank"); } } Console.WriteLine(blankPageCount > 0 ? "Blank pages detected: " + blankPageCount + " of " + mainPages.Count : "No blank pages detected."); } break; case ActionEnum.PageAddition: { // Opening the source file... string sourceFilePath = PromptFileChoice("Select the source PDF file"); using (var sourceFile = new File(sourceFilePath)) { // Source page collection. Pages sourcePages = sourceFile.Document.Pages; // Source page count. int sourcePagesCount = sourcePages.Count; // First page to add. int fromSourcePageIndex = PromptPageChoice("Select the start source page to add", sourcePagesCount); // Last page to add. int toSourcePageIndex = PromptPageChoice("Select the end source page to add", fromSourcePageIndex, sourcePagesCount) + 1; // Target position. int targetPageIndex = PromptPageChoice("Select the position where to insert the source pages", mainPagesCount + 1); // Add the chosen page range to the main document! new PageManager(mainDocument).Add( targetPageIndex, sourcePages.GetSlice( fromSourcePageIndex, toSourcePageIndex ) ); } // Serialize the main file! Serialize(mainFile, action); } break; case ActionEnum.PageMovement: { // First page to move. int fromSourcePageIndex = PromptPageChoice("Select the start page to move", mainPagesCount); // Last page to move. int toSourcePageIndex = PromptPageChoice("Select the end page to move", fromSourcePageIndex, mainPagesCount) + 1; // Target position. int targetPageIndex = PromptPageChoice("Select the position where to insert the pages", mainPagesCount + 1); // Move the chosen page range! new PageManager(mainDocument).Move( fromSourcePageIndex, toSourcePageIndex, targetPageIndex ); // Serialize the main file! Serialize(mainFile, action); } break; case ActionEnum.PageRemoval: { // First page to remove. int fromPageIndex = PromptPageChoice("Select the start page to remove", mainPagesCount); // Last page to remove. int toPageIndex = PromptPageChoice("Select the end page to remove", fromPageIndex, mainPagesCount) + 1; // Remove the chosen page range! new PageManager(mainDocument).Remove( fromPageIndex, toPageIndex ); // Serialize the main file! Serialize(mainFile, action); } break; case ActionEnum.PageExtraction: { // First page to extract. int fromPageIndex = PromptPageChoice("Select the start page", mainPagesCount); // Last page to extract. int toPageIndex = PromptPageChoice("Select the end page", fromPageIndex, mainPagesCount) + 1; // Extract the chosen page range! Document targetDocument = new PageManager(mainDocument).Extract( fromPageIndex, toPageIndex ); // Serialize the target file! Serialize(targetDocument.File, action); } break; case ActionEnum.DocumentMerge: { // Opening the source file... string sourceFilePath = PromptFileChoice("Select the source PDF file"); using (var sourceFile = new File(sourceFilePath)) { // Append the chosen source document to the main document! new PageManager(mainDocument).Add(sourceFile.Document); } // Serialize the main file! Serialize(mainFile, action); } break; case ActionEnum.DocumentBurst: { // Split the document into single-page documents! IList<Document> splitDocuments = new PageManager(mainDocument).Split(); // Serialize the split files! int index = 0; foreach (Document splitDocument in splitDocuments) { Serialize(splitDocument.File, action, ++index); } } break; case ActionEnum.DocumentSplitByPageIndex: { // Number of splits to apply to the source document. int splitCount; try { splitCount = Int32.Parse(PromptChoice("Number of split positions: ")); } catch { splitCount = 0; } // Split positions within the source document. int[] splitIndexes = new int[splitCount]; { int prevSplitIndex = 0; for (int index = 0; index < splitCount; index++) { int splitIndex = PromptPageChoice("Position " + (index + 1) + " of " + splitCount, prevSplitIndex + 1, mainPagesCount); splitIndexes[index] = splitIndex; prevSplitIndex = splitIndex; } } // Split the document at the chosen positions! IList<Document> splitDocuments = new PageManager(mainDocument).Split(splitIndexes); // Serialize the split files! { int index = 0; foreach (Document splitDocument in splitDocuments) { Serialize(splitDocument.File, action, ++index); } } } break; case ActionEnum.DocumentSplitOnMaximumFileSize: { // Maximum file size. long maxDataSize; { long mainFileSize = new io::FileInfo(mainFilePath).Length; int kbMaxDataSize; do { try { kbMaxDataSize = Int32.Parse(PromptChoice("Max file size (KB): ")); } catch { kbMaxDataSize = 0; } } while (kbMaxDataSize == 0); maxDataSize = kbMaxDataSize << 10; if (maxDataSize > mainFileSize) { maxDataSize = mainFileSize; } } // Split the document on maximum file size! IList<Document> splitDocuments = new PageManager(mainDocument).Split(maxDataSize); // Serialize the split files! { int index = 0; foreach (Document splitDocument in splitDocuments) { Serialize(splitDocument.File, action, ++index); } } } break; } } }
public override void Run( ) { ActionEnum action = PromptAction(); // Opening the PDF file... string mainFilePath = PromptFileChoice("Please select a PDF file"); using(File mainFile = new File(mainFilePath)) { Document mainDocument = mainFile.Document; Pages mainPages = mainDocument.Pages; int mainPagesCount = mainPages.Count; switch(action) { case ActionEnum.PageDataSizeCalculation: { Console.WriteLine("\nThis algorithm calculates the data size (expressed in bytes) of the selected document's pages."); Console.WriteLine("Legend:"); Console.WriteLine(" * full: page data size encompassing all its dependencies (like shared resources) -- this is the size of the page when extracted as a single-page document;"); Console.WriteLine(" * differential: additional page data size -- this is the extra-content that's not shared with previous pages;"); Console.WriteLine(" * incremental: data size of the page sublist encompassing all the previous pages and the current one.\n"); // Calculating the page data sizes... HashSet<PdfReference> visitedReferences = new HashSet<PdfReference>(); long incrementalDataSize = 0; foreach(Page page in mainPages) { long pageFullDataSize = PageManager.GetSize(page); long pageDifferentialDataSize = PageManager.GetSize(page, visitedReferences); incrementalDataSize += pageDifferentialDataSize; Console.WriteLine( "Page " + (page.Index+1) + ": " + pageFullDataSize + " (full); " + pageDifferentialDataSize + " (differential); " + incrementalDataSize + " (incremental)" ); } } break; case ActionEnum.PageAddition: { // Opening the source file... string sourceFilePath = PromptFileChoice("Select the source PDF file"); using(File sourceFile = new File(sourceFilePath)) { // Source page collection. Pages sourcePages = sourceFile.Document.Pages; // Source page count. int sourcePagesCount = sourcePages.Count; // First page to add. int fromSourcePageIndex = PromptPageChoice("Select the start source page to add", sourcePagesCount); // Last page to add. int toSourcePageIndex = PromptPageChoice("Select the end source page to add", fromSourcePageIndex, sourcePagesCount) + 1; // Target position. int targetPageIndex = PromptPageChoice("Select the position where to insert the source pages", mainPagesCount + 1); // Add the chosen page range to the main document! new PageManager(mainDocument).Add( targetPageIndex, sourcePages.GetSlice( fromSourcePageIndex, toSourcePageIndex ) ); } // Serialize the main file! Serialize(mainFile, action); } break; case ActionEnum.PageMovement: { // First page to move. int fromSourcePageIndex = PromptPageChoice("Select the start page to move", mainPagesCount); // Last page to move. int toSourcePageIndex = PromptPageChoice("Select the end page to move", fromSourcePageIndex, mainPagesCount) + 1; // Target position. int targetPageIndex = PromptPageChoice("Select the position where to insert the pages", mainPagesCount + 1); // Move the chosen page range! new PageManager(mainDocument).Move( fromSourcePageIndex, toSourcePageIndex, targetPageIndex ); // Serialize the main file! Serialize(mainFile, action); } break; case ActionEnum.PageRemoval: { // First page to remove. int fromPageIndex = PromptPageChoice("Select the start page to remove", mainPagesCount); // Last page to remove. int toPageIndex = PromptPageChoice("Select the end page to remove", fromPageIndex, mainPagesCount) + 1; // Remove the chosen page range! new PageManager(mainDocument).Remove( fromPageIndex, toPageIndex ); // Serialize the main file! Serialize(mainFile, action); } break; case ActionEnum.PageExtraction: { // First page to extract. int fromPageIndex = PromptPageChoice("Select the start page", mainPagesCount); // Last page to extract. int toPageIndex = PromptPageChoice("Select the end page", fromPageIndex, mainPagesCount) + 1; // Extract the chosen page range! Document targetDocument = new PageManager(mainDocument).Extract( fromPageIndex, toPageIndex ); // Serialize the target file! Serialize(targetDocument.File, action); } break; case ActionEnum.DocumentMerge: { // Opening the source file... string sourceFilePath = PromptFileChoice("Select the source PDF file"); using(File sourceFile = new File(sourceFilePath)) { // Append the chosen source document to the main document! new PageManager(mainDocument).Add(sourceFile.Document); } // Serialize the main file! Serialize(mainFile, action); } break; case ActionEnum.DocumentBurst: { // Split the document into single-page documents! IList<Document> splitDocuments = new PageManager(mainDocument).Split(); // Serialize the split files! int index = 0; foreach(Document splitDocument in splitDocuments) {Serialize(splitDocument.File, action, ++index);} } break; case ActionEnum.DocumentSplitByPageIndex: { // Number of splits to apply to the source document. int splitCount; try {splitCount = Int32.Parse(PromptChoice("Number of split positions: "));} catch {splitCount = 0;} // Split positions within the source document. int[] splitIndexes = new int[splitCount]; { int prevSplitIndex = 0; for(int index = 0; index < splitCount; index++) { int splitIndex = PromptPageChoice("Position " + (index + 1) + " of " + splitCount, prevSplitIndex + 1, mainPagesCount); splitIndexes[index] = splitIndex; prevSplitIndex = splitIndex; } } // Split the document at the chosen positions! IList<Document> splitDocuments = new PageManager(mainDocument).Split(splitIndexes); // Serialize the split files! { int index = 0; foreach(Document splitDocument in splitDocuments) {Serialize(splitDocument.File, action, ++index);} } } break; case ActionEnum.DocumentSplitOnMaximumFileSize: { // Maximum file size. long maxDataSize; { long mainFileSize = new io::FileInfo(mainFilePath).Length; int kbMaxDataSize; do { try {kbMaxDataSize = Int32.Parse(PromptChoice("Max file size (KB): "));} catch {kbMaxDataSize = 0;} } while(kbMaxDataSize == 0); maxDataSize = kbMaxDataSize << 10; if(maxDataSize > mainFileSize) {maxDataSize = mainFileSize;} } // Split the document on maximum file size! IList<Document> splitDocuments = new PageManager(mainDocument).Split(maxDataSize); // Serialize the split files! { int index = 0; foreach(Document splitDocument in splitDocuments) {Serialize(splitDocument.File, action, ++index);} } } break; } } }