/// <summary> /// resave first page. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void resavePageButton_Click(object sender, RoutedEventArgs e) { using (var pdfDoc = new PDFDoc()) { var doc = mPdfViewCtrl.GetDoc(); var firstPage = doc.GetPage(1); pdfDoc.PagePushFront(firstPage); doc.PageRemove(doc.GetPageIterator(1)); doc.InsertPages(1, pdfDoc, 1, pdfDoc.GetPageCount(), PDFDocInsertFlag.e_none); mPdfViewCtrl.Update(); mPdfViewCtrl.UpdateLayout(); } }
/// <summary> /// The main entry point for the application. /// </summary> static void Main(string[] args) { // The first step in every application using PDFNet is to initialize the // library and set the path to common PDF resources. The library is usually // initialized only once, but calling Initialize() multiple times is also fine. PDFNet.Initialize(); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; try { // Open the PDF document. using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf")) using (ElementBuilder bld = new ElementBuilder()) // Used to build new Element objects using (ElementWriter writer = new ElementWriter()) // Used to write Elements to the page { UndoManager undo_manager = doc.GetUndoManager(); // Take a snapshot to which we can undo after making changes. ResultSnapshot snap0 = undo_manager.TakeSnapshot(); DocSnapshot snap0_state = snap0.CurrentState(); Page page = doc.PageCreate(); // Start a new page writer.Begin(page); // Begin writing to this page // ---------------------------------------------------------- // Add JPEG image to the file Image img = Image.Create(doc, input_path + "peppers.jpg"); Element element = bld.CreateImage(img, new Matrix2D(200, 0, 0, 250, 50, 500)); writer.WritePlacedElement(element); writer.End(); // Finish writing to the page doc.PagePushFront(page); // Take a snapshot after making changes, so that we can redo later (after undoing first). ResultSnapshot snap1 = undo_manager.TakeSnapshot(); if (snap1.PreviousState().Equals(snap0_state)) { Console.WriteLine("snap1 previous state equals snap0_state; previous state is correct"); } DocSnapshot snap1_state = snap1.CurrentState(); doc.Save(output_path + "addimage.pdf", SDFDoc.SaveOptions.e_incremental); if (undo_manager.CanUndo()) { ResultSnapshot undo_snap = undo_manager.Undo(); doc.Save(output_path + "addimage_undone.pdf", SDFDoc.SaveOptions.e_incremental); DocSnapshot undo_snap_state = undo_snap.CurrentState(); if (undo_snap_state.Equals(snap0_state)) { Console.WriteLine("undo_snap_state equals snap0_state; undo was successful"); } if (undo_manager.CanRedo()) { ResultSnapshot redo_snap = undo_manager.Redo(); doc.Save(output_path + "addimage_redone.pdf", SDFDoc.SaveOptions.e_incremental); if (redo_snap.PreviousState().Equals(undo_snap_state)) { Console.WriteLine("redo_snap previous state equals undo_snap_state; previous state is correct"); } DocSnapshot redo_snap_state = redo_snap.CurrentState(); if (redo_snap_state.Equals(snap1_state)) { Console.WriteLine("Snap1 and redo_snap are equal; redo was successful"); } } else { Console.WriteLine("Problem encountered - cannot redo."); } } else { Console.WriteLine("Problem encountered - cannot undo."); } } } catch (PDFNetException e) { Console.WriteLine(e.Message); } }
static void Main(string[] args) { PDFNet.Initialize(); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; // Sample 1 - Split a PDF document into multiple pages try { Console.WriteLine("_______________________________________________"); Console.WriteLine("Sample 1 - Split a PDF document into multiple pages..."); Console.WriteLine("Opening the input pdf..."); using (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf")) { in_doc.InitSecurityHandler(); int page_num = in_doc.GetPageCount(); for (int i = 1; i <= page_num; ++i) { using (PDFDoc new_doc = new PDFDoc()) { new_doc.InsertPages(0, in_doc, i, i, PDFDoc.InsertFlag.e_none); new_doc.Save(output_path + "newsletter_split_page_" + i + ".pdf", SDFDoc.SaveOptions.e_remove_unused); Console.WriteLine("Done. Result saved in newsletter_split_page_" + i + ".pdf"); } } } } catch (Exception e) { Console.WriteLine("Exception caught:\n{0}", e); } // Sample 2 - Merge several PDF documents into one try { Console.WriteLine("_______________________________________________"); Console.WriteLine("Sample 2 - Merge several PDF documents into one..."); using (PDFDoc new_doc = new PDFDoc()) { new_doc.InitSecurityHandler(); int page_num = 15; for (int i = 1; i <= page_num; ++i) { Console.WriteLine("Opening newsletter_split_page_" + i + ".pdf"); using (PDFDoc in_doc = new PDFDoc(output_path + "newsletter_split_page_" + i + ".pdf")) { new_doc.InsertPages(i, in_doc, 1, in_doc.GetPageCount(), PDFDoc.InsertFlag.e_none); } } new_doc.Save(output_path + "newsletter_merge_pages.pdf", SDFDoc.SaveOptions.e_remove_unused); } Console.WriteLine("Done. Result saved in newsletter_merge_pages.pdf"); } catch (Exception e) { Console.WriteLine("Exception caught:\n{0}", e); } // Sample 3 - Delete every second page try { Console.WriteLine("_______________________________________________"); Console.WriteLine("Sample 3 - Delete every second page..."); Console.WriteLine("Opening the input pdf..."); using (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf")) { in_doc.InitSecurityHandler(); int page_num = in_doc.GetPageCount(); PageIterator itr; while (page_num >= 1) { itr = in_doc.GetPageIterator(page_num); in_doc.PageRemove(itr); page_num -= 2; } in_doc.Save(output_path + "newsletter_page_remove.pdf", 0); } Console.WriteLine("Done. Result saved in newsletter_page_remove.pdf..."); } catch (Exception e) { Console.WriteLine("Exception caught:\n{0}", e); } // Sample 4 - Inserts a page from one document at different // locations within another document try { Console.WriteLine("_______________________________________________"); Console.WriteLine("Sample 4 - Insert a page at different locations..."); Console.WriteLine("Opening the input pdf..."); using (PDFDoc in1_doc = new PDFDoc(input_path + "newsletter.pdf")) using (PDFDoc in2_doc = new PDFDoc(input_path + "fish.pdf")) { in1_doc.InitSecurityHandler(); in2_doc.InitSecurityHandler(); Page src_page = in2_doc.GetPage(1); PageIterator dst_page = in1_doc.GetPageIterator(1); int page_num = 1; while (dst_page.HasNext()) { if (page_num++ % 3 == 0) { in1_doc.PageInsert(dst_page, src_page); } dst_page.Next(); } in1_doc.Save(output_path + "newsletter_page_insert.pdf", 0); Console.WriteLine("Done. Result saved in newsletter_page_insert.pdf..."); } } catch (Exception e) { Console.WriteLine("Exception caught:\n{0}", e); } // Sample 5 - Replicate pages within a single document try { Console.WriteLine("_______________________________________________"); Console.WriteLine("Sample 5 - Replicate pages within a single document..."); Console.WriteLine("Opening the input pdf..."); using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf")) { doc.InitSecurityHandler(); // Replicate the cover page three times (copy page #1 and place it before the // seventh page in the document page sequence) Page cover = doc.GetPage(1); PageIterator p7 = doc.GetPageIterator(7); doc.PageInsert(p7, cover); doc.PageInsert(p7, cover); doc.PageInsert(p7, cover); // Replicate the cover page two more times by placing it before and after // existing pages. doc.PagePushFront(cover); doc.PagePushBack(cover); doc.Save(output_path + "newsletter_page_clone.pdf", 0); Console.WriteLine("Done. Result saved in newsletter_page_clone.pdf..."); } } catch (Exception e) { Console.WriteLine("Exception caught:\n{0}", e); } // Sample 6 - Use ImportPages() in order to copy multiple pages at once // in order to preserve shared resources between pages (e.g. images, fonts, // colorspaces, etc.) try { Console.WriteLine("_______________________________________________"); Console.WriteLine("Sample 6 - Preserving shared resources using ImportPages..."); Console.WriteLine("Opening the input pdf..."); using (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf")) { in_doc.InitSecurityHandler(); using (PDFDoc new_doc = new PDFDoc()) { ArrayList copy_pages = new ArrayList(); for (PageIterator itr = in_doc.GetPageIterator(); itr.HasNext(); itr.Next()) { copy_pages.Add(itr.Current()); } ArrayList imported_pages = new_doc.ImportPages(copy_pages); for (int i = 0; i != imported_pages.Count; ++i) { new_doc.PagePushFront((Page)imported_pages[i]); // Order pages in reverse order. // Use PagePushBack() if you would like to preserve the same order. } new_doc.Save(output_path + "newsletter_import_pages.pdf", 0); Console.WriteLine("Done. Result saved in newsletter_import_pages.pdf..."); Console.WriteLine(); Console.WriteLine("Note that the output file size is less than half the size"); Console.WriteLine("of the file produced using individual page copy operations"); Console.WriteLine("between two documents"); } } } catch (Exception e) { Console.WriteLine("Exception caught:\n{0}", e); } }