Exemple #1
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();
                }
            }
        }
Exemple #2
0
        private void CheckAllPages(PDDocument doc)
        {
            int num = 0;

            try
            {
                this.NumberOfPagesDict = doc.getNumberOfPages();
                foreach (PDPage page in doc.getPages())
                {
                    if (page.getMediaBox() == null)
                    {
                        this.ErrorMessage = string.Format("Page number {0} has no media box", num);
                        this.IsValid      = false;
                    }
                    if (page.getResources() == null)
                    {
                        this.ErrorMessage = string.Format("Page number {0}, has no page resources", num);
                        this.IsValid      = false;
                    }
                    num++;
                }
                if (this.NumberOfPagesDict != num)
                {
                    this.ErrorMessage = string.Format("Page Number Mismatch between dictionary and actual document", new object[0]);
                    this.IsValid      = false;
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                this.ErrorMessage = string.Format("PDF analysis failed on page number {0},\nWith exception {1}", num, exception.Message);
                this.IsValid      = false;
            }
        }
Exemple #3
0
        internal PDFBookmarkItem(PDOutlineItem pdfBoxBookmark, PDDocument doc)
        {
            this.PDFBoxBookmark = pdfBoxBookmark;
            PDPage pDPage = pdfBoxBookmark.findDestinationPage(doc);

            this.BookMarkPage  = doc.getPages().indexOf(pDPage) + 1;
            this.bookmarkTitle = pdfBoxBookmark.getTitle();
            this.BookmarkItems = new List <PDFBookmarkItem>();
        }
Exemple #4
0
 internal static PDDocument AddTrialStampIfNecessary(PDDocument pdfFile, bool addJavaScript)
 {
     if (PDFHelper.AddStamp)
     {
         PDFont pDFont = PDTrueTypeFont.load(pdfFile, new java.io.File("C:\\Windows\\Fonts\\Arial.ttf"), new WinAnsiEncoding());
         foreach (PDPage page in pdfFile.getPages())
         {
             PDPageContentStream pDPageContentStream = new PDPageContentStream(pdfFile, page, PDPageContentStream.AppendMode.APPEND, true);
             pDPageContentStream.setFont(pDFont, 14f);
             pDPageContentStream.beginText();
             pDPageContentStream.newLineAtOffset(100f, page.getMediaBox().getHeight() - 100f);
             pDPageContentStream.showText("Created with a trial copy of Aquaforest PDF Toolkit");
             pDPageContentStream.endText();
             pDPageContentStream.close();
         }
     }
     return(pdfFile);
 }
 internal static void ExtractAttachment(PDFDocument pdfDoc, string OutputPath)
 {
     try
     {
         string     filePath       = pdfDoc.FilePath;
         PDDocument pDFBoxDocument = pdfDoc.PDFBoxDocument;
         PDEmbeddedFilesNameTreeNode embeddedFiles = (new PDDocumentNameDictionary(pDFBoxDocument.getDocumentCatalog())).getEmbeddedFiles();
         if (embeddedFiles != null)
         {
             Map names = embeddedFiles.getNames();
             if (names == null)
             {
                 object[] objArray = embeddedFiles.getKids().toArray();
                 for (int i = 0; i < (int)objArray.Length; i++)
                 {
                     names = ((PDNameTreeNode)objArray[i]).getNames();
                     PDFExtractEmbeddedFiles.extractFiles(names, OutputPath);
                 }
             }
             else
             {
                 PDFExtractEmbeddedFiles.extractFiles(names, OutputPath);
             }
         }
         foreach (PDPage page in pDFBoxDocument.getPages())
         {
             object[] objArray1 = page.getAnnotations().toArray();
             for (int j = 0; j < (int)objArray1.Length; j++)
             {
                 PDAnnotation pDAnnotation = (PDAnnotation)objArray1[j];
                 if (pDAnnotation is PDAnnotationFileAttachment)
                 {
                     PDComplexFileSpecification file         = (PDComplexFileSpecification)((PDAnnotationFileAttachment)pDAnnotation).getFile();
                     PDEmbeddedFile             embeddedFile = PDFExtractEmbeddedFiles.getEmbeddedFile(file);
                     PDFExtractEmbeddedFiles.extractFile(filePath, file.getFilename(), embeddedFile);
                 }
             }
         }
     }
     finally
     {
     }
 }
Exemple #6
0
 internal static PDDocument AddTrialStampIfNecessary(PDDocument pdfFile)
 {
     if (PDFHelper.AddStamp)
     {
         PDImageXObject pDImageXObject = null;
         foreach (PDPage page in pdfFile.getPages())
         {
             string str = PDFHelper.CreateLogo("Created with a trial copy of Aquaforest PDF Toolkit", page.getRotation());
             if (!string.IsNullOrEmpty(str))
             {
                 pDImageXObject = PDImageXObject.createFromFile(str, pdfFile);
                 PDPageContentStream pDPageContentStream = new PDPageContentStream(pdfFile, page, PDPageContentStream.AppendMode.APPEND, true);
                 pDPageContentStream.drawImage(pDImageXObject, 100f, page.getMediaBox().getHeight() - 100f);
                 pDPageContentStream.close();
             }
         }
         pdfFile.getDocumentCatalog().setOpenAction(PDFHelper.GetJavascriptPopup());
     }
     return(pdfFile);
 }
Exemple #7
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);
        }
Exemple #8
0
        public PDF2PDFaConverter(string fileName, string output, AquaforestPDFAFlavour PDFAFlavour)
        {
            try
            {
                Environment.SetEnvironmentVariable("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
                int pDFAFlavour = (int)PDFAFlavour;
                this.SetFlavour(pDFAFlavour);
                this.PDFAFlavour    = pDFAFlavour;
                this.outPutFileName = output;
                this.inputFileName  = fileName;
                string tempPath = Path.GetTempPath();
                Guid   guid     = Guid.NewGuid();
                this.tempFileName = Path.Combine(tempPath, string.Concat("aquaforest\\pdftoolkit\\", guid.ToString(), "\\", Path.GetFileName(output)));
                string directoryName = Path.GetDirectoryName(this.tempFileName);
                if (!Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }
                string str = Path.Combine(directoryName, "password.pdf");
                System.IO.File.Copy(this.inputFileName, str, true);
                PDDocument pDDocument = PDDocument.load(new java.io.File(str));
                this.doc = new PDDocument();
                switch (this.pdfaversion)
                {
                case 1:
                {
                    if (this.doc.getDocument().getVersion() < 1.4f)
                    {
                        this.doc.getDocument().setVersion(1.4f);
                    }
                    break;
                }

                case 2:
                {
                    if (this.doc.getDocument().getVersion() < 1.7f)
                    {
                        this.doc.getDocument().setVersion(1.7f);
                    }
                    break;
                }

                case 3:
                {
                    if (this.doc.getDocument().getVersion() < 1.7f)
                    {
                        this.doc.getDocument().setVersion(1.7f);
                    }
                    break;
                }
                }
                foreach (PDPage page in pDDocument.getPages())
                {
                    this.doc.addPage(page);
                }
                this.doc.setDocumentInformation(pDDocument.getDocumentInformation());
                this.doc.getDocumentCatalog().setDocumentOutline(pDDocument.getDocumentCatalog().getDocumentOutline());
                this.doc.getDocumentCatalog().setAcroForm(pDDocument.getDocumentCatalog().getAcroForm());
                this.doc.getDocumentCatalog().setLanguage(pDDocument.getDocumentCatalog().getLanguage());
                this.doc.getDocumentCatalog().setMetadata(pDDocument.getDocumentCatalog().getMetadata());
                this.doc.getDocumentCatalog().setPageLabels(pDDocument.getDocumentCatalog().getPageLabels());
                this.doc.getDocumentCatalog().setViewerPreferences(pDDocument.getDocumentCatalog().getViewerPreferences());
                this.doc.getDocumentCatalog().setPageMode(pDDocument.getDocumentCatalog().getPageMode());
                this.doc.getDocumentCatalog().setPageLayout(pDDocument.getDocumentCatalog().getPageLayout());
                this.doc.save(this.tempFileName);
                this.doc.close();
                this.doc = PDDocument.load(new java.io.File(this.tempFileName));
                if (pDDocument != null)
                {
                    pDDocument.close();
                    pDDocument = null;
                }
                try
                {
                    Assembly executingAssembly      = Assembly.GetExecutingAssembly();
                    Stream   manifestResourceStream = executingAssembly.GetManifestResourceStream("Aquaforest.PDF.sRGB_IEC61966-2-1_black_scaled.icc");
                    using (FileStream fileStream = System.IO.File.Create("sRGB_IEC61966-2-1_black_scaled.icc", (int)manifestResourceStream.Length))
                    {
                        byte[] numArray = new byte[checked (manifestResourceStream.Length)];
                        manifestResourceStream.Read(numArray, 0, (int)numArray.Length);
                        fileStream.Write(numArray, 0, (int)numArray.Length);
                    }
                    manifestResourceStream = executingAssembly.GetManifestResourceStream("Aquaforest.PDF.font.xml");
                    using (FileStream fileStream1 = System.IO.File.Create("font.xml", (int)manifestResourceStream.Length))
                    {
                        byte[] numArray1 = new byte[checked (manifestResourceStream.Length)];
                        manifestResourceStream.Read(numArray1, 0, (int)numArray1.Length);
                        fileStream1.Write(numArray1, 0, (int)numArray1.Length);
                    }
                }
                catch
                {
                }
                this.iccString = "sRGB_IEC61966-2-1_black_scaled.icc";
            }
            catch (Exception exception)
            {
                Environment.Exit(104);
            }
        }