// --------------------------------------------------------------------------- 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); } }
public void MergeNamedDestinationsTest() { string outputFolder = "PdfCopyTest/"; string outputFile = "namedDestinations.pdf"; Directory.CreateDirectory(outputFolder); // Create simple document MemoryStream main = new MemoryStream(); Document doc = new Document(new Rectangle(612f,792f),54f,54f,36f,36f); PdfWriter pdfwrite = PdfWriter.GetInstance(doc, main); doc.Open(); doc.Add(new Paragraph("Testing Page")); doc.Close(); // Create TOC document MemoryStream two = new MemoryStream(); Document doc2 = new Document(new Rectangle(612f,792f),54f,54f,36f,36f); PdfWriter pdfwrite2 = PdfWriter.GetInstance(doc2, two); doc2.Open(); Chunk chn = new Chunk("<<-- Link To Testing Page -->>"); chn.SetRemoteGoto("DUMMY.PDF","page-num-1"); doc2.Add(new Paragraph(chn)); doc2.Close(); // Merge documents MemoryStream three = new MemoryStream(); PdfReader reader1 = new PdfReader(main.ToArray()); PdfReader reader2 = new PdfReader(two.ToArray()); Document doc3 = new Document(); PdfCopy DocCopy = new PdfCopy(doc3,three); doc3.Open(); DocCopy.AddPage(DocCopy.GetImportedPage(reader2,1)); DocCopy.AddPage(DocCopy.GetImportedPage(reader1,1)); DocCopy.AddNamedDestination("page-num-1",2,new PdfDestination(PdfDestination.FIT)); doc3.Close(); // Fix references and write to file PdfReader finalReader = new PdfReader(three.ToArray()); finalReader.MakeRemoteNamedDestinationsLocal(); PdfStamper stamper = new PdfStamper(finalReader,new FileStream(outputFolder + outputFile, FileMode.Create)); stamper.Close(); CompareTool compareTool = new CompareTool(); String errorMessage = compareTool.CompareByContent(outputFolder + outputFile, RESOURCES + "cmp_" + outputFile, outputFolder, "diff"); if (errorMessage != null) { Assert.Fail(errorMessage); } }