Search code examples
c#asp.netpdfconcatenation

Concatenate PDF files and insert a page into a PDF File


I’ve got this new task of creating a web app (I will use Asp.net + C#): • The customer gives us an X number of PDF files everyday (x will be different from day to day) • My application will need to take this PDF files and insert a blank page after page #3 (not each 3 pages, just after page #3) of each PDF file and then concatenate all of those PDF files them into one large PDF file. I was thinking about using Aspose, since it seems like it can concatenate pdf files, but i will have to check if it can also insert a page inside a pdf file.

Are there any other plugins , webservices, code behind or even a technique that you are aware that does just that ?


Solution

  • The task can be accomplished quite easily with Docotic.Pdf library.

    Here is the code that merges files while adding blank pages after third page in each file.

    public static void insertBlanksAndMerge()
    {
        string[] filesToMerge = { "file1.pdf", "file2.pdf" };
        
        // open first file
        int pagesBefore = 0;
        using (PdfDocument pdf = new PdfDocument(filesToMerge[0]))
        {
            pdf.InsertPage(pagesBefore + 3);
    
            // append all other documents
            for (int i = 1; i < filesToMerge.Length; i++)
            {
                pagesBefore = pdf.PageCount;
    
                pdf.Append(filesToMerge[i]);
                pdf.InsertPage(pagesBefore + 3);
            }
    
            pdf.Save(@"out.pdf");
        }
    }
    

    Please note that PdfDocument constructor and Append method can use not only file names but also streams and byte buffers.

    More samples available on GitHub.

    Disclaimer: I am one of the developers of the library.