Пример #1
0
 public void SplitByTopLevelBookmarks()
 {
     if (this.CheckOutput())
     {
         PDFHelper.DisplayTrialPopupIfNecessary();
         try
         {
             PDDocumentCatalog documentCatalog = this.pdfDocument.PDFBoxDocument.getDocumentCatalog();
             PDDocumentOutline documentOutline = documentCatalog.getDocumentOutline();
             if (documentOutline != null)
             {
                 PDOutlineItem firstChild = documentOutline.getFirstChild();
                 PDPageTree    pages      = documentCatalog.getPages();
                 List <int>    nums       = new List <int>();
                 while (firstChild != null)
                 {
                     PDPage pDPage = firstChild.findDestinationPage(this.pdfDocument.PDFBoxDocument);
                     nums.Add(pages.indexOf(pDPage));
                     firstChild = firstChild.getNextSibling();
                 }
                 nums.Add(pages.getCount());
                 for (int i = 0; i < nums.Count - 1; i++)
                 {
                     int        item       = nums[i];
                     int        num        = nums[i + 1];
                     PDDocument pDDocument = new PDDocument();
                     for (int j = item; j < num; j++)
                     {
                         pDDocument.addPage(this.pdfDocument.PDFBoxDocument.getPage(j));
                     }
                     pDDocument = PDFHelper.AddTrialStampIfNecessary(pDDocument);
                     string str = string.Format("{0} [{1}].pdf", this.OutputFileName, i);
                     pDDocument.save(Path.Combine(this.OutputFilePath, str));
                     pDDocument.close();
                 }
             }
             else
             {
                 Console.WriteLine("This document does not contain any bookmarks.");
             }
         }
         catch (Exception exception1)
         {
             Exception exception = exception1;
             throw new PDFToolkitException(exception.Message, exception.InnerException);
         }
     }
 }
Пример #2
0
        public string GetText(PDDocument pdfDocument, bool format)
        {
            string text;
            double num;
            string str = "";

            if (format)
            {
                try
                {
                    PDFHelper.DisplayTrialPopupIfNecessary();
                    PDFTextStripperByArea pDFTextStripperByArea = new PDFTextStripperByArea();
                    if (PDFHelper.AddStamp)
                    {
                        str = string.Concat(str, "You are using a trial license of PDF Toolkit, as a result only the first three pages would be extracted.");
                        pDFTextStripperByArea.setEndPage(3);
                    }
                    pDFTextStripperByArea.setSortByPosition(true);
                    java.util.List arrayList = new java.util.ArrayList();
                    PDPageTree     pages     = pdfDocument.getPages();
                    arrayList.size();
                    foreach (PDPage page in pages)
                    {
                        if ((!PDFHelper.AddStamp ? true : pages.indexOf(page) <= 2))
                        {
                            PDRectangle cropBox  = page.getCropBox();
                            int         rotation = page.getRotation();
                            if (cropBox == null)
                            {
                                cropBox = page.getMediaBox();
                            }
                            int num1 = 0;
                            int num2 = 0;
                            if (rotation % 180 != 0)
                            {
                                num  = Math.Round((double)cropBox.getWidth());
                                num1 = int.Parse(num.ToString()) - 50;
                                num  = Math.Round((double)cropBox.getHeight());
                                num2 = int.Parse(num.ToString()) - 50;
                            }
                            else
                            {
                                num  = Math.Round((double)cropBox.getHeight());
                                num1 = int.Parse(num.ToString()) - 50;
                                num  = Math.Round((double)cropBox.getWidth());
                                num2 = int.Parse(num.ToString()) - 50;
                            }
                            pDFTextStripperByArea.addRegion("class1", new java.awt.Rectangle(0, 0, num2, num1));
                            pDFTextStripperByArea.extractRegions(page);
                            str = string.Concat(str, pDFTextStripperByArea.getTextForRegion("class1"));
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    throw new PDFToolkitException(exception.Message, exception.InnerException);
                }
                text = str;
            }
            else
            {
                text = this.GetText(pdfDocument);
            }
            return(text);
        }