private static void GetFormFieldProperties(PdfPage page, PdfWidgetAnnot widget, PdfFormField field) { Console.WriteLine("==========================================="); if (field != null) { Console.WriteLine("Name: " + field.GetFullName()); Console.WriteLine("Value: " + field.GetValue()); Console.WriteLine("Tooltip: " + field.GetTooltip()); string options = ""; for (int i = 0; i < field.GetNumOptions(); i++) { options += field.GetOptionValue(i) + " | "; } Console.WriteLine("Options: " + options); Console.WriteLine("MaxLength: " + field.GetMaxLength()); Console.WriteLine("Multiline: " + (field.GetFlags() & Pdfix.kFieldFlagMultiline).ToString()); Console.WriteLine("IsFormatted: " + (field.GetAAction(PdfActionEventType.kActionEventFieldFormat) != null).ToString()); Console.WriteLine("Required: " + ((field.GetFlags() & Pdfix.kFieldFlagRequired).ToString()).ToString()); Console.WriteLine("ReadOnly: " + ((field.GetFlags() & Pdfix.kFieldFlagReadOnly).ToString()).ToString()); Console.WriteLine("IsFormatted: " + (field.GetAAction(PdfActionEventType.kActionEventFieldCalculate) != null).ToString()); Console.WriteLine("IsChecked: " + (field.GetValue() == field.GetWidgetExportValue(widget)).ToString()); Console.WriteLine("FieldType: " + field.GetType().ToString()); } if (page != null && widget != null) { // widget specific properties var bbox = widget.GetBBox(); var page_view = page.AcquirePageView(1, PdfRotate.kRotate0); var rect = page_view.RectToDevice(bbox); page_view.Release(); // calculate widget annot position in % of the page width var left = (double)rect.left / (double)page_view.GetDeviceWidth(); var top = (double)rect.top / (double)page_view.GetDeviceHeight(); var width = (double)(rect.right - rect.left) / (double)page_view.GetDeviceWidth(); var height = (double)(rect.bottom - rect.top) / (double)page_view.GetDeviceHeight(); Console.WriteLine("TabOrder: " + (_tabOrder++).ToString()); Console.WriteLine("Left: " + left.ToString() + ", Top: " + top.ToString()); Console.WriteLine("Width: " + width.ToString() + ", Height: " + height.ToString()); } }
private static void ExtractImage(PdfPage page, PdsImage image, string savePath) { // widget specific properties var bbox = image.GetBBox(); var page_view = page.AcquirePageView(1, PdfRotate.kRotate0); var rect = page_view.RectToDevice(bbox); PdfMatrix matrix = page_view.GetDeviceMatrix(); page_view.Release(); string imgPath = savePath + "/image_" + (++_imageIndex).ToString() + ".jpg"; Console.WriteLine("Image Found: " + imgPath); var img = PdfixEngine.Instance.CreateImage(rect.right - rect.left, rect.bottom - rect.top, PsImageDIBFormat.kImageDIBFormatArgb); PdfPageRenderParams renderParams = new PdfPageRenderParams { clip_box = bbox, matrix = matrix, image = img }; if (!page.DrawContent(renderParams, null, null)) { PdfixEngine.ThrowException(); } PdfImageParams imgParams = new PdfImageParams { format = PdfImageFormat.kImageFormatJpg, quality = 80 }; if (!img.Save(imgPath, imgParams)) { PdfixEngine.ThrowException(); } img.Destroy(); }
public async Task <List <string> > ExtractImage( String email, String licenseKey, String openPath, String imgPath, Double zoom ) { List <string> imageList = new List <string>(); try { Pdfix pdfix = new Pdfix(); if (pdfix == null) { throw new Exception("Pdfix initialization fail"); } if (!pdfix.Authorize(email, licenseKey)) { throw new Exception(pdfix.GetError()); } PdfDoc doc = pdfix.OpenDoc(openPath, ""); if (doc == null) { throw new Exception(pdfix.GetError()); } for (int i = 0; i < doc.GetNumPages(); i++) { PdfPage page = doc.AcquirePage(i); if (page == null) { throw new Exception(pdfix.GetError()); } PdfPageView pageView = page.AcquirePageView(zoom, PdfRotate.kRotate0); if (pageView == null) { throw new Exception(pdfix.GetError()); } int width = pageView.GetDeviceWidth(); int height = pageView.GetDeviceHeight(); PsImage image = pdfix.CreateImage(width, height, PsImageDIBFormat.kImageDIBFormatArgb); if (image == null) { throw new Exception(pdfix.GetError()); } PdfPageRenderParams pdfPageRenderParams = new PdfPageRenderParams(); pdfPageRenderParams.image = image; pdfPageRenderParams.matrix = pageView.GetDeviceMatrix(); pdfPageRenderParams.render_flags = Pdfix.kRenderAnnot; if (!page.DrawContent(pdfPageRenderParams, null, IntPtr.Zero)) { throw new Exception(pdfix.GetError()); } PsStream stream = pdfix.CreateFileStream(imgPath + i.ToString() + ".jpg", PsFileMode.kPsWrite); PdfImageParams imgParams = new PdfImageParams(); imgParams.format = PdfImageFormat.kImageFormatJpg; imgParams.quality = 75; if (!image.SaveToStream(stream, imgParams)) { throw new Exception(pdfix.GetError()); } imageList.Add(imgPath + i.ToString()); stream.Destroy(); pageView.Release(); page.Release(); } doc.Close(); } catch (Exception ex) { throw ex; } return(imageList); }
public async Task <ResponseObject> ExtractJsonFromPDF(string email, string licenseKey, string filePath, List <string> imageList) { ResponseObject responseObject = new ResponseObject(); List <string> errorList = new List <string>(); try { Pdfix pdfix = new Pdfix(); if (pdfix == null) { throw new Exception("Pdfix initialization fail"); } if (!pdfix.Authorize(email, licenseKey)) { throw new Exception(pdfix.GetError()); } PdfDoc doc = pdfix.OpenDoc(filePath, ""); if (doc == null) { throw new Exception(); } int pageCount = doc.GetNumPages(); List <PDF> pdfList = new List <PDF>(); for (int i = 0; i < doc.GetNumPages(); i++) { List <PDFObject> pdfObjectList = new List <PDFObject>(); PdfPage page = doc.AcquirePage(i); PDF pdfObj = new PDF(); pdfObj.page = i.ToString(); pdfObj.imageUrl = GetBase64String(imageList[i]); _tabOrder = 0; int annots = page.GetNumAnnots(); for (int j = 0; j < page.GetNumAnnots(); j++) { PdfAnnot pdfAnnot = page.GetAnnot(j); PdfAnnotSubtype pdfAnnotSubtype = pdfAnnot.GetSubtype(); PdfFormField field = null; bool isChecked = false; if (pdfAnnotSubtype == PdfAnnotSubtype.kAnnotLink) { var widget = (PdfLinkAnnot)pdfAnnot; field = doc.GetFormField(j); isChecked = field.GetValue() == field.GetWidgetExportValue(widget); } if (pdfAnnotSubtype == PdfAnnotSubtype.kAnnotWidget) { var widget = (PdfWidgetAnnot)pdfAnnot; field = widget.GetFormField(); if (field == null) { field = doc.GetFormField(j); } isChecked = field.GetValue() == field.GetWidgetExportValue(widget); } if (pdfAnnotSubtype == PdfAnnotSubtype.kAnnotHighlight) { var widget = (PdfTextMarkupAnnot)pdfAnnot; field = doc.GetFormField(j); isChecked = field.GetValue() == field.GetWidgetExportValue(widget); } if (field == null) { field = doc.GetFormField(j); string fieldName = field.GetFullName(); errorList.Add(fieldName); throw new Exception(); } PDFObject pdfObject = new PDFObject(); pdfObject.fieldName = field.GetFullName(); pdfObject.fieldValue = field.GetValue(); pdfObject.maxLength = field.GetMaxLength(); pdfObject.tooltip = field.GetTooltip(); pdfObject.displayName = field.GetDefaultValue(); pdfObject.multiLine = ((field.GetFlags() & Pdfix.kFieldFlagMultiline) != 0) ? true : false; pdfObject.isFormatted = ((field.GetAAction(PdfActionEventType.kActionEventFieldFormat)) != null) ? true : false; pdfObject.required = ((field.GetFlags() & Pdfix.kFieldFlagRequired) != 0) ? true : false; pdfObject.readOnly = ((field.GetFlags() & Pdfix.kFieldFlagReadOnly) != 0) ? true : false; pdfObject.tabOrder = _tabOrder++; pdfObject.isChecked = isChecked; pdfObject.fieldType = GetFieldType(field); List <string> dropdownList = new List <string>(); for (int k = 0; k < field.GetOptionCount(); k++) { string optionValue = field.GetOptionValue(k); dropdownList.Add(optionValue); } pdfObject.optionList = dropdownList; PdfRect bbox = pdfAnnot.GetBBox(); PdfAnnotAppearance pdfAnnotAppearance = pdfAnnot.GetAppearance(); PdfPageView pageView = page.AcquirePageView(1.0, PdfRotate.kRotate0); if (pageView == null) { throw new Exception(pdfix.GetError()); } var devRect = pageView.RectToDevice(bbox); var x = devRect.left; var y = devRect.top; var width = devRect.right - devRect.left; var height = devRect.bottom - devRect.top; var pageWidth = pageView.GetDeviceWidth(); var pageHeight = pageView.GetDeviceHeight(); var pdfvalue = ((double)x / pageWidth) * 100; var percentage = Convert.ToInt32(Math.Round(pdfvalue, 2)); pdfObject.x = ((double)devRect.left / pageView.GetDeviceWidth()) * 100; pdfObject.y = ((double)devRect.top / pageView.GetDeviceHeight()) * 100; pdfObject.width = ((double)(devRect.right - devRect.left) / pageView.GetDeviceWidth()) * 100; pdfObject.height = ((double)(devRect.bottom - devRect.top) / pageView.GetDeviceHeight()) * 100; pageView.Release(); pdfObjectList.Add(pdfObject); } pdfObj.pdfObjList = pdfObjectList; pdfObj.width = 927; pdfObj.height = 1200; pdfList.Add(pdfObj); } responseObject.flag = true; responseObject.data = pdfList; responseObject.message = "Document Import Successfully"; doc.Close(); pdfix.Destroy(); } catch (Exception ex) { responseObject.errorList = errorList; throw ex; } return(responseObject); }