public void RunPdfTron(string input_path) { PDFNet.Initialize(); // string output_path = "../../../../TestFiles/Output/"; try { // Open the test file PDFDoc doc = new PDFDoc(input_path); doc.InitSecurityHandler(); PageIterator itr; ElementReader page_reader = new ElementReader(); for (itr = doc.GetPageIterator(); itr.HasNext(); itr.Next()) // Read every page { int pageno = itr.GetPageNumber(); page_reader.Begin(itr.Current()); ProcessElements(page_reader); page_reader.End(); } page_reader.Dispose(); // Calling Dispose() on ElementReader/Writer/Builder can result in increased performance and lower memory consumption. doc.Close(); } catch (PDFNetException e) { ConsoleLog += e.Message; } PDFNet.Terminate(); }
protected override void Dispose(bool disposing) { base.Dispose(disposing); mPdfViewCtrl?.CloseDoc(); mPdfViewCtrl = null; mPdfDoc?.Close(); mPdfDoc = null; }
public void Dispose() { if (pdfDoc != null) { pdfDoc.Close(); pdfDoc.Dispose(); } pdfDoc = null; }
public FileStreamWithPdftron(string pdfFilePath, FileItemStreamType streamType, WaterMarkInfo watermarkInfo) { var tempFilePath = string.Empty; try { tempFilePath = Path.GetTempFileName(); pdftron.PDFNet.Initialize(Properties.Settings.Default.PDFTronLicense); using (var doc = new PDFDoc(pdfFilePath)) { if (null != watermarkInfo) { WatermarkFile(doc, watermarkInfo); } if (streamType == FileItemStreamType.Downloadable) { doc.Save(tempFilePath, SDFDoc.SaveOptions.e_compatibility); } else { Convert.ToXod(doc, tempFilePath); } doc.Close(); } _wrappedStream = new FileStreamWithDelete(tempFilePath, FileMode.Open); } catch { if (File.Exists(tempFilePath)) { File.Delete(tempFilePath); } throw; } finally { if (File.Exists(pdfFilePath)) { File.Delete(pdfFilePath); } } }
protected override void Dispose(bool disposing) { base.Dispose(disposing); PTTabbedDocumentViewController tabbedDocumentViewController = mTabViewController; mTabViewController = null; nuint index = tabbedDocumentViewController.SelectedIndex; PTDocumentViewController documentViewController = tabbedDocumentViewController.SelectedViewController; PDFDoc pdfDoc = TypeConvertHelper.ConvPdfDocToManaged(documentViewController.PdfViewCtrl.GetDoc()); documentViewController.CloseDocumentWithCompletionHandler((bool success) => { pdfDoc.Close(); pdfDoc = null; }); tabbedDocumentViewController.RemoveTabAtIndex(index); tabbedDocumentViewController = null; GC.Collect(); }
public void ReadTextFromCoordinates(string input_path, int pagenumber, int urx, int ury, int llx, int lly) { PDFDoc doc = new PDFDoc(input_path); doc.InitSecurityHandler(); Page page = doc.GetPage(pagenumber); ElementReader reader = new ElementReader(); PageIterator itr = doc.GetPageIterator(); reader.Begin(itr.Current()); LowLevelTextExtractUtils u = new LowLevelTextExtractUtils(); //u.DumpAllText(reader); //ConsoleLog += u.ConsoleLog; //reader.End(); string field3 = u.ReadTextFromRect(page, new Rect(urx, ury, llx, lly), reader); ConsoleLog = field3; reader.Dispose(); doc.Close(); }
// 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; } }
public void ReadAdvanced(string input_path) { PDFNet.Initialize(); try { PDFDoc doc = new PDFDoc(input_path); doc.InitSecurityHandler(); Page page = doc.GetPage(1); if (page == null) { ConsoleLog += "Page not found."; return; } TextExtractor txt = new TextExtractor(); txt.Begin(page); // Read the page. // Other options you may want to consider... // txt.Begin(page, null, TextExtractor.ProcessingFlags.e_no_dup_remove); // txt.Begin(page, null, TextExtractor.ProcessingFlags.e_remove_hidden_text); // ... // Example 1. Get all text on the page in a single string. // Words will be separated with space or new line characters. if (example1_basic) { // Get the word count. ConsoleLog += "Word Count: {0}" + txt.GetWordCount(); ConsoleLog += "\n\n- GetAsText --------------------------\n{0}" + txt.GetAsText(); ConsoleLog += "-----------------------------------------------------------"; } // Example 2. Get XML logical structure for the page. if (example2_xml) { String text = txt.GetAsXML(TextExtractor.XMLOutputFlags.e_words_as_elements | TextExtractor.XMLOutputFlags.e_output_bbox | TextExtractor.XMLOutputFlags.e_output_style_info); ConsoleLog += "\n\n- GetAsXML --------------------------\n{0}" + text; ConsoleLog += "-----------------------------------------------------------"; } // Example 3. Extract words one by one. if (example3_wordlist) { TextExtractor.Word word; for (TextExtractor.Line line = txt.GetFirstLine(); line.IsValid(); line = line.GetNextLine()) { for (word = line.GetFirstWord(); word.IsValid(); word = word.GetNextWord()) { ConsoleLog += word.GetString(); } } ConsoleLog += "-----------------------------------------------------------"; } // Example 3. A more advanced text extraction example. // The output is XML structure containing paragraphs, lines, words, // as well as style and positioning information. if (example4_advanced) { Rect bbox; int cur_flow_id = -1, cur_para_id = -1; TextExtractor.Line line; TextExtractor.Word word; TextExtractor.Style s, line_style; // For each line on the page... for (line = txt.GetFirstLine(); line.IsValid(); line = line.GetNextLine()) { if (line.GetNumWords() == 0) { continue; } if (cur_flow_id != line.GetFlowID()) { if (cur_flow_id != -1) { if (cur_para_id != -1) { cur_para_id = -1; ConsoleLog += "</Para>"; } ConsoleLog += "</Flow>"; } cur_flow_id = line.GetFlowID(); ConsoleLog += "<Flow id=\"{0}\">" + cur_flow_id; } if (cur_para_id != line.GetParagraphID()) { if (cur_para_id != -1) { ConsoleLog += "</Para>"; } cur_para_id = line.GetParagraphID(); ConsoleLog += "<Para id=\"{0}\">" + cur_para_id; } bbox = line.GetBBox(); line_style = line.GetStyle(); Console.Write("<Line box=\"" + bbox.y1 + "," + bbox.y2 + "," + bbox.x1 + "," + bbox.x2 + ">"); PrintStyle(line_style); ConsoleLog += ""; // For each word in the line... for (word = line.GetFirstWord(); word.IsValid(); word = word.GetNextWord()) { // Output the bounding box for the word. bbox = word.GetBBox(); ConsoleLog += "<Word box=\"{0}, {1}, {2}, {3}\"" + bbox.x1 + bbox.y1 + bbox.x2 + bbox.y2; int sz = word.GetStringLen(); if (sz == 0) { continue; } // If the word style is different from the parent style, output the new style. s = word.GetStyle(); if (s != line_style) { PrintStyle(s); } ConsoleLog += ">\n" + word.GetString(); ConsoleLog += "</Word>"; } ConsoleLog += "</Line>"; } if (cur_flow_id != -1) { if (cur_para_id != -1) { cur_para_id = -1; ConsoleLog += "</Para>"; } ConsoleLog += "</Flow>"; } } // Note: Calling Dispose() on TextExtractor when it is not anymore in use can result in increased performance and lower memory consumption. txt.Dispose(); doc.Close(); ConsoleLog += "Done."; } catch (PDFNetException e) { ConsoleLog += e.Message; } // Sample code showing how to use low-level text extraction APIs. if (example5_low_level) { try { LowLevelTextExtractUtils util = new LowLevelTextExtractUtils(); PDFDoc doc = new PDFDoc(input_path); doc.InitSecurityHandler(); // Example 1. Extract all text content from the document ElementReader reader = new ElementReader(); PageIterator itr = doc.GetPageIterator(); //for (; itr.HasNext(); itr.Next()) // Read every page { reader.Begin(itr.Current()); LowLevelTextExtractUtils u = new LowLevelTextExtractUtils(); u.DumpAllText(reader); ConsoleLog += u.ConsoleLog; reader.End(); } // Example 2. Extract text based on the selection rectangle. ConsoleLog += "----------------------------------------------------"; ConsoleLog += "Extract text based on the selection rectangle."; ConsoleLog += "----------------------------------------------------"; Page first_page = doc.GetPage(1); string field1 = util.ReadTextFromRect(first_page, new Rect(27, 392, 563, 534), reader); string field2 = util.ReadTextFromRect(first_page, new Rect(28, 551, 106, 623), reader); string field3 = util.ReadTextFromRect(first_page, new Rect(208, 550, 387, 621), reader); ConsoleLog += "Field 1: {0}" + field1; ConsoleLog += "Field 2: {0}" + field2; ConsoleLog += "Field 3: {0}" + field3; // ... reader.Dispose(); doc.Close(); ConsoleLog += "Done."; } catch (PDFNetException e) { ConsoleLog += e.Message; } } PDFNet.Terminate(); }
/// <summary> /// The main entry point for the application. /// </summary> static void Main() { // Initialize PDFNet before calling any other PDFNet function. PDFNet.Initialize(); string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; string input_filename = "US061222892-a.pdf"; PDFDoc pdf_doc = new PDFDoc(input_path + input_filename); pdf_doc.InitSecurityHandler(); SDFDoc cos_doc = pdf_doc.GetSDFDoc(); int num_objs = cos_doc.XRefSize(); for (int i = 1; i < num_objs; ++i) { Obj obj = cos_doc.GetObj(i); if (obj != null && !obj.IsFree() && obj.IsStream()) { // Process only images DictIterator itr = obj.Find("Subtype"); if (!itr.HasNext() || itr.Value().GetName() != "Image") { continue; } pdftron.PDF.Image input_image = new pdftron.PDF.Image(obj); pdftron.PDF.Image new_image = null; // Process only gray-scale images if (input_image.GetComponentNum() != 1) { continue; } int bpc = input_image.GetBitsPerComponent(); if (bpc != 1) // Recompress 1 BPC images { continue; } // Skip images that are already compressed using JBIG2 itr = obj.Find("Filter"); if (itr.HasNext() && itr.Value().IsName() && itr.Value().GetName() == "JBIG2Decode") { continue; } FilterReader reader = new FilterReader(obj.GetDecodedStream()); ObjSet hint_set = new ObjSet(); Obj hint = hint_set.CreateArray(); hint.PushBackName("JBIG2"); hint.PushBackName("Lossless"); hint.PushBackName("Threshold"); hint.PushBackNumber(0.4); hint.PushBackName("SharePages"); hint.PushBackNumber(10000); new_image = pdftron.PDF.Image.Create( cos_doc, reader, input_image.GetImageWidth(), input_image.GetImageHeight(), 1, ColorSpace.CreateDeviceGray(), hint // A hint to image encoder to use JBIG2 compression ); Obj new_img_obj = new_image.GetSDFObj(); // Copy any important entries from the image dictionary itr = obj.Find("ImageMask"); if (itr.HasNext()) { new_img_obj.Put("ImageMask", itr.Value()); } itr = obj.Find("Mask"); if (itr.HasNext()) { new_img_obj.Put("Mask", itr.Value()); } cos_doc.Swap(i, new_image.GetSDFObj().GetObjNum()); } } pdf_doc.Save(output_path + "US061222892_JBIG2.pdf", SDFDoc.SaveOptions.e_remove_unused); pdf_doc.Close(); }
// GET: api/SeparatePDF/5 public IHttpActionResult Get(string id) { try { id = id + ","; PDFNet.Initialize(ConfigurationValues.PdfTronLicenseKey); string[] pagesSeparationGroups = id.Split('~'); string[] newPdfNames = new string[pagesSeparationGroups.Length - 1]; PDFDoc[] newPdfDocs = new PDFDoc[pagesSeparationGroups.Length - 1]; for (int i = 0; i < newPdfNames.Length; i++) { newPdfNames[i] = Guid.NewGuid().ToString() + ".pdf"; newPdfDocs[i] = new PDFDoc(); } DocumentRepository documentData = new DocumentRepository(); CurrentDocuments currentDocuments = documentData.GetCurrentDocumentPathPdf(Utility.GetUserName()); string[] currentDocumentCount = currentDocuments.CurrentDocumentList.Split('~'); string[] currentDocumentList = currentDocumentCount[0].Split('\\'); string pathToCurrentPdfFile = currentDocuments.PathToCurrentPdfDocument; string currentPdfPath = currentDocumentCount[0].Replace(currentDocumentList[currentDocumentList.Length - 1], ""); PDFDoc separatePagesFromDocument = new PDFDoc(pathToCurrentPdfFile); for (int i = 0; i < pagesSeparationGroups.Length - 1; i++) { string[] beginEndPageValues = pagesSeparationGroups[i].Split(','); string pagesSeparationBegin = beginEndPageValues[0]; string pagesSeparationEnd = beginEndPageValues[1]; if (pagesSeparationBegin.Length > 0) { newPdfDocs[i].InsertPages(0, separatePagesFromDocument, int.Parse(pagesSeparationBegin), int.Parse(pagesSeparationEnd), PDFDoc.InsertFlag.e_none); //newPdfDocs[i].Save(ConfigurationValues.OutboundFaxDirectory + "\\" + Utility.GetUserName() + "\\" + newPdfNames[i], 0); newPdfDocs[i].Save(currentPdfPath + newPdfNames[i], 0); } } separatePagesFromDocument.Close(); separatePagesFromDocument.Dispose(); File.Delete(currentDocumentCount[0]); for (int i = 0; i < newPdfNames.Length; i++) { newPdfDocs[i].Close(); newPdfDocs[i].Dispose(); } string[] deleteTheDocumentList = currentDocuments.CurrentDocumentList.Split('~'); for (int i = 0; i < deleteTheDocumentList.Length - 1; i++) { try { File.Delete(ConfigurationValues.OutboundFaxDirectory + "\\" + Utility.GetUserName() + "\\" + deleteTheDocumentList[i]); } catch { } } // ConfigurationValues.OutboundFaxDirectory + "\\" + Utility.GetUserName() + "\\" return Ok(); } catch { return BadRequest("Error in splitting document. Please try again"); } }
// 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; } }
// GET: api/SeparatePDF/5 public HttpResponseMessage Get(string id) //public string Get(string id) { id = id + ","; PDFNet.Initialize("CT Orthopaedic Specialists(ct-ortho.com):ENTCPU:1::W:AMS(20160714):DF4FD2223CBF58B9128E100F400DD2BC2BFD701DC22C3C2E6D83F6B6F5C7"); PDFDoc newDocument = new PDFDoc(); string[] pagesSeparationGroups = id.Split(','); string[] newPDFNames = new string[pagesSeparationGroups.Length - 1]; PDFDoc[] NewPDFDocs = new PDFDoc[pagesSeparationGroups.Length - 1]; //string pathToCurrentPdfFile = documentData.GetCurrentDocumentPathPDF(Utility.GetUserName()); for (int i = 0; i < newPDFNames.Length; i++) { newPDFNames[i] = Guid.NewGuid().ToString() + ".pdf"; NewPDFDocs[i] = new PDFDoc(); } DocumentRepository documentData = new DocumentRepository(); //int pageCount = 0; CurrentDocuments currentDocuments = documentData.GetCurrentDocumentPathPDF(Utility.GetUserName()); string pathToCurrentPdfFile = currentDocuments.PathToCurrentPDFDocument; //string[] fileName = pathToCurrentPdfFile.Split('\\'); //File.Move(pathToCurrentPdfFile, ConfigurationValues.TemporaryFaxPath + "\\" + pathToCurrentPdfFile[pathToCurrentPdfFile.Length - 1]); PDFDoc separatePagesFromDocument = new PDFDoc(pathToCurrentPdfFile); for (int i = 0; i < pagesSeparationGroups.Length - 1; i++) { string[] pagesSeparation = pagesSeparationGroups[i].Split('-'); //PageIterator itr = separatePagesFromDocument.GetPageIterator(i); NewPDFDocs[i].InsertPages(0, separatePagesFromDocument, int.Parse(pagesSeparation[0]), int.Parse(pagesSeparation[1]), PDFDoc.InsertFlag.e_none); NewPDFDocs[i].Save(ConfigurationValues.OutboundFaxDirectory + "\\" + Utility.GetUserName() + "\\" + newPDFNames[i], 0); } // string pathToCurrentPdfFileTemp = pathToCurrentPdfFile.Replace(".pdf", "temp.pdf"); //string pathToCurrentXodFile = documentData.GetCurrentDocumentPathXOD(Utility.GetUserName()); separatePagesFromDocument.Close(); separatePagesFromDocument.Dispose(); File.Delete(pathToCurrentPdfFile); for (int i = 0; i < newPDFNames.Length; i++) { NewPDFDocs[i].Close(); NewPDFDocs[i].Dispose(); } string[] deleteTheDocumentList = currentDocuments.CurrentDocumentList.Split('~'); for (int i = 0; i < deleteTheDocumentList.Length - 1; i++) { File.Delete(ConfigurationValues.OutboundFaxDirectory + "\\" + Utility.GetUserName() + "\\" + deleteTheDocumentList[i]); } // ConfigurationValues.OutboundFaxDirectory + "\\" + Utility.GetUserName() + "\\" HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); return result; }