How to merge PDF files in memory using C# and VB.Net

How to merge PDF files in memory
using C# and VB.Net.


Complete code

using System;
using System.IO;
using System.Collections.Generic;

namespace Sample
{
    class Test
    {
        static void Main(string[] args)
        {

            // Merge PDF in memory
            SautinSoft.PdfVision v = new SautinSoft.PdfVision();

            //v.Serial = "XXXXXXXXXXXXXXX";

            // merge two PDF files
            string[] inpFiles = new string[] { @"..\..\simple text.pdf", @"..\..\table.pdf" };
           
            FileInfo outFile = new FileInfo("Result.pdf");

            List<byte[]> inpFilesByte = new List<byte[]>();

            // Fill the pdfBytesList.
            foreach (string inpFile in inpFiles)
                inpFilesByte.Add(File.ReadAllBytes(inpFile));

            byte[] pdfBytes = v.MergePDFStreamArrayToPDFStream(inpFilesByte);

            if (pdfBytes != null)
            {
                File.WriteAllBytes(outFile.FullName, pdfBytes);
				// Open the resulting PDF document in a default PDF Viewer.
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile.FullName) { UseShellExecute = true });
            }
        }
    }
}

Download.

        
            Imports System
Imports System.IO
Imports System.Collections.Generic

Namespace Sample
    Friend Class Test
        Shared Sub Main(ByVal args() As String)

            ' Merge PDF in memory
            Dim v As New SautinSoft.PdfVision()

            'v.Serial = "XXXXXXXXXXXXXXX"

            ' merge two PDF files
            Dim inpFiles() As String = {"..\..\simple text.pdf", "..\..\table.pdf"}

            Dim outFile As New FileInfo("Result.pdf")

            Dim inpFilesByte As New List(Of Byte())()

            ' Fill the pdfBytesList.
            For Each inpFile As String In inpFiles
                inpFilesByte.Add(File.ReadAllBytes(inpFile))
            Next inpFile

            Dim pdfBytes() As Byte = v.MergePDFStreamArrayToPDFStream(inpFilesByte)

            If pdfBytes IsNot Nothing Then
                File.WriteAllBytes(outFile.FullName, pdfBytes)
                ' Open the resulting PDF document in a default PDF Viewer.
                System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile.FullName) With {.UseShellExecute = True})
            End If
        End Sub
    End Class
End Namespace

Download.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.