Esempio n. 1
0
// ---------------------------------------------------------------------------

        /**
         * Create an XML file with named destinations
         * @param src the PDF with the destinations
         */
        public string CreateXml(byte[] src)
        {
            PdfReader reader = new PdfReader(src);
            Dictionary <string, string> map = SimpleNamedDestination
                                              .GetNamedDestination(reader, false);

            using (MemoryStream ms = new MemoryStream()) {
                SimpleNamedDestination.ExportToXML(map, ms, "ISO8859-1", true);
                ms.Position = 0;
                using (StreamReader sr = new StreamReader(ms)) {
                    return(sr.ReadToEnd());
                }
            }
        }
        // ---------------------------------------------------------------------------
        public void Write(Stream stream)
        {
            using (ZipFile zip = new ZipFile())
            {
                // Use previous examples to create PDF files
                MovieLinks1 m    = new MovieLinks1();
                byte[]      pdfM = Utility.PdfBytes(m);
                LinkActions l    = new LinkActions();
                byte[]      pdfL = l.CreatePdf();
                // Create readers.
                PdfReader[] readers =
                {
                    new PdfReader(pdfL),
                    new PdfReader(pdfM)
                };

                // step 1
                //Document document = new Document();
                // step 2
                using (var ms = new MemoryStream())
                {
                    // step 1
                    using (Document document = new Document())
                    {
                        using (PdfCopy copy = new PdfCopy(document, ms))
                        {
                            // step 3
                            document.Open();
                            // step 4
                            int n;
                            // copy the pages of the different PDFs into one document
                            for (int i = 0; i < readers.Length; i++)
                            {
                                readers[i].ConsolidateNamedDestinations();
                                n = readers[i].NumberOfPages;
                                for (int page = 0; page < n;)
                                {
                                    copy.AddPage(copy.GetImportedPage(readers[i], ++page));
                                }
                            }
                            // Add named destination
                            copy.AddNamedDestinations(
                                // from the second document
                                SimpleNamedDestination.GetNamedDestination(readers[1], false),
                                // using the page count of the first document as offset
                                readers[0].NumberOfPages
                                );
                        }
                        zip.AddEntry(RESULT1, ms.ToArray());
                    }

                    // Create a reader
                    PdfReader reader = new PdfReader(ms.ToArray());
                    // Convert the remote destinations into local destinations
                    reader.MakeRemoteNamedDestinationsLocal();
                    using (MemoryStream ms2 = new MemoryStream())
                    {
                        // Create a new PDF containing the local destinations
                        using (PdfStamper stamper = new PdfStamper(reader, ms2))
                        {
                        }
                        zip.AddEntry(RESULT2, ms2.ToArray());
                    }
                }
                zip.AddEntry(Utility.ResultFileName(m.ToString() + ".pdf"), pdfM);
                zip.AddEntry(Utility.ResultFileName(l.ToString() + ".pdf"), pdfL);
                zip.Save(stream);
            }
        }
Esempio n. 3
0
        public static bool MergePDFs(IEnumerable <string> fileNames, string targetPdf)
        {
            bool merged   = true;
            var  Bookmark = new List <Dictionary <string, object> >();

            using (var stream = new MemoryStream())
            {
                var       document = new Document();
                var       pdf      = new PdfCopy(document, stream);
                PdfReader reader   = null;
                try
                {
                    document.Open();
                    var NumberOfPages = 0;
                    foreach (string file in fileNames)
                    {
                        var FileName = System.IO.Path.GetFileName(file);
                        using (reader = new PdfReader(file))
                        {
                            var IndividualBookmark = SimpleBookmark.GetBookmark(reader);
                            if (IndividualBookmark != null)
                            {
                                AddPageNumber(IndividualBookmark, SimpleNamedDestination.GetNamedDestination(reader, false), NumberOfPages);
                            }

                            Bookmark.Add(new Dictionary <string, object> {
                                { "Title", FileName.Substring(0, FileName.IndexOf('.')) },
                                { "Open", "false" },
                                { "Page", (NumberOfPages + 1) + " Fit" },
                                { "Action", "GoTo" },
                                { "Kids", IndividualBookmark }
                            });

                            NumberOfPages += reader.NumberOfPages;
                            pdf.AddDocument(reader);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    merged = false;
                    if (reader != null)
                    {
                        reader.Close();
                    }

                    throw;
                }
                finally
                {
                    if (document != null)
                    {
                        document.Close();
                    }
                }

                using (var NewReader = new PdfReader(stream.ToArray()))
                    using (var fileStream = new FileStream(targetPdf, FileMode.Create))
                        using (var Stamper = new PdfStamper(NewReader, fileStream))
                        {
                            stream.Dispose();
                            Stamper.Outlines = Bookmark;
                        }
            }

            return(merged);
        }