Esempio n. 1
0
        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());
            }
        }
Esempio n. 2
0
        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);
        }