示例#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);
        }
示例#2
0
        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;
        }
示例#3
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);
        }