Combine PDF Files

The code below shows you how to combine multiple PDF files into a single PDF.

This sample requires a Tools license.

C#
using System;
using System.IO;
using SolidFramework.Model.Pdf.Pages;
namespace CombinePDF
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Call your Solid Documents License
            SolidFramework.License.Import(@"C\:MyFolder\license.xml");

            // Set the location of your the file you want to convert
            String pdfPath = @"C:\YourFolder";

            //*Combining Multiple PDF Files*//

            // Create a new pages model object
            using (PagesModel model = new PagesModel())
            {
                // Get a list of pdf files in the folder.
                string[] pdfFilesPath = Directory.GetFiles(pdfPath, "*.pdf");
                // Create a IPageSource for the selected files then add each of the files to one source
                foreach (string filename in pdfFilesPath)
                {
                    // For each file add it to a source and collate them
                    SolidFramework.Model.Pdf.Pages.IPageSource source = model.CreateSource(filename);
                    model.Insert(model.PagesCount, source);
                }

                // Save the new combined file 
                model.Save(@"C:\MyFolder\combined_pdf.pdf");
            }
        }
    }
}
C++

#include "stdafx.h"
#include "SolidFramework.h"

using namespace std;

int main(int argc, _TCHAR* argv[])
{
    // Setup the license
    SolidFramework::License::Import(L"C:\\MyFolder\\license.xml");

    // Get a list of files from a directory
    vector<wstring, allocator<wstring="">> fileList = SolidFramework::Platform::Directory::GetFiles(L"c:\\YourFolder", false);</wstring,>

    // Get all the pdf file paths
    vector combineFiles = vector();
    for (vector::iterator it = fileList.begin(); it != fileList.end(); ++it)
    {
        if (SolidFramework::Platform::Path::GetExtension(it->c_str()) == L".pdf")
        {
            combineFiles.push_back(it->c_str());
        }
    }

    if (combineFiles.size() > 0)
    {
        // Open the first pdf file in the list.
        SolidFramework::Pdf::PdfDocumentPtr pDocument(new SolidFramework::Pdf::PdfDocument(combineFiles.at(0).c_str()));
        pDocument->Open();

        // Append all the other PDF file pages to the end of the first.
        for (unsigned x = 1; x < combineFiles.size(); x++) { // Open the next pdf file SolidFramework::Pdf::PdfDocumentPtr pNextDocument(new SolidFramework::Pdf::PdfDocument(combineFiles.at(x).c_str())); 
            pNextDocument->Open();
            // Append it to the first
            pDocument->Append(pNextDocument);
            // We are done with this one, next
            pNextDocument->Close();
            pNextDocument->Dispose();
        }

        // Save out the combined pdf
        pDocument->SaveAs(L"c:\\MyFolder\\combined.pdf", SolidFramework::Plumbing::OverwriteMode::ForceOverwrite);

        // Close it out and cleanup
        pDocument->Close();
        pDocument->Dispose();
    }

    return 0;
}

VB.Net

Imports System.IO
Imports SolidFramework

Module CombinePDF

    Sub Main()

        ' Call your Solid Documents License
        SolidFramework.License.Import("C:\MyFolder\license.xml")

        ' Define a variable for your source file
        Dim sPdfPath As String

        'Deifne a variable for you Pages Model
        Dim myModel As SolidFramework.Model.Pdf.Pages.PagesModel

        ' Define a variable for the folder that contains the PDF files you want to combine
        Dim pdfFilePath As StringsArray

        'Define a IPageSource model
        Dim mySource As SolidFramework.Model.Pdf.Pages.IPageSource

        ' Set values to your variables 
        sPdfPath = "C:\YourFolder\"
        myModel = New SolidFramework.Model.Pdf.Pages.PagesModel
        pdfFilePath = Directory.GetFiles(sPdfPath, "*.pdf")

        ' Get a list of pdf files in the folder and create a IPageSource
        For Each sPdfPath In pdfFilePath
            mySource = myModel.CreateSource(sPdfPath)
            myModel.Insert(myModel.PagesCount, mySource)
        Next

        myModel.Save("C:\MyFolder\combined_pdf.pdf")

        ' Clean up
        myModel.Dispose()

    End Sub

End Module