Beispiel #1
0
        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);
        }
Beispiel #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);
        }