예제 #1
0
 public void SplitByRepeatingPageRanges(string pageRange, int repeatEvery)
 {
     if (this.CheckOutput())
     {
         PDFHelper.DisplayTrialPopupIfNecessary();
         try
         {
             if (pageRange.Contains <char>('-'))
             {
                 string str = "";
                 if (pageRange.ToLower().Contains("odd"))
                 {
                     str = "odd";
                 }
                 else if (pageRange.ToLower().Contains("even"))
                 {
                     str = "even";
                 }
                 PDPageTree pages = this.pdfDocument.PDFBoxDocument.getPages();
                 int        count = pages.getCount();
                 this.splitCounter = 0;
                 this.SplitByRepeatingPageRanges(pageRange, repeatEvery, pages, count, str);
             }
             else
             {
                 Console.WriteLine("Invalid page range.");
             }
         }
         catch (Exception exception1)
         {
             Exception exception = exception1;
             throw new PDFToolkitException(exception.Message, exception);
         }
     }
 }
예제 #2
0
        static void Main(string[] args)
        {
            if (args == null || args.Length < 2)
            {
                System.Console.WriteLine("Usage: " + AppDomain.CurrentDomain.FriendlyName + " <original PDF filename> <watermark text> [new PDF filename]" + Environment.NewLine +
                                         "  For example: " + AppDomain.CurrentDomain.FriendlyName + " myDoc.pdf \"This is a Draft\"");
            }
            else
            {
                string origName     = args[0];
                string watermarkTxt = args[1];
                if (!System.IO.File.Exists(origName))
                {
                    System.Console.WriteLine("Error: cannot find the original PDF file(" + origName + "). Please correct the filename or the path and try again.");
                }
                else
                {
                    PDDocument origDoc  = PDDocument.load(new java.io.File(origName)); // NOTE: PDDocument.load() only takes java.io.File, not System.IO.File from C#.Net
                    PDPageTree allPages = origDoc.getPages();
                    PDFont     font     = PDType1Font.HELVETICA_BOLD;
                    for (int i = 0, len = allPages.getCount(); i < len; ++i)
                    {
                        PDPage pg = (PDPage)allPages.get(i);
                        addWatermarkText(origDoc, pg, font, "This is a draft!!!");
                    }

                    origDoc.save("watermarked_" + origName);
                    origDoc.close();
                }
            }
        }
예제 #3
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);
         }
     }
 }
예제 #4
0
        private void SplitByRepeatingPageRanges(string pageRange, int repeatEvery, PDPageTree allPages, int pageCount, string suffix)
        {
            int num;
            int num1;
            int num2;
            int num3;

            if (this.ConfigureRange(pageRange, pageCount, out num, out num1, out num2, out num3))
            {
                List       list       = IteratorUtils.toList(allPages.iterator());
                List       list1      = list.subList(num, num1);
                PDDocument pDDocument = new PDDocument();
                for (int i = num2; i < list1.size(); i = i + num3)
                {
                    pDDocument.addPage((PDPage)list1.@get(i));
                }
                pDDocument = PDFHelper.AddTrialStampIfNecessary(pDDocument);
                string str = string.Format("{0} [{1}].pdf", this.OutputFileName, this.splitCounter);
                pDDocument.save(Path.Combine(this.OutputFilePath, str));
                if (repeatEvery > 0)
                {
                    num  = num + repeatEvery + 1;
                    num1 = num1 + repeatEvery;
                    if (num <= pageCount)
                    {
                        if (num1 > pageCount)
                        {
                            num1 = pageCount;
                        }
                        this.splitCounter = this.splitCounter + 1;
                        string str1 = string.Format("{0}-{1}{2}", num, num1, suffix);
                        this.SplitByRepeatingPageRanges(str1, repeatEvery, allPages, pageCount, suffix);
                    }
                }
            }
            else
            {
                Console.WriteLine("Invalid page range.");
            }
        }
예제 #5
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);
        }