Exemplo n.º 1
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;
        }
Exemplo n.º 2
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();
        }
Exemplo n.º 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);
        }