Example #1
0
 // ---------------------------------------------------------------------------
 public void Write(Stream stream)
 {
     MovieLinks1 ml = new MovieLinks1();
     MovieHistory mh = new MovieHistory();
     List<byte[]> pdf = new List<byte[]>() 
     {
         Utility.PdfBytes(ml),
         Utility.PdfBytes(mh)
     };
     string[] names = { ml.ToString(), mh.ToString() };
     using (ZipFile zip = new ZipFile())
     {
         using (MemoryStream ms = new MemoryStream())
         {
             // step 1
             using (Document document = new Document())
             {
                 // step 2
                 using (PdfCopy copy = new PdfCopy(document, ms))
                 {
                     // step 3
                     document.Open();
                     // step 4
                     for (int i = 0; i < pdf.Count; ++i)
                     {
                         zip.AddEntry(Utility.ResultFileName(names[i] + ".pdf"), pdf[i]);
                         PdfReader reader = new PdfReader(pdf[i]);
                         // loop over the pages in that document
                         int n = reader.NumberOfPages;
                         for (int page = 0; page < n; )
                         {
                             copy.AddPage(copy.GetImportedPage(reader, ++page));
                         }
                         copy.FreeReader(reader);
                         reader.Close();
                     }
                 }
             }
             zip.AddEntry(RESULT, ms.ToArray());
         }
         zip.Save(stream);
     }
 }
        // ---------------------------------------------------------------------------    
        public void Write(Stream stream)
        {
            using (ZipFile zip = new ZipFile())
            {
                MovieLinks1 ml = new MovieLinks1();
                byte[] r1 = Utility.PdfBytes(ml);
                MovieHistory mh = new MovieHistory();
                byte[] r2 = Utility.PdfBytes(mh);
                using (MemoryStream ms = new MemoryStream())
                {
                    // step 1
                    using (Document document = new Document())
                    {
                        // step 2
                        using (PdfCopy copy = new PdfCopy(document, ms))
                        {
                            // step 3
                            document.Open();
                            // step 4
                            // reader for document 1
                            PdfReader reader1 = new PdfReader(r1);
                            int n1 = reader1.NumberOfPages;
                            // reader for document 2
                            PdfReader reader2 = new PdfReader(r2);
                            int n2 = reader2.NumberOfPages;
                            // initializations
                            PdfImportedPage page;
                            PdfCopy.PageStamp stamp;
                            // Loop over the pages of document 1
                            for (int i = 0; i < n1; )
                            {
                                page = copy.GetImportedPage(reader1, ++i);
                                stamp = copy.CreatePageStamp(page);
                                // add page numbers
                                ColumnText.ShowTextAligned(
                                  stamp.GetUnderContent(), Element.ALIGN_CENTER,
                                  new Phrase(string.Format("page {0} of {1}", i, n1 + n2)),
                                  297.5f, 28, 0
                                );
                                stamp.AlterContents();
                                copy.AddPage(page);
                            }

                            // Loop over the pages of document 2
                            for (int i = 0; i < n2; )
                            {
                                page = copy.GetImportedPage(reader2, ++i);
                                stamp = copy.CreatePageStamp(page);
                                // add page numbers
                                ColumnText.ShowTextAligned(
                                  stamp.GetUnderContent(), Element.ALIGN_CENTER,
                                  new Phrase(string.Format("page {0} of {1}", n1 + i, n1 + n2)),
                                  297.5f, 28, 0
                                );
                                stamp.AlterContents();
                                copy.AddPage(page);
                            }
                            reader1.Close();
                            reader2.Close();
                        }
                    }
                    zip.AddEntry(RESULT, ms.ToArray());
                    zip.AddEntry(Utility.ResultFileName(ml.ToString() + ".pdf"), r1);
                    zip.AddEntry(Utility.ResultFileName(mh.ToString() + ".pdf"), r2);
                }
                zip.Save(stream);
            }
        }
        // ---------------------------------------------------------------------------    
        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);
            }
        }