// GET: api/InboundFax/5 public HttpResponseMessage Get(string id) { HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); try { Logging.LogErrors(ConfigurationValues.ErrorLogPath, ConfigurationValues.PdfTronLicenseKey); PDFNet.Initialize(ConfigurationValues.PdfTronLicenseKey); PDFDoc documentToAdd = new PDFDoc(); string xodFileName = Guid.NewGuid().ToString() + ".xod"; string pdfFileName = xodFileName.Replace(".xod", ".pdf"); string filePathXod = ConfigurationValues.PathToXodFile + xodFileName; string filePathPdf = ConfigurationValues.PathToXodFile + pdfFileName; PDFDoc newDocument = new PDFDoc(); if (string.IsNullOrEmpty(id)) { try { File.Delete(filePathXod); } catch { } File.Create(filePathXod).Dispose(); result = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(filePathXod, FileMode.Open); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = ConfigurationValues.XodFileName }; return result; } else { string[] documents = id.Split('~'); for (int i = 0; i < documents.Length - 1; i++) { FaxRepository faxData = new FaxRepository(); //documentToAdd = new PDFDoc(faxData.GetFullPathInboundFax(documents[i])); documentToAdd = new PDFDoc(documents[i]); PageIterator itr = documentToAdd.GetPageIterator(); for (; itr.HasNext(); itr.Next()) { try { pdftron.PDF.Page page = itr.Current(); newDocument.PageInsert(newDocument.GetPageIterator(itr.GetPageNumber()), page); } catch (Exception er) { string s1 = er.ToString(); } } } try { File.Delete(filePathXod); } catch { } documentToAdd.Close(); documentToAdd.Dispose(); DocumentRepository documentData = new DocumentRepository(); documentData.AddUpdateCurrentDocument(Utility.GetUserName(), filePathXod, filePathPdf, id, documentData.GetFullPathToDocument(Utility.GetUserName())); File.Create(filePathXod).Dispose(); newDocument.Save(filePathPdf, 0); pdftron.Filters.Filter objFilter = pdftron.PDF.Convert.ToXod(newDocument); System.Threading.Thread.Sleep(ConfigurationValues.XodSaveDelay); objFilter.WriteToFile(filePathXod, true); var stream = new FileStream(filePathXod, FileMode.Open); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = ConfigurationValues.XodFileName }; newDocument.Close(); newDocument.Dispose(); } return result; } catch (Exception er) { Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString()); return result; } }
static void Main(string[] args) { PDFNet.Initialize(); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; try { // Read a PDF document from a stream or pass-in a memory buffer... FileStream istm = new FileStream(input_path + "tiger.pdf", FileMode.Open, FileAccess.Read); using (PDFDoc doc = new PDFDoc(istm)) using (ElementWriter writer = new ElementWriter()) using (ElementReader reader = new ElementReader()) { doc.InitSecurityHandler(); int num_pages = doc.GetPageCount(); Element element; // Perform some document editing ... // Here we simply copy all elements from one page to another. for (int i = 1; i <= num_pages; ++i) { Page pg = doc.GetPage(2 * i - 1); reader.Begin(pg); Page new_page = doc.PageCreate(pg.GetMediaBox()); doc.PageInsert(doc.GetPageIterator(2 * i), new_page); writer.Begin(new_page); while ((element = reader.Next()) != null) // Read page contents { writer.WriteElement(element); } writer.End(); reader.End(); } doc.Save(output_path + "doc_memory_edit.pdf", SDFDoc.SaveOptions.e_remove_unused); // Save the document to a stream or a memory buffer... using (FileStream ostm = new FileStream(output_path + "doc_memory_edit.txt", FileMode.Create, FileAccess.Write)) { doc.Save(ostm, SDFDoc.SaveOptions.e_remove_unused); } // Read some data from the file stored in memory reader.Begin(doc.GetPage(1)); while ((element = reader.Next()) != null) { if (element.GetType() == Element.Type.e_path) { Console.Write("Path, "); } } reader.End(); Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("Done. Result saved in doc_memory_edit.pdf and doc_memory_edit.txt ..."); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } }
// GET: api/FileFromFolder/5 public HttpResponseMessage Get(string id) { string fullPath = System.Configuration.ConfigurationManager.AppSettings["holdFolder"] + id; PDFNet.Initialize("CT Orthopaedic Specialists(ct-ortho.com):ENTCPU:1::W:AMS(20160714):DF4FD2223CBF58B9128E100F400DD2BC2BFD701DC22C3C2E6D83F6B6F5C7"); PDFDoc documentToAdd = new PDFDoc(); string xodFileName = Guid.NewGuid().ToString() + ".xod"; string pdfFileName = xodFileName.Replace(".xod", ".pdf"); string filePathXod = ConfigurationValues.PathToXodFile + xodFileName; string filePathPdf = ConfigurationValues.PathToXodFile + pdfFileName; PDFDoc newDocument = new PDFDoc(); if (string.IsNullOrEmpty(id)) { try { File.Delete(filePathXod); } catch { } File.Create(filePathXod).Dispose(); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(filePathXod, FileMode.Open); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = ConfigurationValues.XodFileName }; return result; } else { string[] documents = id.Split('~'); documentToAdd = new PDFDoc(fullPath); PageIterator itr = documentToAdd.GetPageIterator(); for (; itr.HasNext(); itr.Next()) { try { pdftron.PDF.Page page = itr.Current(); newDocument.PageInsert(newDocument.GetPageIterator(itr.GetPageNumber()), page); } catch (Exception er) { string s1 = er.ToString(); } } try { File.Delete(filePathXod); } catch { } documentToAdd.Close(); documentToAdd.Dispose(); DocumentRepository documentData = new DocumentRepository(); documentData.AddUpdateCurrentDocument(Utility.GetUserName(), filePathXod, filePathPdf, id, documentData.GetFullPathToDocument(Utility.GetUserName())); File.Create(filePathXod).Dispose(); newDocument.Save(filePathPdf, 0); pdftron.Filters.Filter objFilter = pdftron.PDF.Convert.ToXod(newDocument); System.Threading.Thread.Sleep(ConfigurationValues.XodSaveDelay); objFilter.WriteToFile(filePathXod, true); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(filePathXod, FileMode.Open); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = ConfigurationValues.XodFileName }; newDocument.Close(); newDocument.Dispose(); return result; } }
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); } }
public HttpResponseMessage Get(string id) { string xodFileName = Guid.NewGuid().ToString() + ".xod"; string pdfFileName = xodFileName.Replace(".xod", ".pdf"); string filePathXod = ConfigurationValues.PathToXodFile + xodFileName; string filePathPdf = ConfigurationValues.PathToXodFile + pdfFileName; PDFNet.Initialize("CT Orthopaedic Specialists(ct-ortho.com):ENTCPU:1::W:AMS(20160714):DF4FD2223CBF58B9128E100F400DD2BC2BFD701DC22C3C2E6D83F6B6F5C7"); PDFDoc newDocument = new PDFDoc(); if (string.IsNullOrEmpty(id)) { try { File.Delete(filePathXod); } catch { } File.Create(filePathXod).Dispose(); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(filePathXod, FileMode.Open); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = ConfigurationValues.XodFileName }; return result; } else { string[] documents = id.Split('~'); for (int i = 0; i < documents.Length - 1; i++) { PDFDoc documenToAdd = new PDFDoc(ConfigurationValues.OutboundFaxDirectory + "\\" + Utility.GetUserName() + "\\" + documents[0]); PageIterator itr = documenToAdd.GetPageIterator(); for (; itr.HasNext(); itr.Next()) { try { pdftron.PDF.Page page = itr.Current(); newDocument.PageInsert(newDocument.GetPageIterator(itr.GetPageNumber()), page); } catch (Exception er) { string s1 = er.ToString(); } } } try { File.Delete(filePathXod); } catch { } DocumentData documentData = new DocumentData(); documentData.AddUpdateCurrentDocument(Utility.GetUserName(), filePathXod, filePathPdf); File.Create(filePathXod).Dispose(); newDocument.Save(filePathPdf, 0); pdftron.PDF.Convert.ToXod(newDocument, filePathXod); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(filePathXod, FileMode.Open); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = ConfigurationValues.XodFileName }; return result; } }