public virtual void MergeDocumentTest01()
        {
            String      filename   = sourceFolder + "courierTest.pdf";
            String      filename1  = sourceFolder + "helveticaTest.pdf";
            String      filename2  = sourceFolder + "timesRomanTest.pdf";
            String      resultFile = destinationFolder + "mergedResult01.pdf";
            PdfReader   reader     = new PdfReader(filename);
            PdfReader   reader1    = new PdfReader(filename1);
            PdfReader   reader2    = new PdfReader(filename2);
            FileStream  fos1       = new FileStream(resultFile, FileMode.Create);
            PdfWriter   writer1    = new PdfWriter(fos1);
            PdfDocument pdfDoc     = new PdfDocument(reader);
            PdfDocument pdfDoc1    = new PdfDocument(reader1);
            PdfDocument pdfDoc2    = new PdfDocument(reader2);
            PdfDocument pdfDoc3    = new PdfDocument(writer1);
            PdfMerger   merger     = new PdfMerger(pdfDoc3).SetCloseSourceDocuments(true);

            merger.Merge(pdfDoc, 1, 1);
            merger.Merge(pdfDoc1, 1, 1);
            merger.Merge(pdfDoc2, 1, 1);
            pdfDoc3.Close();
            CompareTool compareTool  = new CompareTool();
            String      errorMessage = compareTool.CompareByContent(resultFile, sourceFolder + "cmp_mergedResult01.pdf", destinationFolder
                                                                    , "diff_");

            if (errorMessage != null)
            {
                NUnit.Framework.Assert.Fail(errorMessage);
            }
        }
        public virtual void MergeTableWithEmptyTdTest()
        {
            String      filename   = sourceFolder + "tableWithEmptyTd.pdf";
            String      resultFile = destinationFolder + "tableWithEmptyTdResult.pdf";
            PdfReader   reader     = new PdfReader(filename);
            PdfDocument sourceDoc  = new PdfDocument(reader);
            PdfDocument output     = new PdfDocument(new PdfWriter(resultFile));

            output.SetTagged();
            PdfMerger merger = new PdfMerger(output).SetCloseSourceDocuments(true);

            merger.Merge(sourceDoc, 1, sourceDoc.GetNumberOfPages());
            sourceDoc.Close();
            reader.Close();
            merger.Close();
            output.Close();
            CompareTool compareTool           = new CompareTool();
            String      tagStructErrorMessage = compareTool.CompareTagStructures(resultFile, sourceFolder + "cmp_tableWithEmptyTd.pdf"
                                                                                 );
            String errorMessage = tagStructErrorMessage == null ? "" : tagStructErrorMessage + "\n";

            if (!String.IsNullOrEmpty(errorMessage))
            {
                NUnit.Framework.Assert.Fail(errorMessage);
            }
        }
        public virtual void MergePdfWithComplexOCGTwiceTest()
        {
            String      pdfWithOCG = sourceFolder + "pdfWithComplexOCG.pdf";
            String      outPdf     = destinationFolder + "mergePdfWithComplexOCGTwiceTest.pdf";
            String      cmpPdf     = sourceFolder + "cmp_mergePdfWithComplexOCGTwiceTest.pdf";
            PdfDocument mergedDoc  = new PdfDocument(new PdfWriter(outPdf));
            PdfMerger   merger     = new PdfMerger(mergedDoc);
            PdfDocument sourcePdf  = new PdfDocument(new PdfReader(new FileInfo(pdfWithOCG)));

            // The test verifies that identical layers from the same document are not copied
            merger.Merge(sourcePdf, 1, sourcePdf.GetNumberOfPages());
            merger.Merge(sourcePdf, 1, sourcePdf.GetNumberOfPages());
            sourcePdf.Close();
            merger.Close();
            mergedDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, destinationFolder));
        }
        public virtual void MergeDocumentTest04()
        {
            String      filename   = sourceFolder + "pdf_open_parameters.pdf";
            String      filename1  = sourceFolder + "iphone_user_guide.pdf";
            String      resultFile = destinationFolder + "mergedResult04.pdf";
            PdfReader   reader     = new PdfReader(filename);
            PdfReader   reader1    = new PdfReader(filename1);
            FileStream  fos1       = new FileStream(resultFile, FileMode.Create);
            PdfWriter   writer1    = new PdfWriter(fos1);
            PdfDocument pdfDoc     = new PdfDocument(reader);
            PdfDocument pdfDoc1    = new PdfDocument(reader1);
            PdfDocument pdfDoc3    = new PdfDocument(writer1);

            pdfDoc3.SetTagged();
            PdfMerger   merger = new PdfMerger(pdfDoc3).SetCloseSourceDocuments(true);
            IList <int> pages  = new List <int>();

            pages.Add(3);
            pages.Add(2);
            pages.Add(1);
            merger.Merge(pdfDoc, pages);
            IList <int> pages1 = new List <int>();

            pages1.Add(5);
            pages1.Add(9);
            pages1.Add(4);
            pages1.Add(3);
            merger.Merge(pdfDoc1, pages1);
            merger.Close();
            CompareTool compareTool         = new CompareTool();
            String      errorMessage        = "";
            String      contentErrorMessage = compareTool.CompareByContent(resultFile, sourceFolder + "cmp_mergedResult04.pdf"
                                                                           , destinationFolder, "diff_");
            String tagStructErrorMessage = compareTool.CompareTagStructures(resultFile, sourceFolder + "cmp_mergedResult04.pdf"
                                                                            );

            errorMessage += tagStructErrorMessage == null ? "" : tagStructErrorMessage + "\n";
            errorMessage += contentErrorMessage == null ? "" : contentErrorMessage;
            if (!String.IsNullOrEmpty(errorMessage))
            {
                NUnit.Framework.Assert.Fail(errorMessage);
            }
        }
        private void MergePdfs(IList <FileInfo> sources, String destination)
        {
            PdfDocument mergedDoc = new PdfDocument(new PdfWriter(destination));
            PdfMerger   merger    = new PdfMerger(mergedDoc);

            foreach (FileInfo source in sources)
            {
                PdfDocument sourcePdf = new PdfDocument(new PdfReader(source));
                merger.Merge(sourcePdf, 1, sourcePdf.GetNumberOfPages()).SetCloseSourceDocuments(true);
                sourcePdf.Close();
            }
            merger.Close();
            mergedDoc.Close();
        }
Exemplo n.º 6
0
        public virtual void MergeOutlinesWithWrongStructureTest()
        {
            PdfDocument inputDoc  = new PdfDocument(new PdfReader(sourceFolder + "infiniteLoopInOutlineStructure.pdf"));
            PdfDocument outputDoc = new PdfDocument(new PdfWriter(destinationFolder + "infiniteLoopInOutlineStructure.pdf"
                                                                  ));
            PdfMerger merger = new PdfMerger(outputDoc, false, true);

            System.Console.Out.WriteLine("Doing merge");
            merger.Merge(inputDoc, 1, 2);
            merger.Close();
            System.Console.Out.WriteLine("Merge done");
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "infiniteLoopInOutlineStructure.pdf"
                                                                             , sourceFolder + "cmp_infiniteLoopInOutlineStructure.pdf", destinationFolder));
        }
        public virtual void MergeOutlinesNamedDestinations()
        {
            String      filename   = sourceFolder + "outlinesNamedDestinations.pdf";
            String      resultFile = destinationFolder + "mergeOutlinesNamedDestinations.pdf";
            PdfReader   reader     = new PdfReader(filename);
            PdfDocument sourceDoc  = new PdfDocument(reader);
            PdfDocument output     = new PdfDocument(new PdfWriter(resultFile));
            PdfMerger   merger     = new PdfMerger(output).SetCloseSourceDocuments(false);

            merger.Merge(sourceDoc, 2, 3);
            merger.Merge(sourceDoc, 2, 3);
            sourceDoc.Close();
            reader.Close();
            merger.Close();
            output.Close();
            CompareTool compareTool  = new CompareTool();
            String      errorMessage = compareTool.CompareByContent(resultFile, sourceFolder + "cmp_mergeOutlinesNamedDestinations.pdf"
                                                                    , destinationFolder, "diff_");

            if (errorMessage != null)
            {
                NUnit.Framework.Assert.Fail(errorMessage);
            }
        }
        public virtual void MergeDocumentOutlinesWithNullDestinationTest01()
        {
            String      resultFile     = destinationFolder + "mergeDocumentOutlinesWithNullDestinationTest01.pdf";
            String      filename       = sourceFolder + "null_dest_outline.pdf";
            PdfDocument sourceDocument = new PdfDocument(new PdfReader(filename));
            PdfMerger   resultDocument = new PdfMerger(new PdfDocument(new PdfWriter(resultFile)));

            resultDocument.Merge(sourceDocument, 1, 1);
            resultDocument.Close();
            sourceDocument.Close();
            CompareTool compareTool  = new CompareTool();
            String      errorMessage = compareTool.CompareByContent(resultFile, sourceFolder + "cmp_mergeDocumentOutlinesWithNullDestinationTest01.pdf"
                                                                    , destinationFolder, "diff_");

            if (errorMessage != null)
            {
                NUnit.Framework.Assert.Fail(errorMessage);
            }
        }
        public virtual void MergeTwoPagePdfWithComplexOCGTest()
        {
            String           pdfWithOCG1 = sourceFolder + "sourceOCG1.pdf";
            String           pdfWithOCG2 = sourceFolder + "twoPagePdfWithComplexOCGTest.pdf";
            String           outPdf      = destinationFolder + "mergeTwoPagePdfWithComplexOCGTest.pdf";
            String           cmpPdf      = sourceFolder + "cmp_mergeTwoPagePdfWithComplexOCGTest.pdf";
            PdfDocument      mergedDoc   = new PdfDocument(new PdfWriter(outPdf));
            PdfMerger        merger      = new PdfMerger(mergedDoc);
            IList <FileInfo> sources     = new List <FileInfo>();

            sources.Add(new FileInfo(pdfWithOCG1));
            sources.Add(new FileInfo(pdfWithOCG2));
            // The test verifies that are copying only those OCGs and properties that are used on the copied pages
            foreach (FileInfo source in sources)
            {
                PdfDocument sourcePdf = new PdfDocument(new PdfReader(source));
                merger.Merge(sourcePdf, 1, 1).SetCloseSourceDocuments(true);
                sourcePdf.Close();
            }
            merger.Close();
            mergedDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, destinationFolder));
        }
Exemplo n.º 10
0
        public static void MergePdf()
        {
            using (MemoryStream stream = new MemoryStream())
            {
                PdfDocument pdfDoc = new PdfDocument(new PdfWriter(stream));
                iText.Kernel.Utils.PdfMerger merger = new iText.Kernel.Utils.PdfMerger(pdfDoc);

                foreach (byte[] pdf in Pdfs)
                {
                    PdfDocument currentPdf = new PdfDocument(new PdfReader(new MemoryStream(pdf)));
                    merger.Merge(currentPdf, 1, currentPdf.GetNumberOfPages());
                    currentPdf.Close();
                }

                merger.Close();
                pdfDoc.Close();

                FinalMergedPdfURL = Format + Convert.ToBase64String(stream.ToArray());
            }

            IsSubmitComplete = true;
            IsMergeComplete  = true;
        }