private static Annot setStamperImage(StamperImage stp)
        {
            var page = _currentDoc.GetPage(stp.Page());

            using (pdftron.PDF.Stamper s = new pdftron.PDF.Stamper(pdftron.PDF.Stamper.SizeType.e_relative_scale, .5, .5))
            {
                s.SetAsAnnotation(true);
                var rect = AnnotationsMannager.ConvertRect(stp.RectArea());
                _currentDoc.InitSecurityHandler();
                //pdftron.PDF.Image img = pdftron.PDF.Image.Create(_currentDoc, String.IsNullOrEmpty(stp.ImagePath()) ? "SuccessStamp.jpg" : stp.ImagePath());
                pdftron.PDF.Image img = pdftron.PDF.Image.Create(_currentDoc, System.Convert.FromBase64String(stp.Image()));
                s.SetTextAlignment(pdftron.PDF.Stamper.TextAlignment.e_align_center);
                s.SetAlignment(pdftron.PDF.Stamper.HorizontalAlignment.e_horizontal_left, pdftron.PDF.Stamper.VerticalAlignment.e_vertical_bottom);
                s.SetSize(pdftron.PDF.Stamper.SizeType.e_absolute_size, rect.x2 - rect.x1, rect.y2 - rect.y1);
                s.SetPosition(rect.x1, rect.y1);
                s.SetAsBackground(false);
                s.SetOpacity(.3);
                s.StampImage(_currentDoc, img, new PageSet(stp.Page()));
            }
            var annot = page.GetAnnot(page.GetNumAnnots() - 1);

            stp.RectArea(AnnotationsMannager.ConvertRect(annot.GetRect()));

            return(annot);
        }
        private void btnNovoPdf_Click(object sender, System.EventArgs e)
        {
            // Using PDFNet related classes and methods, must
            // catch or throw PDFNetException
            try
            {
                using (PDFDoc doc = new PDFDoc())
                {
                    using (Stamper s = new Stamper(Stamper.SizeType.e_relative_scale, 0.5, 0.5))
                    {
                        var randomNumber = new Random().Next(0, System.Convert.ToInt32(tbxNumMaxArquivos.Text));
                        doc.InitSecurityHandler();

                        // An example of creating a new page and adding it to
                        // doc's sequence of pages
                        Page newPg = doc.PageCreate();
                        doc.PagePushBack(newPg);

                        s.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_center, Stamper.VerticalAlignment.e_vertical_center);
                        s.SetFontColor(new ColorPt(1, 0, 0)); // set text color to red
                        s.StampText(doc, $"{tbxDefaultNewName.Text} document {randomNumber}", new PageSet(1, doc.GetPageCount()));

                        var caminhoDestino = chkGerarWatchFolder.Checked ? CONVERTER_DEFAULT_OUTPUT_PATH : fbdCaminhoPasta.SelectedPath;

                        // Save as a linearized file which is most popular
                        // and effective format for quick PDF Viewing.
                        doc.Save(caminhoDestino + $"\\{tbxDefaultNewName.Text}_{randomNumber}.pdf", SDFDoc.SaveOptions.e_linearized);
                    }
                }
            }
            catch (PDFNetException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        static void Main(string[] args)
        {
            PDFNet.Initialize();

            // Relative path to the folder containing test files.
            string input_path = "../../TestFiles/";

            try
            {
                Console.WriteLine("-------------------------------------------------");
                Console.WriteLine("Sample 1 - Extract text data from all pages in the document.");

                // Open the test file
                Console.WriteLine("Opening the input pdf...");
                using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf"))
                    using (ElementReader page_reader = new ElementReader())
                    {
                        doc.InitSecurityHandler();

                        PageIterator itr;
                        for (itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())                            //  Read every page
                        {
                            page_reader.Begin(itr.Current());
                            ProcessElements(page_reader);
                            page_reader.End();
                        }
                        Console.WriteLine("Done.");
                    }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }
Exemple #4
0
        public void RunPdfTron(string input_path)
        {
            PDFNet.Initialize();

            // string output_path = "../../../../TestFiles/Output/";

            try
            {
                // Open the test file
                PDFDoc doc = new PDFDoc(input_path);
                doc.InitSecurityHandler();

                PageIterator  itr;
                ElementReader page_reader = new ElementReader();

                for (itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())        //  Read every page
                {
                    int pageno = itr.GetPageNumber();


                    page_reader.Begin(itr.Current());
                    ProcessElements(page_reader);
                    page_reader.End();
                }

                page_reader.Dispose(); // Calling Dispose() on ElementReader/Writer/Builder can result in increased performance and lower memory consumption.
                doc.Close();
            }
            catch (PDFNetException e)
            {
                ConsoleLog += e.Message;
            }

            PDFNet.Terminate();
        }
Exemple #5
0
        public MainPageViewModel()
        {
            // Register commands
            CMDOpenFile   = new RelayCommand(OpenFile);
            CMDAddBarCode = new RelayCommand(AddBarcode);

            // Initialize PDFTron's SDK in demo mode
            pdftron.PDFNet.Initialize("");

            // Initialize PDF View Control
            PDFViewCtrl = new PDFViewCtrl();
            PDFViewCtrl.PointerPressed += PDFViewCtrl_PointerPressed;

            // Open getting started PDF file
            PDFDoc doc = new PDFDoc("Resources/GettingStarted.pdf");

            doc.InitSecurityHandler();

            // Load document into PDF View Control
            PDFViewCtrl.SetDoc(doc);

            // Init ToolManager
            toolManager = new ToolManager(PDFViewCtrl);

            // Init Dialog ViewModel
            barcodeViewModel = new BarcodeViewModel();
            BarcodeViewModel = new BarcodeDialogViewModel(new BarcodeDialogService(barcodeViewModel));
        }
Exemple #6
0
        static void Main(string[] args)
        {
            // Initialize PDFNet before using any PDFTron related
            // classes and methods (some exceptions can be found in API)
            PDFNet.Initialize();

            // Using PDFNet related classes and methods, must catch or throw PDFNetException
            try
            {
                using (PDFDoc doc = new PDFDoc())
                {
                    doc.InitSecurityHandler();

                    // An example of creating a new page and adding it to
                    // doc's sequence of pages
                    Page newPg = doc.PageCreate();
                    doc.PagePushBack(newPg);

                    // Save as a linearized file which is most popular
                    // and effective format for quick PDF Viewing.
                    doc.Save("linearized_output.pdf", SDFDoc.SaveOptions.e_linearized);

                    System.Console.WriteLine("Done. Results saved in linearized_output.pdf");
                }
            }
            catch (PDFNetException e)
            {
                System.Console.WriteLine(e);
            }
        }
        static void Main(string[] args)
        {
            PDFNet.Initialize();

            try
            {
                using (PDFDoc doc = new PDFDoc(input_path + "numbered.pdf"))
                {
                    doc.InitSecurityHandler();

                    // An example of using SDF/Cos API to add any type of annotations.
                    AnnotationLowLevelAPI(doc);
                    doc.Save(output_path + "annotation_test1.pdf", SDFDoc.SaveOptions.e_linearized);
                    System.Console.WriteLine("Done. Results saved in annotation_test1.pdf");

                    // An example of using the high-level PDFNet API to read existing annotations,
                    // to edit existing annotations, and to create new annotation from scratch.
                    AnnotationHighLevelAPI(doc);
                    doc.Save(output_path + "annotation_test2.pdf", SDFDoc.SaveOptions.e_linearized);
                    System.Console.WriteLine("Done. Results saved in annotation_test2.pdf");
                }

                // an example of creating various annotations in a brand new document
                using (PDFDoc doc1 = new PDFDoc())
                {
                    CreateTestAnnots(doc1);
                    doc1.Save(output_path + "new_annot_test_api.pdf", SDFDoc.SaveOptions.e_linearized);
                    System.Console.WriteLine("Saved new_annot_test_api.pdf");
                }
            }
            catch (PDFNetException e)
            {
                System.Console.WriteLine(e.Message);
            }
        }
Exemple #8
0
        private async Task <PDFDoc> OpenFilePDFViewer(IStorageFile file, FileAccessMode mode)
        {
            if (file == null)
            {
                return(null);
            }

            Windows.Storage.Streams.IRandomAccessStream stream;
            try
            {
                stream = await file.OpenAsync(mode);
            }
            catch (Exception e)
            {
                // NOTE: If file already opened it will cause an exception
                var msg = new MessageDialog(e.Message);
                _ = msg.ShowAsync();
                return(null);
            }

            PDFDoc doc = new PDFDoc(stream);

            doc.InitSecurityHandler();

            return(doc);
        }
Exemple #9
0
        async private void OpenFile()
        {
            FileOpenPicker filePicker = new FileOpenPicker();

            filePicker.ViewMode = PickerViewMode.List;
            filePicker.FileTypeFilter.Add(".pdf");

            StorageFile file = await filePicker.PickSingleFileAsync();

            if (file == null)
            {
                return;
            }

            Windows.Storage.Streams.IRandomAccessStream stream;
            try
            {
                stream = await file.OpenAsync(FileAccessMode.ReadWrite);
            }
            catch (Exception e)
            {
                // NOTE: If file already opened it will cause an exception
                var msg = new MessageDialog(e.Message);
                _ = msg.ShowAsync();
                return;
            }

            PDFDoc doc = new PDFDoc(stream);

            doc.InitSecurityHandler();

            // Set loaded doc to PDFView Controler
            PDFViewCtrl.SetDoc(doc);
        }
Exemple #10
0
        static void Main(string[] args)
        {
            PDFNet.Initialize();
            // Relative path to the folder containing test files.
            string input_path  = "../../TestFiles/";
            string output_path = "../../TestFiles/Output/";

            Console.WriteLine("_______________________________________________");
            Console.WriteLine("Opening the input pdf...");

            try             // Test  - Adjust the position of content within the page.
            {
                using (PDFDoc input_doc = new PDFDoc(input_path + "tiger.pdf"))
                {
                    input_doc.InitSecurityHandler();

                    Page pg        = input_doc.GetPage(1);
                    Rect media_box = pg.GetMediaBox();

                    media_box.x1 -= 200;                        // translate the page 200 units (1 uint = 1/72 inch)
                    media_box.x2 -= 200;

                    media_box.Update();

                    input_doc.Save(output_path + "tiger_shift.pdf", 0);
                }

                Console.WriteLine("Done. Result saved in tiger_shift...");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        private async Task OpenFilePDFViewer(IStorageFile file, FileAccessMode mode)
        {
            if (file == null)
            {
                return;
            }

            Windows.Storage.Streams.IRandomAccessStream stream;
            try
            {
                stream = await file.OpenAsync(mode);
            }
            catch (Exception e)
            {
                // NOTE: If file already opened it will cause an exception
                var msg = new MessageDialog(e.Message);
                _ = msg.ShowAsync();
                return;
            }

            PDFDoc doc = new PDFDoc(stream);

            doc.InitSecurityHandler();

            // Set loaded doc to PDFView Controler
            PDFViewCtrl.SetDoc(doc);

            ThumbnailViewer = new ThumbnailViewer(PDFViewCtrl, file.Path);
        }
 static void Redact(string input, string output, ArrayList rarr, Redactor.Appearance app)
 {
     using (PDFDoc doc = new PDFDoc(input))
     {
         doc.InitSecurityHandler();
         Redactor.Redact(doc, rarr, app, false, true);
         doc.Save(output, SDFDoc.SaveOptions.e_linearized);
     }
 }
Exemple #13
0
        static void Main(string[] args)
        {
            PDFNet.Initialize();

            // Create a PDF Package.
            try
            {
                using (PDFDoc doc = new PDFDoc())
                {
                    AddPackage(doc, input_path + "numbered.pdf", "My File 1");
                    AddPackage(doc, input_path + "newsletter.pdf", "My Newsletter...");
                    AddPackage(doc, input_path + "peppers.jpg", "An image");
                    AddCovePage(doc);
                    doc.Save(output_path + "package.pdf", SDFDoc.SaveOptions.e_linearized);
                    Console.WriteLine("Done.");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }

            // Extract parts from a PDF Package.
            try
            {
                using (PDFDoc doc = new PDFDoc(output_path + "package.pdf"))
                {
                    doc.InitSecurityHandler();

                    pdftron.SDF.NameTree files = NameTree.Find(doc, "EmbeddedFiles");
                    if (files.IsValid())
                    {
                        // Traverse the list of embedded files.
                        NameTreeIterator i = files.GetIterator();
                        for (int counter = 0; i.HasNext(); i.Next(), ++counter)
                        {
                            string entry_name = i.Key().GetAsPDFText();
                            Console.WriteLine("Part: {0}", entry_name);
                            FileSpec file_spec = new FileSpec(i.Value());
                            Filter   stm       = file_spec.GetFileData();
                            if (stm != null)
                            {
                                string fname = output_path + "extract_" + counter.ToString() + ".pdf";
                                stm.WriteToFile(fname, false);
                            }
                        }
                    }
                }

                Console.WriteLine("Done.");
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }
Exemple #14
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="pdfFile">The pdf file to be operating</param>
 public TableLocator(string pdfFile)
 {
     _pdfFile = pdfFile;
     pdfDoc   = new PDFDoc(_pdfFile);
     pdfDoc.InitSecurityHandler();
     pdfTronHelper     = new PdfTronHelper(pdfDoc);
     IsConsolidated    = true;
     ConsolidatedFound = true;
     ParentFound       = true;
 }
        static void Main(string[] args)
        {
            PDFNet.Initialize();

            // Relative path to the folder containing test files.
            string input_path      = "../../TestFiles/";
            string output_path     = "../../TestFiles/Output/";
            string input_filename  = "newsletter.pdf";
            string output_filename = "newsletter_edited.pdf";

            try
            {
                Console.WriteLine("Opening the input file...");
                using (PDFDoc doc = new PDFDoc(input_path + input_filename))
                {
                    doc.InitSecurityHandler();

                    ElementWriter writer  = new ElementWriter();
                    ElementReader reader  = new ElementReader();
                    XSet          visited = new XSet();

                    PageIterator itr = doc.GetPageIterator();

                    while (itr.HasNext())
                    {
                        try
                        {
                            Page page = itr.Current();
                            visited.Add(page.GetSDFObj().GetObjNum());

                            reader.Begin(page);
                            writer.Begin(page, ElementWriter.WriteMode.e_replacement, false, true, page.GetResourceDict());

                            ProcessElements(reader, writer, visited);
                            writer.End();
                            reader.End();

                            itr.Next();
                        }
                        catch (PDFNetException e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }

                    doc.Save(output_path + output_filename, SDFDoc.SaveOptions.e_remove_unused);
                    Console.WriteLine("Done. Result saved in {0}...", output_filename);
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        private void btnCombPdf_Click(object sender, EventArgs e)
        {
            ofdAbrirArquivo.Multiselect = true;

            if (ofdAbrirArquivo.ShowDialog() == DialogResult.OK)
            {
                var mergedFileName = GetFilePath(ofdAbrirArquivo.FileNames[0]) + string.Join('_', ofdAbrirArquivo.SafeFileNames.Select(x => x.TrimEnd(".pdf".ToCharArray())));

                try
                {
                    using (PDFDoc new_doc = new PDFDoc())
                    {
                        new_doc.InitSecurityHandler();

                        foreach (var input_doc in ofdAbrirArquivo.FileNames)
                        {
                            using (PDFDoc in_doc = new PDFDoc(input_doc))
                            {
                                in_doc.InitSecurityHandler();

                                new_doc.InsertPages(new_doc.GetPageCount() + 1, in_doc, 1, in_doc.GetPageCount(), PDFDoc.InsertFlag.e_none);
                            }
                        }

                        //Era pra funcionar, mas lança uma exceção no método "new_doc.ImportPages"

                        //ArrayList copy_pages = new ArrayList();

                        //for (PageIterator itr = in_doc.GetPageIterator(); itr.HasNext(); itr.Next())
                        //{
                        //    copy_pages.Add(itr.Current());
                        //}

                        //var imported_pages = new_doc.ImportPages(copy_pages, false);

                        //for (int i = 0; i != imported_pages.Count; ++i)
                        //{
                        //    new_doc.PagePushBack((Page)imported_pages[i]);
                        //}

                        new_doc.Save(GetNewFileName(mergedFileName, FileNameOptionEnum.Merge), SDFDoc.SaveOptions.e_linearized);
                    }
                }
                catch (PDFNetException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (PDFDoc doc = new PDFDoc())
                {
                    doc.InitSecurityHandler();

                    pdftron.PDF.Page newPg = doc.PageCreate();
                    doc.PagePushBack(newPg);

                    Ink ink = Ink.Create(doc, new pdftron.PDF.Rect(110, 10, 300, 200));
                    pdftron.PDF.Point pt3 = new pdftron.PDF.Point(110, 10);
                    //pt3.x = 110; pt3.y = 10;
                    ink.SetPoint(0, 0, pt3);
                    pt3.x = 150; pt3.y = 50;
                    ink.SetPoint(0, 1, pt3);
                    pt3.x = 190; pt3.y = 60;
                    ink.SetPoint(0, 2, pt3);
                    pt3.x = 180; pt3.y = 90;
                    ink.SetPoint(1, 0, pt3);
                    pt3.x = 190; pt3.y = 95;
                    ink.SetPoint(1, 1, pt3);
                    pt3.x = 200; pt3.y = 100;
                    ink.SetPoint(1, 2, pt3);
                    pt3.x = 166; pt3.y = 86;
                    ink.SetPoint(2, 0, pt3);
                    pt3.x = 196; pt3.y = 96;
                    ink.SetPoint(2, 1, pt3);
                    pt3.x = 221; pt3.y = 121;
                    ink.SetPoint(2, 2, pt3);
                    pt3.x = 288; pt3.y = 188;
                    ink.SetPoint(2, 3, pt3);
                    ink.SetColor(new ColorPt(0, 1, 1), 3);
                    newPg.AnnotPushBack(ink);

                    // Save as a linearized file which is most popular
                    // and effective format for quick PDF Viewing.
                    doc.Save("linearized_output.pdf", SDFDoc.SaveOptions.e_linearized);

                    System.Console.WriteLine("Done. Results saved in linearized_output.pdf");

                    MessageBox.Show("Done. Results saved in linearized_output.pdf", "Document Creation");
                }
            }
            catch (PDFNetException ex)
            {
                System.Console.WriteLine(ex.Message);
            }
        }
        private void btnCopiaPdf_Click(object sender, EventArgs e)
        {
            if (ofdAbrirArquivo.ShowDialog() == DialogResult.OK)
            {
                using (PDFDoc in_doc = new PDFDoc(ofdAbrirArquivo.FileName))
                {
                    using (PDFDoc copy_doc = new PDFDoc())
                    {
                        copy_doc.InitSecurityHandler();

                        copy_doc.InsertPages(copy_doc.GetPageCount() + 1, in_doc, 1, in_doc.GetPageCount(), PDFDoc.InsertFlag.e_none);

                        copy_doc.Save(GetNewFileName(ofdAbrirArquivo.FileName, FileNameOptionEnum.Copy), SDFDoc.SaveOptions.e_linearized);
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            try
            {
                PDFNet.Initialize();

                Console.WriteLine("-------------------------------------------------");
                Console.WriteLine("Extract page element information from all ");
                Console.WriteLine("pages in the document.");

                // Open the test file
                using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf"))
                {
                    doc.InitSecurityHandler();

                    int          pgnum = doc.GetPageCount();
                    PageIterator itr;

                    using (ElementReader page_reader = new ElementReader())
                    {
                        for (itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())                                    //  Read every page
                        {
                            Console.WriteLine("Page {0:d}----------------------------------------",
                                              itr.GetPageNumber());

                            Rect crop_box = itr.Current().GetCropBox();
                            crop_box.Normalize();

                            // Console.WriteLine(" Page Rectangle: x={0:f} y={1:f} x2={2:f} y2={3:f}", crop_box.x1, crop_box.y1, crop_box.x2, crop_box.y2);
                            // Console.WriteLine(" Page Size: width={0:f} height={1:f}", crop_box.Width(), crop_box.Height());

                            page_reader.Begin(itr.Current());
                            ProcessElements(page_reader);
                            page_reader.End();
                        }
                    }

                    Console.WriteLine("Done.");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        /// <summary>
        /// Convert a document to PDF format
        /// </summary>
        async private void ConvertFile()
        {
            FileOpenPicker filePicker = new FileOpenPicker();

            filePicker.ViewMode = PickerViewMode.List;
            filePicker.FileTypeFilter.Add(".doc");
            filePicker.FileTypeFilter.Add(".docx");

            StorageFile file = await filePicker.PickSingleFileAsync();

            if (file == null)
            {
                return;
            }

            Windows.Storage.Streams.IRandomAccessStream stream;
            try
            {
                stream = await file.OpenAsync(FileAccessMode.ReadWrite);
            }
            catch (Exception e)
            {
                // NOTE: If file already opened it will cause an exception
                var msg = new MessageDialog(e.Message);
                _ = msg.ShowAsync();
                return;
            }

            // Convert Logic
            IFilter            filter     = new RandomAccessStreamFilter(stream);
            WordToPDFOptions   opts       = new WordToPDFOptions();
            DocumentConversion conversion = pdftron.PDF.Convert.UniversalConversion(filter, opts);

            var convRslt = conversion.TryConvert();

            if (convRslt == DocumentConversionResult.e_document_conversion_success)
            {
                PDFDoc doc = conversion.GetDoc();
                doc.InitSecurityHandler();

                PDFViewCtrl.SetDoc(doc);

                ThumbnailViewer = new ThumbnailViewer(PDFViewCtrl, file.Path);
            }
        }
        private static void WatermarkFile(PDFDoc doc, WaterMarkInfo watermarkInfo)
        {
            using (var stamp = new Stamper(Stamper.SizeType.e_relative_scale, 1, 1))
            {
                doc.InitSecurityHandler();

                stamp.SetAlignment(Stamper.HorizontalAlignment.e_horizontal_center, Stamper.VerticalAlignment.e_vertical_center);
                stamp.SetFontColor(new ColorPt(0, 0, 0)); // set text color to red
                stamp.SetOpacity(0.1);
                stamp.SetRotation(-67);
                stamp.ShowsOnPrint(true);
                stamp.ShowsOnScreen(true);
                stamp.SetAsBackground(false);
                stamp.StampText(doc, watermarkInfo.CustomMessage + "\n"
                                + watermarkInfo.UserIPAddress,
                                new PageSet(1, doc.GetPageCount()));
            }
        }
        public string Extract(string path)
        {
            var builder = new StringBuilder();

            using (var doc = new PDFDoc(path))
            {
                doc.InitSecurityHandler();

                using (TextExtractor txt = new TextExtractor())
                {
                    PageIterator itr = doc.GetPageIterator();
                    for (; itr.HasNext(); itr.Next())
                    {
                        txt.Begin(itr.Current());
                        builder.AppendLine(txt.GetAsText());
                    }
                }
            }
            return(builder.ToString());
        }
Exemple #23
0
        private void StartExtract2()
        {
            List <List <string> > bulkFileFilter = null;
            List <LineFound>      bulkFile       = null;

            pdftron.PDFNet.Initialize("Reuters Technology China Ltd.(thomsonreuters.com):CPU:1::W:AMC(20121010):AD5EE33F2505D1CAF1B425461F9C92BAA89204FA0AD8AAA17E07887EF0FA");
            PDFDoc doc = new PDFDoc(config.FilePath2);

            doc.InitSecurityHandler();
            string    patternTitle = @"コード";
            int       page         = 5;
            PdfString ricPosition  = GetRicPosition(doc, patternTitle, page);

            if (ricPosition == null)
            {
                return;
            }

            ricPosition.Position.x1 = 106.8;
            ricPosition.Position.x2 = 99.12;
            ricPosition.Position.y1 = 29.5105;
            ricPosition.Position.y2 = 44.8734;
            string patternRic = @"\d{4}";
            //string patternValue = @"\-?(\,\.\d)*";
            string patternValue = @"(\-|\+)?\d+(\,|\.|\d)+";

            bulkFile = GetValue(doc, ricPosition, patternRic, patternValue);
            int indexOK = 0;

            bulkFileFilter = FilterBulkFile(bulkFile, indexOK);
            string filePath = Path.Combine(config.OutputFolder, string.Format("{0}.csv", Path.GetFileNameWithoutExtension(config.FilePath2)));

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            XlsOrCsvUtil.GenerateStringCsv(filePath, bulkFileFilter);
            AddResult(Path.GetFileNameWithoutExtension(filePath), filePath, "type2");
        }
        /// <summary>
        /// It open dialog to load a PDF File
        /// </summary>
        private void OpenDocument()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (openFileDialog.ShowDialog() == true)
            {
                try
                {
                    var doc = new PDFDoc(openFileDialog.FileName);
                    doc.InitSecurityHandler();

                    PDFViewer.CurrentDocument = doc;
                    _undoManager = doc.GetUndoManager(); // Get document Undo Redo Manager

                    NotifyPropertyChanged(nameof(ToolsEnabled));
                }
                catch (Exception)
                {
                    throw new Exception("OpenDocument() failed");
                }
            }
        }
Exemple #25
0
        public void ReadTextFromCoordinates(string input_path, int pagenumber, int urx, int ury, int llx, int lly)
        {
            PDFDoc doc = new PDFDoc(input_path);

            doc.InitSecurityHandler();
            Page          page   = doc.GetPage(pagenumber);
            ElementReader reader = new ElementReader();
            PageIterator  itr    = doc.GetPageIterator();

            reader.Begin(itr.Current());

            LowLevelTextExtractUtils u = new LowLevelTextExtractUtils();
            //u.DumpAllText(reader);
            //ConsoleLog += u.ConsoleLog;
            //reader.End();

            string field3 = u.ReadTextFromRect(page, new Rect(urx, ury, llx, lly), reader);

            ConsoleLog = field3;

            reader.Dispose();
            doc.Close();
        }
        // POST: api/Annotations
        public void Post([FromBody]Annotations value)
        {
            DocumentRepository documentData = new DocumentRepository();
            CurrentDocuments currentDocuments = documentData.GetCurrentDocumentPathPDF(Utility.GetUserName());
            string [] pathParts = currentDocuments.PathToCurrentPDFDocument.Split('\\');
            string tempPDFPath = string.Empty;

            for (int i = 0; i < pathParts.Length - 1 ; i++)
            {
                tempPDFPath = tempPDFPath + pathParts[i] + "\\";
            }

            tempPDFPath = tempPDFPath + "temp" + pathParts[pathParts.Length -1];

            System.IO.File.Copy(currentDocuments.PathToCurrentPDFDocument, tempPDFPath,true);
            try
            {

                PDFDoc in_doc = new PDFDoc(tempPDFPath);
                {
                    in_doc.InitSecurityHandler();

                    //string str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><xfdf xmlns=\"http://ns.adobe.com/xfdf\" xml:space=\"preserve\"><square subject=\"Rectangle\" page=\"0\" name=\"cf4d2e58-e9c5-2a58-5b4d-9b4b1a330e45\" title=\"user\" creationdate=\"D:20120827112326-07'00'\" date=\"D:20120827112326-07'00'\" rect=\"227.7814207650273,597.6174863387978,437.07103825136608,705.0491803278688\" color=\"#000000\" interior-color=\"#FFFF00\" flags=\"print\" width=\"1\"><popup flags=\"print,nozoom,norotate\" open=\"no\" page=\"0\" rect=\"0,792,0,792\" /></square></xfdf>";
                    string str = value.Data;

                    using (FDFDoc fdoc = new FDFDoc(FDFDoc.CreateFromXFDF(str)))
                    {
                        in_doc.FDFMerge(fdoc);
                        in_doc.Save(currentDocuments.PathToCurrentPDFDocument, SDFDoc.SaveOptions.e_linearized);
                    }
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        // POST: api/Annotations
        public void Post([FromBody]Annotations value)
        {
            DocumentRepository documentData = new DocumentRepository();
            CurrentDocuments currentDocuments = documentData.GetCurrentDocumentPathPdf(Utility.GetUserName());
            string [] pathParts = currentDocuments.PathToCurrentPdfDocument.Split('\\');
            string tempPdfPath = string.Empty;

            for (int i = 0; i < pathParts.Length - 1 ; i++)
            {
                tempPdfPath = tempPdfPath + pathParts[i] + "\\";
            }

            tempPdfPath = tempPdfPath + "temp" + pathParts[pathParts.Length -1];

            System.IO.File.Copy(currentDocuments.PathToCurrentPdfDocument, tempPdfPath,true);
            try
            {
                PDFDoc inDoc = new PDFDoc(tempPdfPath);
                {
                   inDoc.InitSecurityHandler();

                    string str = value.Data;

                    using (FDFDoc fdoc = new FDFDoc(FDFDoc.CreateFromXFDF(str)))
                    {
                        inDoc.FDFMerge(fdoc);
                        inDoc.FlattenAnnotations();
                        inDoc.Save(currentDocuments.PathToCurrentPdfDocument, SDFDoc.SaveOptions.e_incremental);
                        //inDoc.Save(currentDocuments.PathToCurrentPdfDocument, SDFDoc.SaveOptions.e_linearized);
                    }
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        /// <summary>
        /// Genera la factura de la reserva id pasada como parametro, si la factura pertenece al socio y si ha pagado la reserva, y la devuelve al socio
        /// </summary>
        /// <param name="id">id de la reserva para la cual mostrar la factura</param>
        /// <returns>pdf con la factura si socio, sino redirige a /Home/index o /Admin/index dependiendo si es admin o no logueado</returns>
        public ActionResult VerFacturaReserva(int? id)
        {
            if (isSocio())
            {
                reservas reserva = db.reservas.Find(id);
                if (reserva == null)
                {
                    addError("No tiene permisos para visualizar esta reserva");
                    saveErrors();
                    return RedirectToAction("MisReservas", "Socio");
                }
                string socio_id = (string)Session["UserID"];
                if (reserva.socios.id != socio_id)
                {
                    addError("No tiene permisos para visualizar esta reserva");
                    saveErrors();
                    return RedirectToAction("MisReservas", "Socio");
                }
                if (reserva.pagado == false)
                {
                    addError("La factura no puede ser visualizada hasta que la reserva sea abonada");
                    saveErrors();
                    return RedirectToAction("MisReservas", "Socio");
                }

                // Creacion del pdf a partir de los datos xml en la BBDD
                PDFNet.Initialize();

                // Ruta relavita a las carpetas que contienen los archivos
                string input_path = "c:/Google Drive/PFC/pdf/";
                string output_path = "c:/Google Drive/PFC/pdf/Output/";
                try
                {
                    // Juntar XFDF desde el xml string
                    PDFDoc in_doc = new PDFDoc(input_path + "factura.pdf");
                    {
                        in_doc.InitSecurityHandler();

                        //Debug.WriteLine("Juntamos XFDF string con el PDF...");

                        //reservas reserva = db.reservas.Find(id);
                        string str = reserva.facturas.xml_factura;
                        //Debug.WriteLine(str);

                        using (FDFDoc fdoc = new FDFDoc(FDFDoc.CreateFromXFDF(str)))
                        {
                            in_doc.FDFMerge(fdoc);
                            // Iniciamos los permisos del pdf, para que no se pueda editar
                            StdSecurityHandler newHandler = new StdSecurityHandler();
                            newHandler.SetPermission(SecurityHandler.Permission.e_doc_modify, false);
                            newHandler.SetPermission(SecurityHandler.Permission.e_fill_forms, false);
                            newHandler.SetPermission(SecurityHandler.Permission.e_extract_content, false);
                            newHandler.SetPermission(SecurityHandler.Permission.e_mod_annot, false);

                            in_doc.SetSecurityHandler(newHandler);
                            in_doc.Save(output_path + "factura_modificada.pdf", SDFDoc.SaveOptions.e_linearized);
                            Debug.WriteLine("Juntado completado.");
                        }
                    }
                }
                catch (PDFNetException e)
                {
                    Debug.WriteLine(e.Message);
                }

                return File("c:/Google drive/PFC/pdf/output/factura_modificada.pdf", "application/pdf");
            }
            else
                return RedirectToAction("Index", isAdmin() ? "Admin" : "Home");
        }
        static void Main(string[] args)
        {
            PDFNet.Initialize();

            // Relative path to the folder containing test files.
            string input_path  = "../../TestFiles/";
            string output_path = "../../TestFiles/Output/";

            try
            {
                Console.WriteLine("-------------------------------------------------");
                Console.WriteLine("Opening the input pdf...");
                using (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf"))
                {
                    in_doc.InitSecurityHandler();

                    // Create a list of pages to import from one PDF document to another.
                    ArrayList import_list = new ArrayList();
                    for (PageIterator itr = in_doc.GetPageIterator(); itr.HasNext(); itr.Next())
                    {
                        import_list.Add(itr.Current());
                    }

                    using (PDFDoc new_doc = new PDFDoc())                     //  Create a new document
                        using (ElementBuilder builder = new ElementBuilder())
                            using (ElementWriter writer = new ElementWriter())
                            {
                                ArrayList imported_pages = new_doc.ImportPages(import_list);

                                // Paper dimension for A3 format in points. Because one inch has
                                // 72 points, 11.69 inch 72 = 841.69 points
                                Rect   media_box = new Rect(0, 0, 1190.88, 841.69);
                                double mid_point = media_box.Width() / 2;

                                for (int i = 0; i < imported_pages.Count; ++i)
                                {
                                    // Create a blank new A3 page and place on it two pages from the input document.
                                    Page new_page = new_doc.PageCreate(media_box);
                                    writer.Begin(new_page);

                                    // Place the first page
                                    Page    src_page = (Page)imported_pages[i];
                                    Element element  = builder.CreateForm(src_page);

                                    double sc_x  = mid_point / src_page.GetPageWidth();
                                    double sc_y  = media_box.Height() / src_page.GetPageHeight();
                                    double scale = Math.Min(sc_x, sc_y);
                                    element.GetGState().SetTransform(scale, 0, 0, scale, 0, 0);
                                    writer.WritePlacedElement(element);

                                    // Place the second page
                                    ++i;
                                    if (i < imported_pages.Count)
                                    {
                                        src_page = (Page)imported_pages[i];
                                        element  = builder.CreateForm(src_page);
                                        sc_x     = mid_point / src_page.GetPageWidth();
                                        sc_y     = media_box.Height() / src_page.GetPageHeight();
                                        scale    = Math.Min(sc_x, sc_y);
                                        element.GetGState().SetTransform(scale, 0, 0, scale, mid_point, 0);
                                        writer.WritePlacedElement(element);
                                    }

                                    writer.End();
                                    new_doc.PagePushBack(new_page);
                                }
                                new_doc.Save(output_path + "newsletter_booklet.pdf", SDFDoc.SaveOptions.e_linearized);
                                Console.WriteLine("Done. Result saved in newsletter_booklet.pdf...");
                            }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception caught:\n{0}", e);
            }
        }
Exemple #30
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            // Initialize PDFNet before calling any other PDFNet function.
            PDFNet.Initialize();

            string input_path     = "../../TestFiles/";
            string output_path    = "../../TestFiles/Output/";
            string input_filename = "US061222892-a.pdf";

            PDFDoc pdf_doc = new PDFDoc(input_path + input_filename);

            pdf_doc.InitSecurityHandler();

            SDFDoc cos_doc  = pdf_doc.GetSDFDoc();
            int    num_objs = cos_doc.XRefSize();

            for (int i = 1; i < num_objs; ++i)
            {
                Obj obj = cos_doc.GetObj(i);
                if (obj != null && !obj.IsFree() && obj.IsStream())
                {
                    // Process only images
                    DictIterator itr = obj.Find("Subtype");
                    if (!itr.HasNext() || itr.Value().GetName() != "Image")
                    {
                        continue;
                    }

                    pdftron.PDF.Image input_image = new pdftron.PDF.Image(obj);
                    pdftron.PDF.Image new_image   = null;

                    // Process only gray-scale images
                    if (input_image.GetComponentNum() != 1)
                    {
                        continue;
                    }

                    int bpc = input_image.GetBitsPerComponent();
                    if (bpc != 1)                     // Recompress 1 BPC images
                    {
                        continue;
                    }

                    // Skip images that are already compressed using JBIG2
                    itr = obj.Find("Filter");
                    if (itr.HasNext() && itr.Value().IsName() &&
                        itr.Value().GetName() == "JBIG2Decode")
                    {
                        continue;
                    }

                    FilterReader reader = new FilterReader(obj.GetDecodedStream());

                    ObjSet hint_set = new ObjSet();
                    Obj    hint     = hint_set.CreateArray();
                    hint.PushBackName("JBIG2");
                    hint.PushBackName("Lossless");
                    hint.PushBackName("Threshold");
                    hint.PushBackNumber(0.4);
                    hint.PushBackName("SharePages");
                    hint.PushBackNumber(10000);

                    new_image = pdftron.PDF.Image.Create(
                        cos_doc,
                        reader,
                        input_image.GetImageWidth(),
                        input_image.GetImageHeight(),
                        1,
                        ColorSpace.CreateDeviceGray(),
                        hint                          // A hint to image encoder to use JBIG2 compression
                        );

                    Obj new_img_obj = new_image.GetSDFObj();

                    // Copy any important entries from the image dictionary
                    itr = obj.Find("ImageMask");
                    if (itr.HasNext())
                    {
                        new_img_obj.Put("ImageMask", itr.Value());
                    }

                    itr = obj.Find("Mask");
                    if (itr.HasNext())
                    {
                        new_img_obj.Put("Mask", itr.Value());
                    }

                    cos_doc.Swap(i, new_image.GetSDFObj().GetObjNum());
                }
            }

            pdf_doc.Save(output_path + "US061222892_JBIG2.pdf", SDFDoc.SaveOptions.e_remove_unused);
            pdf_doc.Close();
        }
        static void Main(string[] args)
        {
            PDFNet.Initialize();

            // Relative path to the folder containing test files.
            string input_path = "../../TestFiles/";

            bool example1_basic     = false;
            bool example2_xml       = false;
            bool example3_wordlist  = false;
            bool example4_advanced  = true;
            bool example5_low_level = false;

            // Sample code showing how to use high-level text extraction APIs.
            try
            {
                using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf"))
                {
                    doc.InitSecurityHandler();

                    Page page = doc.GetPage(1);
                    if (page == null)
                    {
                        Console.WriteLine("Page not found.");
                        return;
                    }

                    using (TextExtractor txt = new TextExtractor())
                    {
                        txt.Begin(page);                          // Read the page.
                        // Other options you may want to consider...
                        // txt.Begin(page, null, TextExtractor.ProcessingFlags.e_no_dup_remove);
                        // txt.Begin(page, null, TextExtractor.ProcessingFlags.e_remove_hidden_text);
                        // ...

                        // Example 1. Get all text on the page in a single string.
                        // Words will be separated with space or new line characters.
                        if (example1_basic)
                        {
                            // Get the word count.
                            Console.WriteLine("Word Count: {0}", txt.GetWordCount());

                            Console.WriteLine("\n\n- GetAsText --------------------------\n{0}", txt.GetAsText());
                            Console.WriteLine("-----------------------------------------------------------");
                        }

                        // Example 2. Get XML logical structure for the page.
                        if (example2_xml)
                        {
                            String text = txt.GetAsXML(TextExtractor.XMLOutputFlags.e_words_as_elements | TextExtractor.XMLOutputFlags.e_output_bbox | TextExtractor.XMLOutputFlags.e_output_style_info);
                            Console.WriteLine("\n\n- GetAsXML  --------------------------\n{0}", text);
                            Console.WriteLine("-----------------------------------------------------------");
                        }

                        // Example 3. Extract words one by one.
                        if (example3_wordlist)
                        {
                            TextExtractor.Word word;
                            for (TextExtractor.Line line = txt.GetFirstLine(); line.IsValid(); line = line.GetNextLine())
                            {
                                for (word = line.GetFirstWord(); word.IsValid(); word = word.GetNextWord())
                                {
                                    Console.WriteLine(word.GetString());
                                }
                            }
                            Console.WriteLine("-----------------------------------------------------------");
                        }

                        // Example 3. A more advanced text extraction example.
                        // The output is XML structure containing paragraphs, lines, words,
                        // as well as style and positioning information.
                        if (example4_advanced)
                        {
                            Rect bbox;
                            int  cur_flow_id = -1, cur_para_id = -1;

                            TextExtractor.Line  line;
                            TextExtractor.Word  word;
                            TextExtractor.Style s, line_style;

                            Console.WriteLine("<PDFText>");
                            // For each line on the page...
                            for (line = txt.GetFirstLine(); line.IsValid(); line = line.GetNextLine())
                            {
                                if (line.GetNumWords() == 0)
                                {
                                    continue;
                                }

                                if (cur_flow_id != line.GetFlowID())
                                {
                                    if (cur_flow_id != -1)
                                    {
                                        if (cur_para_id != -1)
                                        {
                                            cur_para_id = -1;
                                            Console.WriteLine("</Para>");
                                        }
                                        Console.WriteLine("</Flow>");
                                    }
                                    cur_flow_id = line.GetFlowID();
                                    Console.WriteLine("<Flow id=\"{0}\">", cur_flow_id);
                                }

                                if (cur_para_id != line.GetParagraphID())
                                {
                                    if (cur_para_id != -1)
                                    {
                                        Console.WriteLine("</Para>");
                                    }
                                    cur_para_id = line.GetParagraphID();
                                    Console.WriteLine("<Para id=\"{0}\">", cur_para_id);
                                }

                                bbox       = line.GetBBox();
                                line_style = line.GetStyle();
                                Console.Write("<Line box=\"{0}, {1}, {2}, {3}\"", bbox.x1.ToString("0.00"), bbox.y1.ToString("0.00"), bbox.x2.ToString("0.00"), bbox.y2.ToString("0.00"));
                                PrintStyle(line_style);
                                Console.Write(" cur_num=\"" + line.GetCurrentNum() + "\"" + ">\n");

                                // For each word in the line...
                                for (word = line.GetFirstWord(); word.IsValid(); word = word.GetNextWord())
                                {
                                    // Output the bounding box for the word.
                                    bbox = word.GetBBox();
                                    Console.Write("<Word box=\"{0}, {1}, {2}, {3}\"", bbox.x1.ToString("0.00"), bbox.y1.ToString("0.00"), bbox.x2.ToString("0.00"), bbox.y2.ToString("0.00"));
                                    Console.Write(" cur_num=\"" + word.GetCurrentNum() + "\"");
                                    int sz = word.GetStringLen();
                                    if (sz == 0)
                                    {
                                        continue;
                                    }

                                    // If the word style is different from the parent style, output the new style.
                                    s = word.GetStyle();
                                    if (s != line_style)
                                    {
                                        PrintStyle(s);
                                    }

                                    Console.Write(">{0}", word.GetString());
                                    Console.WriteLine("</Word>");
                                }
                                Console.WriteLine("</Line>");
                            }

                            if (cur_flow_id != -1)
                            {
                                if (cur_para_id != -1)
                                {
                                    cur_para_id = -1;
                                    Console.WriteLine("</Para>");
                                }
                                Console.WriteLine("</Flow>");
                            }
                        }
                    }
                    Console.WriteLine("</PDFText>");
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }

            // Sample code showing how to use low-level text extraction APIs.
            if (example5_low_level)
            {
                try
                {
                    LowLevelTextExtractUtils util = new LowLevelTextExtractUtils();
                    using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf"))
                    {
                        doc.InitSecurityHandler();

                        // Example 1. Extract all text content from the document
                        using (ElementReader reader = new ElementReader())
                        {
                            PageIterator itr = doc.GetPageIterator();
                            //for (; itr.HasNext(); itr.Next()) //  Read every page
                            {
                                reader.Begin(itr.Current());
                                LowLevelTextExtractUtils.DumpAllText(reader);
                                reader.End();
                            }

                            // Example 2. Extract text based on the selection rectangle.
                            Console.WriteLine("----------------------------------------------------");
                            Console.WriteLine("Extract text based on the selection rectangle.");
                            Console.WriteLine("----------------------------------------------------");

                            Page   first_page = doc.GetPage(1);
                            string field1     = util.ReadTextFromRect(first_page, new Rect(27, 392, 563, 534), reader);
                            string field2     = util.ReadTextFromRect(first_page, new Rect(28, 551, 106, 623), reader);
                            string field3     = util.ReadTextFromRect(first_page, new Rect(208, 550, 387, 621), reader);

                            Console.WriteLine("Field 1: {0}", field1);
                            Console.WriteLine("Field 2: {0}", field2);
                            Console.WriteLine("Field 3: {0}", field3);
                            // ...

                            Console.WriteLine("Done.");
                        }
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            // The first step in every application using PDFNet is to initialize the
            // library and set the path to common PDF resources. The library is usually
            // initialized only once, but calling Initialize() multiple times is also fine.
            PDFNet.Initialize();

            try
            {
                // Optional: Set ICC color profiles to fine tune color conversion
                // for PDF 'device' color spaces. You can use your own ICC profiles.
                // Standard Adobe color profiles can be download from Adobes site:
                // http://www.adobe.com/support/downloads/iccprofiles/iccprofiles_win.html
                //
                // Simply drop all *.icc files in PDFNet resource folder or you specify
                // the full pathname.
                //---
                // PDFNet.SetResourcesPath("../../../../../resources");
                // PDFNet.SetColorManagement();
                // PDFNet.SetDefaultDeviceCMYKProfile("USWebCoatedSWOP.icc"); // will search in PDFNet resource folder.
                // PDFNet.SetDefaultDeviceRGBProfile("AdobeRGB1998.icc");

                // Optional: Set predefined font mappings to override default font
                // substitution for documents with missing fonts. For example:
                //---
                // PDFNet.AddFontSubst("StoneSans-Semibold", "C:/WINDOWS/Fonts/comic.ttf");
                // PDFNet.AddFontSubst("StoneSans", "comic.ttf");  // search for 'comic.ttf' in PDFNet resource folder.
                // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Identity, "C:/WINDOWS/Fonts/arialuni.ttf");
                // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Japan1, "C:/Program Files/Adobe/Acrobat 7.0/Resource/CIDFont/KozMinProVI-Regular.otf");
                // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Japan2, "c:/myfonts/KozMinProVI-Regular.otf");
                //
                // If fonts are in PDFNet resource folder, it is not necessary to specify
                // the full path name. For example,
                //---
                // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Korea1, "AdobeMyungjoStd-Medium.otf");
                // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_CNS1, "AdobeSongStd-Light.otf");
                // PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_GB1, "AdobeMingStd-Light.otf");
            }
            catch (Exception)
            {
                Console.WriteLine("The specified color profile was not found.");
            }

            // Relative path to the folder containing test files.
            string input_path  = "../../TestFiles/";
            string output_path = "../../TestFiles/Output/";


            using (PDFDraw draw = new PDFDraw())
            {
                //--------------------------------------------------------------------------------
                // Example 1) Convert the first PDF page to PNG at 92 DPI.
                // A three step tutorial to convert PDF page to an image.
                try
                {
                    // A) Open the PDF document.
                    using (PDFDoc doc = new PDFDoc(input_path + "tiger.pdf"))
                    {
                        // Initialize the security handler, in case the PDF is encrypted.
                        doc.InitSecurityHandler();

                        // B) The output resolution is set to 92 DPI.
                        draw.SetDPI(92);

                        // C) Rasterize the first page in the document and save the result as PNG.
                        Page pg = doc.GetPage(1);
                        draw.Export(pg, output_path + "tiger_92dpi.png");

                        Console.WriteLine("Example 1: tiger_92dpi.png");

                        // Export the same page as TIFF
                        draw.Export(pg, output_path + "tiger_92dpi.tif", "TIFF");
                    }
                }
                catch (PDFNetException e) {
                    Console.WriteLine(e.Message);
                }

                //--------------------------------------------------------------------------------
                // Example 2) Convert the all pages in a given document to JPEG at 72 DPI.
                ObjSet hint_set = new ObjSet();               // A collection of rendering 'hits'.
                Console.WriteLine("Example 2:");
                try
                {
                    using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf"))
                    {
                        // Initialize the security handler, in case the PDF is encrypted.
                        doc.InitSecurityHandler();

                        draw.SetDPI(72);                         // Set the output resolution is to 72 DPI.

                        // Use optional encoder parameter to specify JPEG quality.
                        Obj encoder_param = hint_set.CreateDict();
                        encoder_param.PutNumber("Quality", 80);

                        // Traverse all pages in the document.
                        for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())
                        {
                            string output_filename = string.Format("newsletter{0:d}.jpg", itr.GetPageNumber());
                            Console.WriteLine("newsletter{0:d}.jpg", itr.GetPageNumber());
                            draw.Export(itr.Current(), output_path + output_filename, "JPEG", encoder_param);
                        }
                    }

                    Console.WriteLine("Done.");
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine(e.Message);
                }

                try                  // Examples 3-6
                {
                    // Common code for remaining samples.
                    using (PDFDoc tiger_doc = new PDFDoc(input_path + "tiger.pdf"))
                    {
                        // Initialize the security handler, in case the PDF is encrypted.
                        tiger_doc.InitSecurityHandler();
                        Page page = tiger_doc.GetPage(1);

                        //--------------------------------------------------------------------------------
                        // Example 3) Convert the first page to GDI+ Bitmap. Also, rotate the
                        // page 90 degrees and save the result as RAW.
                        draw.SetDPI(100);                         // Set the output resolution is to 100 DPI.
                        draw.SetRotate(Page.Rotate.e_90);         // Rotate all pages 90 degrees clockwise.

                        BitmapInfo buf = draw.GetBitmap(page, PDFDraw.PixelFormat.e_rgb, false);

                        // Save the raw RGB data to disk.
                        string filename = "tiger_100dpi_rot90.raw";

                        System.IO.File.WriteAllBytes(output_path + filename, buf.Buffer);

                        Console.WriteLine("Example 3: tiger_100dpi_rot90.raw");
                        draw.SetRotate(Page.Rotate.e_0);                          // Disable image rotation for remaining samples.

                        //--------------------------------------------------------------------------------
                        // Example 4) Convert PDF page to a fixed image size. Also illustrates some
                        // other features in PDFDraw class such as rotation, image stretching, exporting
                        // to grayscale, or monochrome.

                        // Initialize render 'gray_hint' parameter, that is used to control the
                        // rendering process. In this case we tell the rasterizer to export the image as
                        // 1 Bit Per Component (BPC) image.
                        Obj mono_hint = hint_set.CreateDict();
                        mono_hint.PutNumber("BPC", 1);

                        // SetImageSize can be used instead of SetDPI() to adjust page  scaling
                        // dynamically so that given image fits into a buffer of given dimensions.
                        draw.SetImageSize(1000, 1000);                                  // Set the output image to be 1000 wide and 1000 pixels tall
                        draw.Export(page, output_path + "tiger_1000x1000.png", "PNG", mono_hint);
                        Console.WriteLine("Example 4: tiger_1000x1000.png");

                        draw.SetImageSize(200, 400);                                // Set the output image to be 200 wide and 300 pixels tall
                        draw.SetRotate(Page.Rotate.e_180);                          // Rotate all pages 90 degrees clockwise.

                        // 'gray_hint' tells the rasterizer to export the image as grayscale.
                        Obj gray_hint = hint_set.CreateDict();
                        gray_hint.PutName("ColorSpace", "Gray");

                        draw.Export(page, output_path + "tiger_200x400_rot180.png", "PNG", gray_hint);
                        Console.WriteLine("Example 4: tiger_200x400_rot180.png");

                        draw.SetImageSize(400, 200, false);                         // The third parameter sets 'preserve-aspect-ratio' to false.
                        draw.SetRotate(Page.Rotate.e_0);                            // Disable image rotation.
                        draw.Export(page, output_path + "tiger_400x200_stretch.jpg", "JPEG");
                        Console.WriteLine("Example 4: tiger_400x200_stretch.jpg");

                        //--------------------------------------------------------------------------------
                        // Example 5) Zoom into a specific region of the page and rasterize the
                        // area at 200 DPI and as a thumbnail (i.e. a 50x50 pixel image).
                        page.SetCropBox(new Rect(216, 522, 330, 600));                          // Set the page crop box.

                        // Select the crop region to be used for drawing.
                        draw.SetPageBox(Page.Box.e_crop);
                        draw.SetDPI(900);                          // Set the output image resolution to 900 DPI.
                        draw.Export(page, output_path + "tiger_zoom_900dpi.png", "PNG");
                        Console.WriteLine("Example 5: tiger_zoom_900dpi.png");

                        // -------------------------------------------------------------------------------
                        // Example 6)
                        draw.SetImageSize(50, 50);         // Set the thumbnail to be 50x50 pixel image.
                        draw.Export(page, output_path + "tiger_zoom_50x50.png", "PNG");
                        Console.WriteLine("Example 6: tiger_zoom_50x50.png");
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine(e.Message);
                }

                Obj cmyk_hint = hint_set.CreateDict();
                cmyk_hint.PutName("ColorSpace", "CMYK");

                //--------------------------------------------------------------------------------
                // Example 7) Convert the first PDF page to CMYK TIFF at 92 DPI.
                // A three step tutorial to convert PDF page to an image.
                try
                {
                    // A) Open the PDF document.
                    using (PDFDoc doc = new PDFDoc(input_path + "tiger.pdf"))
                    {
                        // Initialize the security handler, in case the PDF is encrypted.
                        doc.InitSecurityHandler();

                        // B) The output resolution is set to 92 DPI.
                        draw.SetDPI(92);

                        // C) Rasterize the first page in the document and save the result as TIFF.
                        Page pg = doc.GetPage(1);
                        draw.Export(pg, output_path + "out1.tif", "TIFF", cmyk_hint);
                        Console.WriteLine("Example 7: out1.tif");
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine(e.Message);
                }

                //--------------------------------------------------------------------------------
                // Example 8) Export raster content to PNG using different image smoothing settings.
                try
                {
                    // A) Open the PDF document.
                    using (PDFDoc doc = new PDFDoc(input_path + "tiger.pdf"))
                    {
                        // Initialize the security handler, in case the PDF is encrypted.
                        doc.InitSecurityHandler();

                        // B) Get the page matrix
                        Page     pg  = doc.GetPage(1);
                        Page.Box box = Page.Box.e_crop;
                        Matrix2D mtx = pg.GetDefaultMatrix(true, box);
                        // We want to render a quadrant, so use half of width and height
                        double pg_w = pg.GetPageWidth(box) / 2;
                        double pg_h = pg.GetPageHeight(box) / 2;

                        // C) Scale matrix from PDF space to buffer space
                        double dpi             = 96.0;
                        double scale           = dpi / 72.0; // PDF space is 72 dpi
                        int    buf_w           = (int)(Math.Floor(scale * pg_w));
                        int    buf_h           = (int)(Math.Floor(scale * pg_h));
                        int    bytes_per_pixel = 4; // BGRA buffer
                        int    buf_size        = buf_w * buf_h * bytes_per_pixel;
                        mtx.Translate(0, -pg_h);    // translate by '-pg_h' since we want south-west quadrant
                        mtx = new Matrix2D(scale, 0, 0, scale, 0, 0) * mtx;

                        // D) Rasterize page into memory buffer, according to our parameters
                        byte[]        buf;
                        PDFRasterizer rast = new PDFRasterizer();
                        buf = rast.Rasterize(pg, buf_w, buf_h, buf_w * bytes_per_pixel, bytes_per_pixel, true, mtx);

                        // buf now contains raw BGRA bitmap.
                        Console.WriteLine("Example 8: Successfully rasterized into memory buffer.");
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine(e.Message);
                }
                //--------------------------------------------------------------------------------
                // Example 9) Export raster content to PNG using different image smoothing settings.
                try
                {
                    using (PDFDoc text_doc = new PDFDoc(input_path + "lorem_ipsum.pdf"))
                    {
                        text_doc.InitSecurityHandler();

                        draw.SetImageSmoothing(false, false);
                        string filename = "raster_text_no_smoothing.png";
                        draw.Export(text_doc.GetPageIterator().Current(), output_path + filename);
                        Console.WriteLine("Example 9 a): " + filename + ". Done.");

                        filename = "raster_text_smoothed.png";
                        draw.SetImageSmoothing(true, false /*default quality bilinear resampling*/);
                        draw.Export(text_doc.GetPageIterator().Current(), output_path + filename);
                        Console.WriteLine("Example 9 b): " + filename + ". Done.");

                        filename = "raster_text_high_quality.png";
                        draw.SetImageSmoothing(true, true /*high quality area resampling*/);
                        draw.Export(text_doc.GetPageIterator().Current(), output_path + filename);
                        Console.WriteLine("Example 9 c): " + filename + ". Done.");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

                //--------------------------------------------------------------------------------
                // Example 10) Export separations directly, without conversion to an output colorspace
                try
                {
                    using (PDFDoc separation_doc = new PDFDoc(input_path + "op_blend_test.pdf"))
                    {
                        separation_doc.InitSecurityHandler();
                        Obj separation_hint = hint_set.CreateDict();
                        separation_hint.PutName("ColorSpace", "Separation");
                        draw.SetDPI(96);
                        draw.SetImageSmoothing(true, true);
                        draw.SetOverprint(PDFRasterizer.OverprintPreviewMode.e_op_on);

                        string filename = "merged_separations.png";
                        draw.Export(separation_doc.GetPageIterator().Current(), output_path + filename, "PNG");
                        Console.WriteLine("Example 10 a): " + filename + ". Done.");

                        filename = "separation";
                        draw.Export(separation_doc.GetPageIterator().Current(), output_path + filename, "PNG", separation_hint);
                        Console.WriteLine("Example 10 b): " + filename + "_[ink].png. Done.");

                        filename = "separation_NChannel.tif";
                        draw.Export(separation_doc.GetPageIterator().Current(), output_path + filename, "TIFF", separation_hint);
                        Console.WriteLine("Example 10 c): " + filename + ". Done.");
                    }
                }
                catch (PDFNetException e)
                {
                    Console.WriteLine(e.Message);
                }
            }              // using PDFDraw
        }
Exemple #33
0
        public void ReadAdvanced(string input_path)
        {
            PDFNet.Initialize();

            try
            {
                PDFDoc doc = new PDFDoc(input_path);
                doc.InitSecurityHandler();

                Page page = doc.GetPage(1);
                if (page == null)
                {
                    ConsoleLog += "Page not found.";
                    return;
                }

                TextExtractor txt = new TextExtractor();
                txt.Begin(page); // Read the page.
                // Other options you may want to consider...
                // txt.Begin(page, null, TextExtractor.ProcessingFlags.e_no_dup_remove);
                // txt.Begin(page, null, TextExtractor.ProcessingFlags.e_remove_hidden_text);
                // ...

                // Example 1. Get all text on the page in a single string.
                // Words will be separated with space or new line characters.
                if (example1_basic)
                {
                    // Get the word count.
                    ConsoleLog += "Word Count: {0}" + txt.GetWordCount();

                    ConsoleLog += "\n\n- GetAsText --------------------------\n{0}" + txt.GetAsText();
                    ConsoleLog += "-----------------------------------------------------------";
                }

                // Example 2. Get XML logical structure for the page.
                if (example2_xml)
                {
                    String text = txt.GetAsXML(TextExtractor.XMLOutputFlags.e_words_as_elements | TextExtractor.XMLOutputFlags.e_output_bbox | TextExtractor.XMLOutputFlags.e_output_style_info);
                    ConsoleLog += "\n\n- GetAsXML  --------------------------\n{0}" + text;
                    ConsoleLog += "-----------------------------------------------------------";
                }

                // Example 3. Extract words one by one.
                if (example3_wordlist)
                {
                    TextExtractor.Word word;
                    for (TextExtractor.Line line = txt.GetFirstLine(); line.IsValid(); line = line.GetNextLine())
                    {
                        for (word = line.GetFirstWord(); word.IsValid(); word = word.GetNextWord())
                        {
                            ConsoleLog += word.GetString();
                        }
                    }
                    ConsoleLog += "-----------------------------------------------------------";
                }

                // Example 3. A more advanced text extraction example.
                // The output is XML structure containing paragraphs, lines, words,
                // as well as style and positioning information.
                if (example4_advanced)
                {
                    Rect bbox;
                    int  cur_flow_id = -1, cur_para_id = -1;

                    TextExtractor.Line  line;
                    TextExtractor.Word  word;
                    TextExtractor.Style s, line_style;

                    // For each line on the page...
                    for (line = txt.GetFirstLine(); line.IsValid(); line = line.GetNextLine())
                    {
                        if (line.GetNumWords() == 0)
                        {
                            continue;
                        }

                        if (cur_flow_id != line.GetFlowID())
                        {
                            if (cur_flow_id != -1)
                            {
                                if (cur_para_id != -1)
                                {
                                    cur_para_id = -1;
                                    ConsoleLog += "</Para>";
                                }
                                ConsoleLog += "</Flow>";
                            }
                            cur_flow_id = line.GetFlowID();
                            ConsoleLog += "<Flow id=\"{0}\">" + cur_flow_id;
                        }

                        if (cur_para_id != line.GetParagraphID())
                        {
                            if (cur_para_id != -1)
                            {
                                ConsoleLog += "</Para>";
                            }
                            cur_para_id = line.GetParagraphID();
                            ConsoleLog += "<Para id=\"{0}\">" + cur_para_id;
                        }

                        bbox       = line.GetBBox();
                        line_style = line.GetStyle();
                        Console.Write("<Line box=\"" + bbox.y1 + "," + bbox.y2 + "," + bbox.x1 + "," + bbox.x2 + ">");
                        PrintStyle(line_style);
                        ConsoleLog += "";

                        // For each word in the line...
                        for (word = line.GetFirstWord(); word.IsValid(); word = word.GetNextWord())
                        {
                            // Output the bounding box for the word.
                            bbox        = word.GetBBox();
                            ConsoleLog += "<Word box=\"{0}, {1}, {2}, {3}\"" + bbox.x1 + bbox.y1 + bbox.x2 + bbox.y2;

                            int sz = word.GetStringLen();
                            if (sz == 0)
                            {
                                continue;
                            }

                            // If the word style is different from the parent style, output the new style.
                            s = word.GetStyle();
                            if (s != line_style)
                            {
                                PrintStyle(s);
                            }

                            ConsoleLog += ">\n" + word.GetString();
                            ConsoleLog += "</Word>";
                        }
                        ConsoleLog += "</Line>";
                    }

                    if (cur_flow_id != -1)
                    {
                        if (cur_para_id != -1)
                        {
                            cur_para_id = -1;
                            ConsoleLog += "</Para>";
                        }
                        ConsoleLog += "</Flow>";
                    }
                }

                // Note: Calling Dispose() on TextExtractor when it is not anymore in use can result in increased performance and lower memory consumption.
                txt.Dispose();
                doc.Close();
                ConsoleLog += "Done.";
            }
            catch (PDFNetException e)
            {
                ConsoleLog += e.Message;
            }

            // Sample code showing how to use low-level text extraction APIs.
            if (example5_low_level)
            {
                try
                {
                    LowLevelTextExtractUtils util = new LowLevelTextExtractUtils();
                    PDFDoc doc = new PDFDoc(input_path);
                    doc.InitSecurityHandler();

                    // Example 1. Extract all text content from the document
                    ElementReader reader = new ElementReader();
                    PageIterator  itr    = doc.GetPageIterator();
                    //for (; itr.HasNext(); itr.Next()) //  Read every page
                    {
                        reader.Begin(itr.Current());

                        LowLevelTextExtractUtils u = new LowLevelTextExtractUtils();
                        u.DumpAllText(reader);
                        ConsoleLog += u.ConsoleLog;
                        reader.End();
                    }

                    // Example 2. Extract text based on the selection rectangle.
                    ConsoleLog += "----------------------------------------------------";
                    ConsoleLog += "Extract text based on the selection rectangle.";
                    ConsoleLog += "----------------------------------------------------";

                    Page   first_page = doc.GetPage(1);
                    string field1     = util.ReadTextFromRect(first_page, new Rect(27, 392, 563, 534), reader);
                    string field2     = util.ReadTextFromRect(first_page, new Rect(28, 551, 106, 623), reader);
                    string field3     = util.ReadTextFromRect(first_page, new Rect(208, 550, 387, 621), reader);

                    ConsoleLog += "Field 1: {0}" + field1;
                    ConsoleLog += "Field 2: {0}" + field2;
                    ConsoleLog += "Field 3: {0}" + field3;
                    // ...

                    reader.Dispose();
                    doc.Close();
                    ConsoleLog += "Done.";
                }
                catch (PDFNetException e)
                {
                    ConsoleLog += e.Message;
                }
            }

            PDFNet.Terminate();
        }