コード例 #1
0
        public static void Run(
            String openPath,                            // source PDF document
            String savePath                             // dest PDF document
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;


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

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

            // cleanup any previous structure tree
            if (!doc.RemoveTags(null, null))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            // autotag document first
            if (!doc.AddTags(null, null))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            // get the struct tree
            PdsStructTree struct_tree = doc.GetStructTree();

            if (struct_tree == null)
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            // tag text on the bottom of the page as artifact
            for (int i = 0; i < struct_tree.GetNumChildren(); i++)
            {
                PdsObject        kid_obj  = struct_tree.GetChildObject(i);
                PdsStructElement kid_elem = struct_tree.GetStructElementFromObject(kid_obj);
                TagParagraphAsHeading(kid_elem);
            }

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

            doc.Close();
        }
コード例 #2
0
        public static void Run(
            String email,                               // authorization email
            String licenseKey,                          // authorization license key
            String openPath,                            // source PDF document
            String savePath,                            // output PDF document
            String imgPath,                             // watermark to apply
            PdfWatermarkParams watermarkParams          // watermark params
            )
        {
            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());
            }

            // set watermark params
            watermarkParams.page_range.start_page      = 1;
            watermarkParams.page_range.end_page        = 3;
            watermarkParams.page_range.page_range_spec = PdfPageRangeType.kEvenPagesOnly;
            watermarkParams.h_value = 10;
            watermarkParams.v_value = 10;
            watermarkParams.scale   = 0.5;
            watermarkParams.opacity = 0.5;

            if (!doc.AddWatermarkFromImage(watermarkParams, imgPath))
            {
                throw new Exception(pdfix.GetError());
            }

            if (!doc.Save(savePath, PdfSaveFlags.kSaveFull))
            {
                throw new Exception(pdfix.GetError());
            }

            doc.Close();
            pdfix.Destroy();
        }
コード例 #3
0
        public static void Run(
            String openPath,                            // source PDF document
            String savePath                             // dest PDF document
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

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

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

            // cleanup any previous structure tree
            if (!doc.RemoveTags(null, null))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            // autotag document first
            if (!doc.AddTags(null, null))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            // get the struct tree
            PdsStructTree struct_tree = doc.GetStructTree();

            if (struct_tree == null)
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            // move paragraph to the back of it's parent
            if (!MoveParagraphToParent(struct_tree))
            {
                throw new Exception("No table found.");
            }

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

            doc.Close();
        }
コード例 #4
0
        public static void Run(
            String email,                               // authorization email
            String licenseKey,                          // authorization license key
            String openPath                             // source PDF document
            )
        {
            pdfix = new Pdfix();
            if (pdfix == null)
            {
                throw new Exception("Pdfix initialization fail");
            }

            if (!pdfix.Authorize(email, licenseKey))
            {
                throw new Exception(pdfix.GetError());
            }

            doc = pdfix.OpenDoc(openPath, "");
            if (doc == null)
            {
                throw new Exception(pdfix.GetError());
            }

            String[] op = new string[4];
            op[0] = "count pages";
            op[1] = "remove annotations";
            op[2] = "place watermark";
            op[3] = "extract table";

            for (int j = 0; j < 4; j++)
            {
                t[j]      = new Thread(DoSomething);
                t[j].Name = op[j];
                t[j].Start();
            }

            for (int j = 0; j < 4; j++)
            {
                t[j].Join();
            }

            doc.Close();
            doc = null;
            pdfix.Destroy();
            pdfix = null;
        }
コード例 #5
0
        public static void Run(
            String email,                              // authorization email
            String licenseKey,                         // authorization license key
            String openPath,                           // source PDF document
            String savePath,                           // output TXT document
            String configPath                          // configuration file
            )
        {
            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());
            }

            StreamWriter file = new System.IO.StreamWriter(savePath);

            // iterate through pages and parse each page individually
            for (int i = 0; i < doc.GetNumPages(); i++)
            {
                PdfPage page = doc.AcquirePage(i);
                if (page == null)
                {
                    throw new Exception(pdfix.GetError());
                }
                ParsePage(pdfix, page, file);
                page.Release();
            }

            file.Close();

            doc.Close();
            pdfix.Destroy();
        }
コード例 #6
0
        public static void Run(
            String openPath,                            // source PDF document
            String savePath,                            // output PDF document
            String dataPath,                            // path to OCR data
            String language                             // default OCR language
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

            OcrTesseract ocr = new OcrTesseract();

            if (ocr == null)
            {
                throw new Exception("OcrTesseract initialization fail");
            }

            if (!ocr.Initialize(pdfix))
            {
                throw new Exception(pdfix.GetError());
            }

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

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

            ocr.SetLanguage(language);
            ocr.SetDataPath(dataPath);

            TesseractDoc ocrDoc = ocr.OpenOcrDoc(doc);

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


            //if (!ocrDoc.Save(savePath, ocrParams, null, null))
            //    throw new Exception(pdfix.GetError());

            ocrDoc.Close();
            doc.Close();
        }
コード例 #7
0
        public static void Run(
            String email,                               // authorization email
            String licenseKey,                          // authorization license key
            String openPath,                            // source PDF document
            String savePath,                            // output PDF document
            String language,                            // document reading language
            String title,                               // document title
            String configPath                           // configuration file
            )
        {
            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());
            }

            // convert to PDF/UA
            PdfAccessibleParams accParams = new PdfAccessibleParams();

            if (!doc.MakeAccessible(accParams, null, IntPtr.Zero))
            {
                throw new Exception(pdfix.GetError());
            }

            if (!doc.Save(savePath, PdfSaveFlags.kSaveFull))
            {
                throw new Exception(pdfix.GetError());
            }

            doc.Close();
            pdfix.Destroy();
        }
コード例 #8
0
        public static void Run(
            String openPath                             // source PDF document
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

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

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

            // cleanup any previous structure tree
            if (!doc.RemoveTags(null, null))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            // autotag document first
            if (!doc.AddTags(null, null))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            // read document structure tree
            PdsStructTree struct_tree = doc.GetStructTree();

            if (struct_tree == null)
            {
                Console.WriteLine("No Tags available");
            }
            else
            {
                for (var i = 0; i < struct_tree.GetNumChildren(); i++)
                {
                    PdsObject        kid_object  = struct_tree.GetChildObject(i);
                    PdsStructElement struct_elem = struct_tree.GetStructElementFromObject(kid_object);
                    ProcessStructElement(doc, struct_elem, "");
                }
            }


            doc.Close();
        }
コード例 #9
0
        public static void Run(
            String email,                       // authorization email
            String licenseKey,                  // authorization license key
            String openPath,                    // source PDF document
            String savePath
            )
        {
            Pdfix pdfix = new Pdfix();

            if (pdfix == null)
            {
                throw new Exception("Pdfix initialization fail");
            }
            _pdfix = pdfix;

            if (!pdfix.Authorize(email, licenseKey))
            {
                throw new Exception("Authorization fail. " + pdfix.GetError());
            }

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

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

            for (int i = 0; i < doc.GetNumPages(); i++)
            {
                var page = doc.AcquirePage(i);
                if (page == null)
                {
                    throw new Exception(pdfix.GetError());
                }
                for (int j = 0; j < page.GetNumPageObjects(); j++)
                {
                    ProcessPageObject(page, page.GetPageObject(i), savePath);
                }
                page.Release();
            }

            doc.Close();
            pdfix.Destroy();
        }
コード例 #10
0
        public static void Run(
            String email,                               // authorization email
            String licenseKey,                          // authorization license key
            String openPath,                            // source PDF document
            String savePath                             // output PDF document
            )
        {
            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());
            }

            if (!doc.RemoveTags(null, IntPtr.Zero))
            {
                throw new Exception(pdfix.GetError());
            }

            if (!doc.AddTags(null, IntPtr.Zero))
            {
                throw new Exception(pdfix.GetError());
            }

            if (!doc.Save(savePath, PdfSaveFlags.kSaveFull))
            {
                throw new Exception(pdfix.GetError());
            }

            doc.Close();
            pdfix.Destroy();
        }
コード例 #11
0
        public static void Run(
            String email,                               // authorization email
            String licenseKey,                          // authorization license key
            String openPath                             // source PDF document
            )
        {
            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());
            }

            var rootObj = doc.GetRootObject();
            var layers  = ReadOCGLayers.ReadLayerNames(rootObj);

            var page = doc.AcquirePage(0);

            foreach (KeyValuePair <string, int> layer in layers)
            {
                Console.WriteLine("Text in layer " + layer.Key + "(" + layer.Value.ToString() + ")");
                for (var i = 0; i < page.GetNumPageObjects(); i++)
                {
                    var page_obj = page.GetPageObject(i);
                    CheckPageObject(page_obj, layer);
                }
            }
            page.Release();
            doc.Close();
            pdfix.Destroy();
        }
コード例 #12
0
        public static void Run(
            String openPath                             // source PDF document
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

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

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

            PdsObject rootObj = doc.GetRootObject();

            ParseObject(rootObj, 1);

            doc.Close();
        }
コード例 #13
0
        public static void Run(
            String openPath,                            // source PDF document
            String savePath,                            // output XML document
            String configPath                           // configuration file
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

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

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

            StreamWriter file = new System.IO.StreamWriter(savePath);

            //pdfix.CreateCustomStream()

            // XML headers
            file.Write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            file.Write("<!--Created from PDF via PDFix-->\n");
            file.Write("<Document>\n");

            PsMetadata metadata = doc.GetMetadata();


            // iterate through pages and parse each page individually
            for (int i = 0; i < doc.GetNumPages(); i++)
            {
                PdfPage page = doc.AcquirePage(i);
                if (page == null)
                {
                    throw new Exception(pdfix.GetError());
                }
                ParsePage(pdfix, page, file);
                page.Release();
            }

            file.Close();

            doc.Close();
        }
コード例 #14
0
        public static void Run(
            String openPath,                            // source PDF document
            String savePath                             // dest PDF document
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

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

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


            // get the struct tree
            PdsStructTree struct_tree = doc.GetStructTree();

            if (struct_tree == null)
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            PdsStructElement figure = GetFirstFigure(struct_tree);

            if (figure == null)
            {
                throw new Exception("No figure found.");
            }

            if (!figure.SetAlt("This is a new alternate text"))
            {
                throw new Exception(pdfix.GetError());
            }

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

            doc.Close();
        }
コード例 #15
0
        public static void Run(
            String email,                               // authorization email
            String licenseKey,                          // authorization license key
            String openPath,                            // source PDF document
            String savePath,                            // output PDF document
            PdfFlattenAnnotsParams flattenAnnotsParams  // flatten annots params
            )
        {
            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());
            }

            if (!doc.FlattenAnnots(flattenAnnotsParams))
            {
                throw new Exception(pdfix.GetError());
            }

            if (!doc.Save(savePath, PdfSaveFlags.kSaveFull))
            {
                throw new Exception(pdfix.GetError());
            }

            doc.Close();
            pdfix.Destroy();
        }
コード例 #16
0
        public static void Run(
            String openPath,                            // source PDF document
            String savePath                             // output PDF document
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

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

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

            for (int i = 0; i < doc.GetNumPages(); i++)
            {
                var page = doc.AcquirePage(i);
                for (int j = page.GetNumAnnots() - 1; j >= 0; j--)
                {
                    var annot = page.GetAnnot(j);
                    if (annot == null || annot.GetSubtype() == PdfAnnotSubtype.kAnnotLink)
                    {
                        continue;
                    }
                    if (!page.FlattenAnnot(annot))
                    {
                        throw new Exception(pdfix.GetError());
                    }
                }
                page.SetContent();
                page.Release();
            }

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

            doc.Close();
        }
コード例 #17
0
        public static void Run(
            String openPath                     // source PDF document
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

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

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

            for (var i = 0; i < doc.GetNumFormFields(); i++)
            {
                var field = doc.GetFormField(i);
                GetFormFieldValue(field);
            }


            doc.Close();
        }
コード例 #18
0
        public static void Run(
            String email,                       // authorization email
            String licenseKey,                  // authorization license key
            String openPath                     // source PDF document
            )
        {
            Pdfix pdfix = new Pdfix();

            if (pdfix == null)
            {
                throw new Exception("Pdfix initialization fail");
            }
            _pdfix = pdfix;

            if (!pdfix.Authorize(email, licenseKey))
            {
                throw new Exception("Authorization fail. " + pdfix.GetError());
            }

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

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

            Console.WriteLine("detect form field tab order");
            ProcessFormFieldsViaPages(doc);

            Console.WriteLine("");
            Console.WriteLine("********************************************************");
            Console.WriteLine("");

            Console.WriteLine("map all document fields, some spccific proerties may ne inaccesscible");
            ProcessDocumentFormFields(doc);

            doc.Close();
            pdfix.Destroy();
        }
コード例 #19
0
        public static void Run(
            String openPath,                            // source PDF document
            String savePath,                            // output PDF document
            String pfxPath,                             // pfx file PKCS 12 certificate
            String pfxPassword                          // pfx password
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

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

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

            PdfDigSig digSig = pdfix.CreateDigSig();

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

            digSig.SetReason("Testing PDFix API");
            digSig.SetLocation("Location");
            digSig.SetContactInfo("*****@*****.**");
            if (!digSig.SetPfxFile(pfxPath, pfxPassword))
            {
                throw new Exception(pdfix.GetError());
            }
            if (!digSig.SignDoc(doc, savePath))
            {
                throw new Exception(pdfix.GetError());
            }
            digSig.Destroy();

            doc.Close();
        }
コード例 #20
0
        public static void Run(
            String email,                                           // authorization email
            String licenseKey                                       // license key
            )
        {
            Pdfix pdfix = new Pdfix();

            if (pdfix == null)
            {
                throw new Exception("Pdfix initialization fail. Please set correct email and license key.");
            }

            Console.WriteLine(pdfix.GetVersionMajor().ToString());

            if (licenseKey.Length > 0)
            {
                if (email.Length > 0)
                {
                    // Authorization using an account name/key
                    var account_auth = pdfix.GetAccountAuthorization();
                    if (account_auth.Authorize(email, licenseKey) == false)
                    {
                        throw new Exception("PDFix SDK Account Authorization failed");
                    }
                }
                else
                {
                    // Authorization using the activation key
                    var standard_auth = pdfix.GetStandardAuthorization();
                    if (!standard_auth.IsAuthorized() && !standard_auth.Activate(licenseKey))
                    {
                        throw new Exception("PDFix SDK Standard Authorization failed");
                    }
                }
            }

            pdfix.Destroy();
        }
コード例 #21
0
        ///////////////////////////////////////////////////////////////////////
        // ParsePage
        ///////////////////////////////////////////////////////////////////////
        private static void ParsePage(Pdfix pdfix, PdfPage page, String outDir)
        {
            // get pageMap for the current page
            PdePageMap pageMap = page.AcquirePageMap(null, IntPtr.Zero);

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

            // get page container
            PdeElement container = pageMap.GetElement();

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

            // parse children recursivelly
            ParseElement(container, outDir);

            pageMap.Release();
        }
コード例 #22
0
        public static void Run(
            String openPath                             // source PDF document
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

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

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

            var rootObj = doc.GetRootObject();
            var layers  = ReadLayerNames(rootObj);

            foreach (KeyValuePair <string, int> layer in layers)
            {
                Console.WriteLine(layer.Key + " : " + layer.Value.ToString());
            }

            doc.Close();
        }
コード例 #23
0
        public static void Run(
            String email,                               // authorization email
            String licenseKey,                          // authorization license key
            String openPath                             // source PDF document
            )
        {
            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());
            }

            var rootObj = doc.GetRootObject();
            var layers  = ReadLayerNames(rootObj);

            foreach (KeyValuePair <string, int> layer in layers)
            {
                Console.WriteLine(layer.Key + " : " + layer.Value.ToString());
            }

            doc.Close();
            pdfix.Destroy();
        }
コード例 #24
0
        public static void Run(String open_file)
        {
            Pdfix pdfix = PdfixEngine.Instance;

            // open doc using file stream
            var fileStm = pdfix.CreateFileStream(open_file, PsFileMode.kPsReadOnly);

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

            PdfDoc doc0 = pdfix.OpenDocFromStream(fileStm, "");

            if (doc0 == null)
            {
                throw new Exception(pdfix.GetError());
            }
            Console.WriteLine(@"Document open from file stream: {0} pages", doc0.GetNumPages());
            doc0.Close();

            // open doc using memory stream
            byte[] fileData = new byte[fileStm.GetSize()];
            fileStm.Read(0, fileData);

            var memStm = pdfix.CreateMemStream();

            memStm.Write(0, fileData);

            PdfDoc doc1 = pdfix.OpenDocFromStream(memStm, "");

            if (doc1 == null)
            {
                throw new Exception(pdfix.GetError());
            }
            Console.WriteLine(@"Document open from memory stream: {0} pages", doc1.GetNumPages());
            doc1.Close();
            memStm.Destroy();

            // open doc using custom stream
            PsStreamReadProc read_proc = (offset, buffer, size, client_data) =>
            {
                // read byte[] data from the source and copy to buffer
                var    stm = (((GCHandle)client_data).Target as PsStream);
                byte[] buf = new byte[size];
                var    ret = stm.Read(offset, buf);
                if (!ret)
                {
                    return(-1);
                }
                Marshal.Copy(buf, 0, buffer, size);
                return(size);
            };
            PsStreamGetSizeProc get_size_proc = (client_data) =>
            {
                // return the size of stream
                var stm = (((GCHandle)client_data).Target as PsStream);
                return(stm.GetSize());
            };

            var client_data_handle = GCHandle.Alloc(fileStm);
            var customStm          = pdfix.CreateCustomStream(read_proc, (IntPtr)client_data_handle);

            customStm.SetGetSizeProc(get_size_proc);

            PdfDoc doc2 = pdfix.OpenDocFromStream(customStm, "");

            if (doc2 == null)
            {
                throw new Exception(pdfix.GetError());
            }
            Console.WriteLine(@"Document open from custom stream: {0} pages", doc2.GetNumPages());
            doc2.Close();
            customStm.Destroy();
            client_data_handle.Free();

            fileStm.Destroy();
        }
コード例 #25
0
        public static void Run(
            String email,                               // authorization email
            String licenseKey,                          // authorization license key
            String openPath,                            // source PDF document
            String savePath                             // dest PDF document
            )
        {
            pdfix = new Pdfix();
            if (pdfix == null)
            {
                throw new Exception("Pdfix initialization fail");
            }

            if (!pdfix.Authorize(email, licenseKey))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

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

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

            // cleanup any previous structure tree
            if (!doc.RemoveTags(null, IntPtr.Zero))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            // autotag document first
            if (!doc.AddTags(null, IntPtr.Zero))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            // get the struct tree
            PdsStructTree struct_tree = doc.GetStructTree();

            if (struct_tree == null)
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            PdsStructElement paragraph = GetFirstParagraph(struct_tree);

            if (paragraph == null)
            {
                throw new Exception("No paragraph found.");
            }

            PdfRect annot_bbox = new PdfRect();

            GetStructElementBBox(paragraph, ref annot_bbox);

            // add new link annotation to the page
            PdfPage      page  = doc.AcquirePage(0);
            PdfLinkAnnot annot = page.AddLinkAnnot(0, annot_bbox);

            if (annot == null)
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            // re-tag the document the link annotation
            if (!doc.RemoveTags(null, IntPtr.Zero))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }
            if (!doc.AddTags(null, IntPtr.Zero))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            if (!doc.Save(savePath, PdfSaveFlags.kSaveFull))
            {
                throw new Exception(pdfix.GetError());
            }

            doc.Close();
            pdfix.Destroy();
        }
コード例 #26
0
        public static void Run(
            String openPath,                            // source PDF document
            String savePath                             // output image document
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

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

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

            // choose page to render
            var page = doc.AcquirePage(0);

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

            // prepare page view with zoom and rotation
            var pageView = page.AcquirePageView((float)1.0, PdfRotate.kRotate0);

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

            // prepare an image to write data into
            var image = pdfix.CreateImage(pageView.GetDeviceWidth(), pageView.GetDeviceHeight(),
                                          PsImageDIBFormat.kImageDIBFormatArgb);

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

            // draw content into the image
            if (!page.DrawContent(new PdfPageRenderParams()
            {
                clip_box = page.GetCropBox(),
                matrix = pageView.GetDeviceMatrix(),
                image = image
            },
                                  null, null))
            {
                throw new Exception(pdfix.GetError());
            }

            // prepare file stream to write into
            var stm = pdfix.CreateFileStream(savePath, PsFileMode.kPsTruncate);

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

            // save image to file
            if (!image.SaveToStream(stm, new PdfImageParams()
            {
                format = PdfImageFormat.kImageFormatJpg,
                quality = 80
            }))
            {
                throw new Exception(pdfix.GetError());
            }

            // cleanup
            stm.Destroy();
            page.Release();
            doc.Close();
        }
コード例 #27
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);
        }
コード例 #28
0
        public static void Run(
            String openPath,                            // source PDF document
            String configPath,                          // configuration file
            bool preflight                              // create document preflight
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

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

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

            var docTemplate = doc.GetTemplate();

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

            // load config if set
            if (configPath.Length > 0)
            {
                var stm = pdfix.CreateFileStream(configPath, PsFileMode.kPsReadOnly);
                if (stm != null)
                {
                    if (!docTemplate.LoadFromStream(stm, PsDataFormat.kDataFormatJson))
                    {
                        throw new Exception(pdfix.GetError());
                    }
                    stm.Destroy();
                }
            }

            if (preflight)
            {
                for (var i = 0; i < doc.GetNumPages(); i++)
                {
                    if (!docTemplate.AddPage(i, null, null))
                    {
                        throw new Exception(pdfix.GetError());
                    }
                }
                if (docTemplate.Update(null, null))
                {
                    throw new Exception(pdfix.GetError());
                }
            }

            // prepare the output JSON
            var docObj = new JObject();

            var dataType = new DataType();

            dataType.page_annots  = true;
            dataType.extract_bbox = true;

            ExtractDocumentData(doc, docObj, dataType);

            doc.Close();

            Console.Write(docObj.ToString());
        }
コード例 #29
0
        public static void Run(
            String email,                               // authorization email
            String licenseKey,                          // authorization license key
            String openPath,                            // source PDF document
            String savePath,                            // output PDF document
            String configPath,                          // configuration file
            PdfHtmlParams htmlParams                    // html conversion params
            )
        {
            Pdfix pdfix = new Pdfix();

            if (pdfix == null)
            {
                throw new Exception("Pdfix initialization fail");
            }

            if (!pdfix.Authorize(email, licenseKey))
            {
                throw new Exception(pdfix.GetError());
            }

            PdfToHtml pdfToHtml = new PdfToHtml();

            if (pdfToHtml == null)
            {
                throw new Exception("PdfToHtml initialization fail");
            }

            if (!pdfToHtml.Initialize(pdfix))
            {
                throw new Exception(pdfix.GetError());
            }

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

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

            // customize conversion
            PsFileStream stm = pdfix.CreateFileStream(configPath, PsFileMode.kPsReadOnly);

            if (stm != null)
            {
                PdfDocTemplate docTmpl = doc.GetDocTemplate();
                if (docTmpl == null)
                {
                    throw new Exception(pdfix.GetError());
                }
                if (!docTmpl.LoadFromStream(stm, PsDataFormat.kDataFormatJson))
                {
                    throw new Exception(pdfix.GetError());
                }
                stm.Destroy();
            }

            // set html conversion params
            //htmlParams.type = PdfHtmlType.kPdfHtmlResponsive;
            //htmlParams.width = 1200;
            //htmlParams.flags |= PdfToHtml.kHtmlExportJavaScripts;
            //htmlParams.flags |= PdfToHtml.kHtmlExportFonts;
            //htmlParams.flags |= PdfToHtml.kHtmlRetainFontSize;
            //htmlParams.flags |= PdfToHtml.kHtmlRetainTextColor;
            htmlParams.flags |= PdfToHtml.kHtmlNoExternalCSS | PdfToHtml.kHtmlNoExternalJS |
                                PdfToHtml.kHtmlNoExternalIMG | PdfToHtml.kHtmlNoExternalFONT;

            PdfHtmlDoc htmlDoc = pdfToHtml.OpenHtmlDoc(doc);

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

            if (!htmlDoc.Save(savePath, htmlParams, null, IntPtr.Zero))
            {
                throw new Exception(pdfix.GetError());
            }

            htmlDoc.Close();
            doc.Close();
            pdfToHtml.Destroy();
            pdfix.Destroy();
        }
コード例 #30
0
        public static void Run(
            String openPath,                            // source PDF document
            String savePath                             // dest PDF document
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

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

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

            // cleanup any previous structure tree
            if (!doc.RemoveTags(null, null))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            // autotag document first
            if (!doc.AddTags(null, null))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            // get the struct tree
            PdsStructTree struct_tree = doc.GetStructTree();

            if (struct_tree == null)
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            PdsStructElement table = GetFirstTable(struct_tree);

            if (table == null)
            {
                throw new Exception("No table found.");
            }

            PdfRect bbox = new PdfRect();

            GetStructElementBBox(table, ref bbox);

            // remove all items from the table to make it untagged cotnent
            for (int i = table.GetNumChildren() - 1; i >= 0; i--)
            {
                table.RemoveChild(i);
            }

            // tag page
            PdfPage page = doc.AcquirePage(0);

            PdePageMap page_map = page.AcquirePageMap();
            PdeElement elem     = page_map.CreateElement(PdfElementType.kPdeImage, null);

            elem.SetBBox(bbox);
            elem.SetAlt("This is image caption");

            // prepare document template to ignore already tagged content
            var doc_prelight = doc.GetTemplate();

            doc_prelight.SetProperty("ignore_tags", 1);

            // re-tag non-tagged page content
            PdePageMap pageMap = page.AcquirePageMap();

            if (pageMap == null)
            {
                throw new Exception(pdfix.GetError());
            }
            if (!pageMap.CreateElements(null, null))
            {
                throw new Exception(pdfix.GetError());
            }

            if (!page_map.AddTags(table, null, null))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

            // udpate the table element type
            if (!table.SetType("Sect"))
            {
                throw new Exception(pdfix.GetErrorType().ToString());
            }

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

            doc.Close();
        }