Esempio n. 1
0
        internal static bool EmbedPDFAttachment(List <PDFAttachmentItem> attachments, PDDocument doc)
        {
            bool flag;

            try
            {
                PDEmbeddedFilesNameTreeNode pDEmbeddedFilesNameTreeNode = new PDEmbeddedFilesNameTreeNode();
                List arrayList = new ArrayList();
                foreach (PDFAttachmentItem attachment in attachments)
                {
                    PDComplexFileSpecification pDComplexFileSpecification = new PDComplexFileSpecification();
                    pDComplexFileSpecification.setFile(System.IO.Path.GetFileName(attachment.filePath));
                    java.io.File   file           = new java.io.File(attachment.filePath);
                    byte[]         numArray       = Files.readAllBytes(file.toPath());
                    PDEmbeddedFile pDEmbeddedFile = new PDEmbeddedFile(doc, new ByteArrayInputStream(numArray));
                    pDEmbeddedFile.setSize((int)numArray.Length);
                    pDEmbeddedFile.setCreationDate(new GregorianCalendar());
                    pDComplexFileSpecification.setEmbeddedFile(pDEmbeddedFile);
                    PDEmbeddedFilesNameTreeNode pDEmbeddedFilesNameTreeNode1 = new PDEmbeddedFilesNameTreeNode();
                    pDEmbeddedFilesNameTreeNode1.setNames(Collections.singletonMap(attachment.filePath, pDComplexFileSpecification));
                    arrayList.@add(pDEmbeddedFilesNameTreeNode1);
                }
                pDEmbeddedFilesNameTreeNode.setKids(arrayList);
                PDDocumentNameDictionary pDDocumentNameDictionary = new PDDocumentNameDictionary(doc.getDocumentCatalog());
                pDDocumentNameDictionary.setEmbeddedFiles(pDEmbeddedFilesNameTreeNode);
                doc.getDocumentCatalog().setNames(pDDocumentNameDictionary);
                flag = true;
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                throw new PDFToolkitException(exception.Message, exception);
            }
            return(flag);
        }
Esempio n. 2
0
        public PdfOcrResult Execute(byte[] bytes)
        {
            PDDocument document = null;

            try
            {
                LoadPdf(bytes, ref document);

                List allPages = document.getDocumentCatalog().getAllPages();
                if (allPages.size() == 0)
                {
                    throw new PdfNotReadableException("Pdf contains no readable content");
                }

                //only first page
                PDPage page = (PDPage)allPages.get(0);

                PDStream contents = page.getContents();
                if (contents == null)
                {
                    throw new PdfNotReadableException("Pdf contains no readable content");
                }

                var items = new PdfToCharacters().GetItems(page, page.findResources(), page.getContents().getStream());
                if (items.Count == 0)
                {
                    throw new PdfNotReadableException("Pdf contains no readable content");
                }

                var mediaBox = page.findMediaBox();

                var height     = mediaBox?.getHeight() ?? 0;
                var width      = mediaBox?.getWidth() ?? 0;
                var itemsArray = items.ToArray();

                var keywords = "";
                try
                {
                    keywords = document.getDocumentInformation()?.getKeywords();
                }
                catch (Exception) { } // we do not know if PDF box can fail on this, if there is no keywords or something else. We dont really care we just want the keywords if possible.

                return(new PdfOcrResult()
                {
                    Items = itemsArray, Height = height, Width = width, Keywords = keywords
                });
            }
            catch (PdfReadException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new PdfReadException("Pdf could not be loaded. It is not a redable pdf.", e);
            }
            finally
            {
                document?.close();
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Get a thumbnail of the document, if possible
        /// </summary>
        /// <param name="sizeX">The maximum X size of the thumbnail</param>
        /// <param name="sizeY">The maximum y size of the thumbnail</param>
        /// <param name="forceFullSize">True if the thumbnail should be exatly XxY pixels and False if the thumbnail
        /// should fit inside a XxY box but should maintain its aspect ratio</param>
        /// <returns>A JPEG byte thumbnail or null if the thumbnail can´t be generated</returns>
        public override byte[] GetThumbnail(int sizeX, int sizeY, bool forceFullSize)
        {
            // If we have no bytes then we can't do anything.
            if (Bytes == null || Bytes.Length == 0)
            {
                return(null);
            }

            try
            {
                org.pdfbox.pdfviewer.PageDrawer pagedrawer = new
                                                             org.pdfbox.pdfviewer.PageDrawer();

                java.io.ByteArrayInputStream byteStream = new java.io.ByteArrayInputStream(Bytes);
                PDDocument     doc   = PDDocument.load(byteStream);
                int            count = doc.getNumberOfPages();
                java.util.List pages = doc.getDocumentCatalog().getAllPages();
                if (pages.size() > 0)
                {
                    PDPage page = pagedrawer.getPage();
                    java.awt.image.BufferedImage  image = page.convertToImage();
                    java.io.ByteArrayOutputStream os    = new java.io.ByteArrayOutputStream();
                    ImageIO.write(image, "jpg", os);
                    byte[] data = os.toByteArray();
                    return(data);
                }
            }
            catch (Exception e)
            {
                log.Error("Failed to get the thumbnail from the PDF file " + Name, e);
            }

            return(null);
        }
Esempio n. 4
0
        public string PdfFields()
        {
            string    pdfText = String.Empty;
            PDFParser parser  = new PDFParser(new BufferedInputStream(new FileInputStream(PdfFile)));

            parser.parse();
            PDDocument originialPdfDoc = parser.getPDDocument();

            bool isOriginalDocEncrypted = originialPdfDoc.isEncrypted();

            if (isOriginalDocEncrypted)
            {
                originialPdfDoc.openProtection(new StandardDecryptionMaterial(PdfPassword));
            }

            try
            {
                PDDocumentCatalog docCatalog = originialPdfDoc.getDocumentCatalog();
                PDAcroForm        acroForm   = docCatalog.getAcroForm();
                PDField           field      = acroForm.getField("Name");
                if (field != null)
                {
                    field.setValue("name");
                }
            }
            catch (java.io.IOException ex)
            {
                throw ex;
            }
            return(pdfText);
        }
Esempio n. 5
0
        public static string HandleContent(string fileName, int pageIndex = 1)
        {
            try
            {
                PDDocument document = null;
                try
                {
                    document = PDDocument.load(fileName);
                    List allPages = document.getDocumentCatalog().getAllPages();

                    int size = pageIndex == 0 ? allPages.size() : 1;

                    for (int i = 0; i < size; i++)
                    {
                        var page = (PDPage)allPages.get(i);

                        var contents = page.getContents();

                        PrintTextLocatins2 printer = new PrintTextLocatins2();

                        if (contents != null)
                        {
                            printer.processStream(page, page.findResources(), page.getContents().getStream());
                        }
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    if (document != null)
                    {
                        document.close();
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return("");
        }
Esempio n. 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);
 }
Esempio n. 7
0
 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
     {
     }
 }
Esempio n. 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);
            }
        }
        /**
         * Extracts a ZUGFeRD invoice from a PDF document represented by an input stream.
         * Errors are reported via exception handling.
         */
        public void extractLowLevel(InputStream pdfStream)
        {
            PDDocument doc = null;

            try
            {
                doc = PDDocument.load(pdfStream);
                // PDDocumentInformation info = doc.getDocumentInformation();
                PDDocumentNameDictionary names = new PDDocumentNameDictionary(
                    doc.getDocumentCatalog());
                PDEmbeddedFilesNameTreeNode etn;
                etn = names.getEmbeddedFiles();
                if (etn == null)
                {
                    doc.close();
                    return;
                }
                //Set efMap = etn.getNames().keySet();
                Object[] efMap = etn.getNames().keySet().toArray();
                //Map<String, COSObjectable> efMap = etn.getNames();
                // String filePath = "/tmp/";
                //for (String filename : efMap.keySet()) {
                foreach (String filename in efMap)
                {
                    ///**
                    //* currently (in the release candidate of version 1) only one
                    //* attached file with the name ZUGFeRD-invoice.xml is allowed
                    //* */
                    if (filename.Equals("ZUGFeRD-invoice.xml"))
                    { //$NON-NLS-1$
                        containsMeta = true;
                        PDComplexFileSpecification fileSpec     = (PDComplexFileSpecification)etn.getNames().get(filename);
                        PDEmbeddedFile             embeddedFile = fileSpec.getEmbeddedFile();
                        // String embeddedFilename = filePath + filename;
                        // File file = new File(filePath + filename);
                        // System.out.println("Writing " + embeddedFilename);
                        // ByteArrayOutputStream fileBytes=new
                        // ByteArrayOutputStream();
                        // FileOutputStream fos = new FileOutputStream(file);
                        rawXML = embeddedFile.getByteArray();
                        setMeta(Encoding.UTF8.GetString(rawXML));
                        // fos.write(embeddedFile.getByteArray());
                        // fos.close();
                    }
                }
            }
            catch (IOException e1)
            {
                throw e1;
            }
            finally
            {
                try
                {
                    if (doc != null)
                    {
                        doc.close();
                    }
                }
                catch (IOException e) { }
            }
        }
Esempio n. 10
0
        private void LoadPDFAndLookForAttachments(string PDFPath)
        {
            try
            {
                pd.close();
            }
            catch
            {
                // pd isn't open
            }

            FileDropStatus.Text = "";
            AttachmentsPanel.Children.Clear();

            if (!String.Equals(System.IO.Path.GetExtension(PDFPath), ".pdf", StringComparison.CurrentCultureIgnoreCase))
            {
                MessageBoxResult result = MessageBox.Show("PDF Attacher only reads PDFs.", "PDF expected.");
            }
            else
            {
                FileDropStatus.Text = System.IO.Path.GetFileName(PDFPath);
                pd = PDDocument.load(PDFPath);

                // Get attachments and save out as a file
                PDDocumentCatalog           catalog       = pd.getDocumentCatalog();
                PDDocumentNameDictionary    names         = catalog.getNames();
                PDEmbeddedFilesNameTreeNode embeddedFiles = names.getEmbeddedFiles();

                Map embeddedFileNames = embeddedFiles.getNames();
                embeddedFileNamesNet = embeddedFileNames.ToDictionary <String, PDComplexFileSpecification>();

                AttachmentsPanel.Children.Clear();
                //For-Each Loop is used to list all embedded files (if there is more than one)
                foreach (KeyValuePair <String, PDComplexFileSpecification> entry in embeddedFileNamesNet)
                {
                    StackPanel attachmentPanel = new StackPanel();
                    attachmentPanel.Orientation = Orientation.Vertical;
                    attachmentPanel.Margin      = new Thickness(5);
                    attachmentPanel.MinWidth    = 90;
                    System.Windows.Controls.Image attachmentImage = new System.Windows.Controls.Image();
                    attachmentImage.Height = 32;
                    attachmentImage.Width  = 32;

                    attachmentPanel.Tag = entry.Key;

                    attachmentPanel.MouseEnter += AttachmentPanel_MouseEnter;
                    attachmentPanel.MouseLeave += AttachmentPanel_MouseLeave;
                    attachmentPanel.MouseUp    += AttachmentPanel_MouseUp;

                    Icon         attachmentIcon         = ShellIcon.GetLargeIconFromExtension(System.IO.Path.GetExtension(entry.Key));
                    BitmapSource attachmentBitmapSource = Imaging.CreateBitmapSourceFromHIcon(attachmentIcon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                    attachmentImage.Source = attachmentBitmapSource;
                    attachmentImage.Margin = new Thickness(5, 5, 5, 0);
                    attachmentIcon.Dispose();

                    TextBlock attachmentTextBlock = new TextBlock();
                    attachmentTextBlock.Margin = new Thickness(5);
                    attachmentTextBlock.Text   = System.IO.Path.GetFileName(entry.Key);

                    System.Windows.Controls.Image deleteImage = new System.Windows.Controls.Image();
                    deleteImage.Height = 20;
                    deleteImage.Width  = 20;
                    deleteImage.Margin = new Thickness(0, 6, -50, -6);

                    // var uriSource = new Uri(@"/PDFBox_sharp;component/DeleteIcon.png", UriKind.Relative);
                    // deleteImage.Source = new BitmapImage(uriSource);

                    // attachmentPanel.Children.Add(deleteImage);
                    attachmentPanel.Children.Add(attachmentImage);
                    attachmentPanel.Children.Add(attachmentTextBlock);

                    AttachmentsPanel.Children.Add(attachmentPanel);
                }

                Icon         sysicon = System.Drawing.Icon.ExtractAssociatedIcon(PDFPath);
                BitmapSource bmpSrc  = Imaging.CreateBitmapSourceFromHIcon(sysicon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                PDFIcon.Source = bmpSrc;
                sysicon.Dispose();
            }
            //pd.close();
        }
Esempio n. 11
0
        // Decent example here -- https://github.com/Aiybe/PDFData/blob/master/od-reader/src/main/java/im/abe/pdfdata/AttachmentDataStorage.java

        private void AttachmentZone_Drop(object sender, DragEventArgs e)
        {
            // reset UI
            AttachmentZoneBorder.BorderThickness = new Thickness(1);
            AttachmentZone.Background            = new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 255, 255));
            deselectAllAttachmentPanels();

            // attach files to PDF
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                // Note that you can have more than one file.
                string[]      files     = (string[])e.Data.GetData(DataFormats.FileDrop);
                List <string> filesList = files.ToList();

                if (pd != null)
                {
                    foreach (string file in filesList)
                    {
                        byte[] fileBytes;
                        try
                        {
                            fileBytes = System.IO.File.ReadAllBytes(file);
                        }
                        catch
                        {
                            // couldn't read file
                            MessageBox.Show("Can't open " + file + " make sure it is not open in another application.", "Can't open file.");
                            continue;
                        }
                        FileStream _filestream        = System.IO.File.Open(file, FileMode.Open);
                        string     nameOfFileToAttach = _filestream.Name;
                        _filestream.Close();

                        PDComplexFileSpecification fs = new PDComplexFileSpecification();
                        fs.setFile(nameOfFileToAttach);

                        ByteArrayInputStream inputStream = new ByteArrayInputStream(fileBytes, 0, fileBytes.Length);
                        //ByteArrayInputStream inputStream = new ByteArrayInputStream(fileBytes);
                        PDEmbeddedFile embeddedFile = new PDEmbeddedFile(pd, inputStream);
                        embeddedFile.setModDate(java.util.Calendar.getInstance());
                        embeddedFile.setSize(fileBytes.Length);
                        fs.setEmbeddedFile(embeddedFile);

                        PDDocumentCatalog        catalog = pd.getDocumentCatalog();
                        PDDocumentNameDictionary names   = catalog.getNames();

                        Map TomsNewMap = new HashMap();
                        if (names != null)
                        {
                            // there are already some attached files
                            PDEmbeddedFilesNameTreeNode embeddedFiles = names.getEmbeddedFiles();
                            Map embeddedFileNames = embeddedFiles.getNames();
                            Dictionary <String, PDComplexFileSpecification> embeddedFileNamesNet = embeddedFileNames.ToDictionary <String, PDComplexFileSpecification>();


                            // Attach all the existing files
                            foreach (KeyValuePair <String, PDComplexFileSpecification> entry in embeddedFileNamesNet)
                            {
                                TomsNewMap.put(entry.Key, entry.Value);
                            }
                        }
                        else
                        {
                            // there are no files already attached -- so create a new name dictionary
                            names = new PDDocumentNameDictionary(catalog);
                        }

                        // Attach the new file
                        TomsNewMap.put(System.IO.Path.GetFileName(nameOfFileToAttach), fs);
                        PDEmbeddedFilesNameTreeNode TomsEmbeddedFiles = new PDEmbeddedFilesNameTreeNode();
                        TomsEmbeddedFiles.setNames(TomsNewMap);
                        names.setEmbeddedFiles(TomsEmbeddedFiles);
                        catalog.setNames(names);

                        pd.save(pathToCurrentPDF);
                        pd.close();
                    }
                }
                else
                {
                    MessageBoxResult result = MessageBox.Show("No PDF file loaded.");
                }
            }

            // reload the PDF
            LoadPDFAndLookForAttachments(pathToCurrentPDF);
        }
        /**
         * Embeds an external file (generic - any type allowed) in the PDF.
         *
         * @param doc
         *            PDDocument to attach the file to.
         * @param filename
         *            name of the file that will become attachment name in the PDF
         * @param relationship
         *            how the file relates to the content, e.g. "Alternative"
         * @param description
         *            Human-readable description of the file content
         * @param subType
         *            type of the data e.g. could be "text/xml" - mime like
         * @param data
         *            the binary data of the file/attachment
         */
        public void PDFAttachGenericFile(PDDocument doc, String filename,
                                         String relationship, String description, String subType, byte[] data)
        {
            PDComplexFileSpecification fs = new PDComplexFileSpecification();

            fs.setFile(filename);

            COSDictionary dict = fs.getCOSDictionary();

            dict.setName("AFRelationship", relationship);
            dict.setString("UF", filename);
            dict.setString("Desc", description);

            ByteArrayInputStream fakeFile = new ByteArrayInputStream(data);
            PDEmbeddedFile       ef       = new PDEmbeddedFile(doc, fakeFile);

            ef.setSubtype(subType);
            ef.setSize(data.Length);
            ef.setCreationDate(new GregorianCalendar());

            ef.setModDate(GregorianCalendar.getInstance());

            fs.setEmbeddedFile(ef);

            // In addition make sure the embedded file is set under /UF
            dict = fs.getCOSDictionary();
            COSDictionary efDict = (COSDictionary)dict
                                   .getDictionaryObject(COSName.EF);
            COSBase lowerLevelFile = efDict.getItem(COSName.F);

            efDict.setItem(COSName.UF, lowerLevelFile);

            // now add the entry to the embedded file tree and set in the document.
            PDDocumentNameDictionary names = new PDDocumentNameDictionary(
                doc.getDocumentCatalog());
            PDEmbeddedFilesNameTreeNode efTree = names.getEmbeddedFiles();

            if (efTree == null)
            {
                efTree = new PDEmbeddedFilesNameTreeNode();
            }

            //Map<String, COSObjectable> namesMap = new HashMap<String, COSObjectable>();
            //      Map<String, COSObjectable> oldNamesMap = efTree.getNames();
            //if (oldNamesMap != null) {
            // for (String key : oldNamesMap.keySet()) {
            //  namesMap.put(key, oldNamesMap.get(key));
            // }
            //}
            //efTree.setNames(namesMap);
            //should be ported more exactly...
            efTree.setNames(Collections.singletonMap(filename, fs));

            names.setEmbeddedFiles(efTree);
            doc.getDocumentCatalog().setNames(names);

            // AF entry (Array) in catalog with the FileSpec
            COSArray cosArray = (COSArray)doc.getDocumentCatalog()
                                .getCOSDictionary().getItem("AF");

            if (cosArray == null)
            {
                cosArray = new COSArray();
            }
            cosArray.add(fs);
            COSDictionary dict2 = doc.getDocumentCatalog().getCOSDictionary();
            COSArray      array = new COSArray();

            array.add(fs.getCOSDictionary()); // see below
            dict2.setItem("AF", array);
            doc.getDocumentCatalog().getCOSDictionary().setItem("AF", cosArray);
        }
        private PDDocumentCatalog makeDocPDFA3compliant(String producer,
                                                        String creator, bool attachZugferdHeaders)
        {
            String fullProducer = producer + " (via mustangproject.org "
                                  + versionStr + ")";

            PDDocumentCatalog cat      = doc.getDocumentCatalog();
            PDMetadata        metadata = new PDMetadata(doc);

            cat.setMetadata(metadata);
            // we're using the jempbox org.apache.jempbox.xmp.XMPMetadata version,
            // not the xmpbox one
            XMPMetadata xmp = new XMPMetadata();

            XMPSchemaPDFAId pdfaid = new XMPSchemaPDFAId(xmp);

            pdfaid.setAbout(""); //$NON-NLS-1$
            xmp.addSchema(pdfaid);

            XMPSchemaDublinCore dc = xmp.addDublinCoreSchema();

            dc.addCreator(creator);
            dc.setAbout(""); //$NON-NLS-1$

            XMPSchemaBasic xsb = xmp.addBasicSchema();

            xsb.setAbout(""); //$NON-NLS-1$

            xsb.setCreatorTool(creator);
            xsb.setCreateDate(GregorianCalendar.getInstance());
            // PDDocumentInformation pdi=doc.getDocumentInformation();
            PDDocumentInformation pdi = new PDDocumentInformation();

            pdi.setProducer(fullProducer);
            pdi.setAuthor(creator);
            doc.setDocumentInformation(pdi);

            XMPSchemaPDF pdf = xmp.addPDFSchema();

            pdf.setProducer(fullProducer);
            pdf.setAbout(""); //$NON-NLS-1$

            /*
             * // Mandatory: PDF/A3-a is tagged PDF which has to be expressed using
             * a // MarkInfo dictionary (PDF A/3 Standard sec. 6.7.2.2) PDMarkInfo
             * markinfo = new PDMarkInfo(); markinfo.setMarked(true);
             * doc.getDocumentCatalog().setMarkInfo(markinfo);
             */
            /*
             *
             * To be on the safe side, we use level B without Markinfo because we
             * can not guarantee that the user correctly tagged the templates for
             * the PDF.
             */
            pdfaid.setConformance(conformanceLevel);//$NON-NLS-1$ //$NON-NLS-1$

            pdfaid.setPart(new java.lang.Integer(3));

            if (attachZugferdHeaders)
            {
                addZugferdXMP(xmp); /*
                                     * this is the only line where we do something
                                     * Zugferd-specific, i.e. add PDF metadata
                                     * specifically for Zugferd, not generically for
                                     * a embedded file
                                     */
            }

            metadata.importXMPMetadata(xmp);
            return(cat);
        }
        public Dictionary<string, PDFField> GetPDFFormFields(PDDocument document, bool includeValues)
        {
            Dictionary<string, PDFField> pdfFormFields = new Dictionary<string, PDFField>();

            PDDocumentCatalog docCat = document.getDocumentCatalog();
            PDAcroForm form = docCat.getAcroForm();

            string aa = string.Empty;
            var a = form.getFields();

            var iterator = a.iterator();

            while (iterator.hasNext())
            {
                try
                {
                    PDFField pdffield = new PDFField();

                    PDField f = (PDField)iterator.next();

                    pdffield.Type = f.getFieldType();
                    pdffield.IsRequired = f.isRequired();
                    pdffield.IsReadOnly = f.isReadonly();
                    pdffield.FullName = f.getFullyQualifiedName();
                    pdffield.AlternativeName = f.getAlternateFieldName();
                    pdffield.PartialName = f.getPartialName();

                    string fieldvalue = string.Empty;

                    // sig field throws not implemented in ver 1.2.1
                    if (includeValues)
                    {
                        try
                        {
                            fieldvalue = f.getValue();
                        }
                        catch (Exception e) { }

                    }

                    if (pdffield.Type == "Sig")
                    {
                        PDSignatureField sig = (PDSignatureField)f;
                        var x = sig.getSignature();
                        if (x != null)
                        {
                            fieldvalue = x.getName();
                        }
                    }

                    pdffield.FieldValue = fieldvalue;

                    pdfFormFields.Add(pdffield.FullName, pdffield);
                }
                catch (Exception e) { }
            }

            return pdfFormFields;
        }
Esempio n. 15
0
        public static void ConvertToDoc(string pdfFilePath, string tempDir)
        {
            var pdfHeader       = System.IO.Path.GetFileName(pdfFilePath);
            var underscoreIndex = pdfHeader.IndexOf('_');

            pdfHeader = pdfHeader.Remove(0, underscoreIndex + 1);

            WordEx.AddTitle(pdfHeader);

            _pdfDoc = new PDDocument();

            try
            {
                _pdfDoc = PDDocument.load(pdfFilePath);
            }
            catch
            {
                MessageBox.Show("Cant load pdf, try re-downloading:" + Environment.NewLine + pdfFilePath, "PDF Error");
                return;
            }

            var pagelist = _pdfDoc.getDocumentCatalog().getAllPages();

            for (int x = 0; x < pagelist.size(); x++)
            {
                //string pageTxt = GetPageText(x);
                //PDFPage pdfPage = GetPageHeader(pageTxt);

                //WordEx.AddHeader(pdfPage.Header);

                PDPage page = (PDPage)pagelist.get(x);

                PDResources pdResources = page.getResources();

                Map pageImages = pdResources.getImages();
                if (pageImages != null)
                {
                    Iterator imageIter = pageImages.keySet().iterator();
                    while (imageIter.hasNext())
                    {
                        String         key            = (String)imageIter.next();
                        PDXObjectImage pdxObjectImage = (PDXObjectImage)pageImages.get(key);
                        var            buffImage      = pdxObjectImage.getRGBImage();
                        Bitmap         theImage       = buffImage.getBitmap();
                        if (!ContainsDocImage(theImage))
                        {
                            //WordEx.AddImage(theImage, pdfPage.Header, pdfHeader);
                            WordEx.AddImage(theImage, pdfHeader, pdfHeader);
                        }
                        else
                        {
                            theImage.Dispose();
                        }
                    }
                }
            }
            string docText = _stripper.getText(_pdfDoc);

            WordEx.AddText(docText);

            foreach (Bitmap btmap in _docImages)
            {
                btmap.Dispose();
            }
            _docImages.Clear();
            _pdfDoc.close();
            _pdfDoc = null;
        }