Exemplo 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);
        }