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); }
private void DeleteFile_Click(object sender, RoutedEventArgs e) { // to delete we just mark for not to be reincluded and do a round of saving PDDocumentCatalog catalog = pd.getDocumentCatalog(); PDDocumentNameDictionary names = catalog.getNames(); PDEmbeddedFilesNameTreeNode embeddedFiles = names.getEmbeddedFiles(); Map embeddedFileNames = embeddedFiles.getNames(); Dictionary <String, PDComplexFileSpecification> embeddedFileNamesNet = embeddedFileNames.ToDictionary <String, PDComplexFileSpecification>(); Map TomsNewMap = new HashMap(); // Attach all the existing files foreach (KeyValuePair <String, PDComplexFileSpecification> entry in embeddedFileNamesNet) { if (selectedFile == entry.Key) { // } else { TomsNewMap.put(entry.Key, entry.Value); } } PDEmbeddedFilesNameTreeNode TomsEmbeddedFiles = new PDEmbeddedFilesNameTreeNode(); TomsEmbeddedFiles.setNames(TomsNewMap); names.setEmbeddedFiles(TomsEmbeddedFiles); catalog.setNames(names); pd.save(pathToCurrentPDF); // reload the PDF LoadPDFAndLookForAttachments(pathToCurrentPDF); ButtonsStackPanel.Visibility = Visibility.Hidden; }
/** * 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); }