Api to read array of PDF files and then combine into single PDF

Vaibhav Ganesh Kaulkar 1 Reputation point
2022-01-03T11:55:16.787+00:00

Hi, is there a way to have an Api to read array of PDF files and then combine into single PDF using itext 7? TIA

Community Center Not monitored
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2022-01-03T12:15:32.38+00:00

  2. Vaibhav Ganesh Kaulkar 1 Reputation point
    2022-01-03T15:08:04.177+00:00

    Managed to write a function that is combining 2 pdfs into 1 file. I used a console application. Not sure whether the same I can reuse within API.

    private static void createPdf(string DEST,string SRC1,string SRC2)
            {
                //Initialize PDF document with output intent
                PdfDocument pdf = new PdfDocument(new PdfWriter(DEST));
                //PdfMerger merger = new PdfMerger(pdf);
                PdfMerger merger = new PdfMerger(pdf);
    
                //Add pages from the first document
                PdfReader reader = new PdfReader(SRC1);
                reader.SetUnethicalReading(true);
                PdfDocument firstSourcePdf = new PdfDocument(reader);           
                merger.Merge(firstSourcePdf, 1, firstSourcePdf.GetNumberOfPages());
    
                //Add pages from the second pdf document
                PdfReader reader1 = new PdfReader(SRC2);
                reader1.SetUnethicalReading(true);
                PdfDocument secondSourcePdf = new PdfDocument(reader1);
                merger.Merge(secondSourcePdf, 1, secondSourcePdf.GetNumberOfPages());
    
                firstSourcePdf.Close();
                secondSourcePdf.Close();
                pdf.Close();
            }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.