private static void ExtractAnnot(PdfAnnot annot, JObject node, DataType data_types)
        {
            var annot_dict = annot.GetObject();
            var subtype    = annot_dict.GetText("Subtype");

            node.Add("subtype", subtype);
            if (data_types.extract_bbox)
            {
                var bbox_node = new JObject();
                ExtractBBox(annot.GetBBox(), bbox_node, data_types);
                node.Add("bbox", bbox_node);
            }

            if (subtype == "Widget")
            {
                ExtractWidgetAnnot((PdfWidgetAnnot)annot, node, data_types);
            }
        }
        public static void Run(
            String openPath,                            // source PDF document
            String savePath,                            // output PDF document
            String imgPath                              // watermark to apply
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

            PdfDoc doc = pdfix.OpenDoc(openPath, "");

            if (doc == null)
            {
                throw new Exception(pdfix.GetError());
            }

            // identify image format from file path
            PdfImageFormat format = PdfImageFormat.kImageFormatJpg;

            if (Path.GetExtension(imgPath).ToLower() == ".png")
            {
                format = PdfImageFormat.kImageFormatPng;
            }

            // load image file data into memory stream
            byte[] bytes  = File.ReadAllBytes(imgPath);
            var    memStm = pdfix.CreateMemStream();

            if (memStm == null)
            {
                throw new Exception(pdfix.GetError());
            }
            memStm.Write(0, bytes);

            // create XObject from the image
            var image_obj = doc.CreateXObjectFromImage(memStm, format);

            if (image_obj == null)
            {
                throw new Exception(pdfix.GetError());
            }

            // find or add annotation on the first page
            var page = doc.AcquirePage(0);

            if (page == null)
            {
                throw new Exception(pdfix.GetError());
            }

            PdfAnnot annot = null;// page.GetAnnot(0);

            if (annot == null)
            {
                // create new annotation
                var page_view = page.AcquirePageView(1, PdfRotate.kRotate0);
                if (page_view == null)
                {
                    throw new Exception(pdfix.GetError());
                }

                // rect for the new annotation
                PdfRect annot_rect = new PdfRect()
                {
                    left   = 100,
                    right  = 300,
                    bottom = 100,
                    top    = 200
                };
                annot = page.CreateAnnot(PdfAnnotSubtype.kAnnotStamp, annot_rect);
                page.AddAnnot(0, annot);
            }

            // set annotation appearance
            if (!annot.SetAppearanceFromXObject(image_obj, PdfAnnotAppearanceMode.kAppearanceNormal))
            {
                throw new Exception(pdfix.GetError());
            }

            page.Release();

            // save document
            if (!doc.Save(savePath, Pdfix.kSaveFull))
            {
                throw new Exception(pdfix.GetError());
            }

            doc.Close();
        }
        public static void Run(
            String openPath,                            // source PDF document
            String savePath,                            // output PDF document
            String imgPath                              // watermark to apply
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

            PdfDoc doc = pdfix.OpenDoc(openPath, "");

            if (doc == null)
            {
                throw new Exception(pdfix.GetError());
            }

            // identify image format from file path
            PdfImageFormat format = PdfImageFormat.kImageFormatJpg;

            if (Path.GetExtension(imgPath).ToLower() == ".png")
            {
                format = PdfImageFormat.kImageFormatPng;
            }

            // load image file data into memory stream
            byte[] bytes  = File.ReadAllBytes(imgPath);
            var    memStm = pdfix.CreateMemStream();

            if (memStm == null)
            {
                throw new Exception(pdfix.GetError());
            }
            memStm.Write(0, bytes);

            // create XObject from the image
            var image_obj = doc.CreateXObjectFromImage(memStm, format);

            if (image_obj == null)
            {
                throw new Exception(pdfix.GetError());
            }

            // add annotation on the first page
            var page = doc.AcquirePage(0);

            if (page == null)
            {
                throw new Exception(pdfix.GetError());
            }

            var page_view = page.AcquirePageView(1, PdfRotate.kRotate0);

            if (page_view == null)
            {
                throw new Exception(pdfix.GetError());
            }

            // rect for the new annotation
            PdfRect annot_rect = new PdfRect()
            {
                left   = 100,
                right  = 300,
                bottom = 100,
                top    = 200
            };

            PdfAnnot annot = page.CreateAnnot(PdfAnnotSubtype.kAnnotStamp, annot_rect);

            page.AddAnnot(0, annot);
            // create content
            var content = doc.CreateContent();

            // add image to content
            var             xobjdict    = image_obj.GetStreamDict();
            var             width       = xobjdict.GetNumber("Width");
            var             height      = xobjdict.GetNumber("Height");
            var             ratio       = height / width;
            var             res_width   = annot_rect.right - annot_rect.left;
            var             res_height  = res_width * ratio;
            var             center_adj  = ((annot_rect.top - annot_rect.bottom) - res_height) / 2;
            var             imageobject = content.AddNewImage(-1, image_obj, new PdfMatrix(res_width, 0, 0, res_height, 0, center_adj));
            PdfGraphicState imageGs     = new PdfGraphicState();

            imageGs.color_state.fill_opacity = 255;
            imageobject.SetGState(imageGs);

            // create text state
            PdfTextState textState  = new PdfTextState();
            var          colorSpace = doc.CreateColorSpace(PdfColorSpaceFamily.kColorSpaceDeviceRGB);
            var          fontName   = "Segoe UI";
            var          fn         = fontName.Trim();
            var          sysFont    = pdfix.FindSysFont(fn, 0, PdfFontCodepage.kFontDefANSICodepage);

            if (sysFont == null)
            {
                throw new Exception(pdfix.GetError());
            }
            textState.font = doc.CreateFont(sysFont, PdfFontCharset.kFontAnsiCharset, 0);
            sysFont.Destroy();

            if (textState.font == null)
            {
                throw new Exception(pdfix.GetError());
            }

            textState.font_size             = 11;
            textState.color_state.fill_type = PdfFillType.kFillTypeSolid;
            var fill_color = colorSpace.CreateColor();

            fill_color.SetValue(0, 0.5f);
            fill_color.SetValue(1, 0.5f);
            fill_color.SetValue(2, 0.5f);
            textState.color_state.fill_color   = fill_color;
            textState.color_state.fill_opacity = 255;
            textState.color_state.stroke_type  = PdfFillType.kFillTypeSolid;
            var stroke_color = colorSpace.CreateColor();

            stroke_color.SetValue(0, 0);
            stroke_color.SetValue(1, 0);
            stroke_color.SetValue(2, 0);
            textState.color_state.stroke_color   = stroke_color;
            textState.color_state.stroke_opacity = 255;
            textState.char_spacing = 2;

            // add text to content
            var line       = "test text in appearance";
            var textObject = content.AddNewText(-1, textState.font, new PdfMatrix(1, 0, 0, 1, 10, 10));

            if (textObject == null)
            {
                throw new Exception(pdfix.GetError());
            }

            textObject.SetTextState(textState);
            textObject.SetText(line);

            // set annotation appearance
            PdsContentParams contentParams = new PdsContentParams();

            contentParams.bbox      = new PdfRect(annot_rect.right - annot_rect.left, 0, 0, annot_rect.top - annot_rect.bottom);
            contentParams.matrix    = new PdfMatrix(1, 0, 0, 1, 0, 0);
            contentParams.form_type = 1;
            contentParams.flags     = 2;
            var appearance_stream = content.ToObject(doc, contentParams);

            annot.SetAppearanceFromXObject(appearance_stream, PdfAnnotAppearanceMode.kAppearanceNormal);

            page.Release();

            // save document
            if (!doc.Save(savePath, Pdfix.kSaveFull))
            {
                throw new Exception(pdfix.GetError());
            }

            doc.Close();
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        public static void Run(
            String openPath,                            // source PDF document
            String savePath,                            // output PDF document
            String attachmentPath                       // attachment PDF
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

            PdfDoc doc = pdfix.OpenDoc(openPath, "");

            if (doc == null)
            {
                throw new Exception(pdfix.GetError());
            }

            // rect for the new annotation
            PdfRect annot_rect = new PdfRect()
            {
                left   = 300,
                right  = 328,
                bottom = 50,
                top    = 118
            };

            // create annotation dictionary and fill it
            PdsDictionary annot_dict  = doc.CreateDictObject(true);
            PdsArray      color_array = annot_dict.PutArray("C");

            color_array.PutNumber(0, 1);
            color_array.PutNumber(1, 0.33f);
            color_array.PutNumber(2, 0.25f);
            annot_dict.PutString("Contents", "AutoTag_Sample_original.pdf");
            annot_dict.PutNumber("F", 28);
            annot_dict.PutString("Name", "Paperclip");
            annot_dict.PutRect("Rect", annot_rect);
            annot_dict.PutString("Subj", "File Attachment");
            annot_dict.PutName("Subtype", "FileAttachment");
            annot_dict.PutString("T", "Mr PDFixer");
            annot_dict.PutName("Type", "Annot");

            PdsDictionary fs_dict = annot_dict.PutDict("FS");

            fs_dict.PutString("F", "AutoTag_Sample_original.pdf");
            fs_dict.PutName("Type", "Filespec");
            fs_dict.PutString("UF", "AutoTag_Sample_original.pdf");

            // open attachment doc
            var fileStm = pdfix.CreateFileStream(attachmentPath, PsFileMode.kPsReadOnly);

            if (fileStm == null)
            {
                throw new Exception(pdfix.GetError());
            }

            byte[] fileData = new byte[fileStm.GetSize()];
            fileStm.Read(0, fileData);

            // create stream object from attachment
            PdsStream filestream = doc.CreateStreamObject(true, null, fileData);

            PdsDictionary ef_dict = fs_dict.PutDict("EF");

            ef_dict.Put("F", filestream);

            // create annotation object from dictionary
            PdfAnnot annot = doc.GetAnnotFromObject(annot_dict);

            // add annotation on the first page
            var page = doc.AcquirePage(0);

            if (page == null)
            {
                throw new Exception(pdfix.GetError());
            }

            if (!page.AddAnnot(0, annot))
            {
                throw new Exception(pdfix.GetError());
            }

            page.Release();

            // save document
            if (!doc.Save(savePath, Pdfix.kSaveFull))
            {
                throw new Exception(pdfix.GetError());
            }

            doc.Close();
        }