Merge several PDF documents into a single document in memory

Namespace:  SautinSoft
Assembly:  PdfMetamorphosis (in PdfMetamorphosis.dll) Version: 6.7.12.19

Syntax

      
 C#  Visual Basic 
public byte[] MergePDFStreamArrayToPDFStream(
	ArrayList pdfBytes
)
Public Function MergePDFStreamArrayToPDFStream ( _
	pdfBytes As ArrayList _
) As Byte()

Parameters

pdfBytes
ArrayList
ArrayList which contains PDF documents as array of bytes

Return Value

Single PDF document as byte array or null in case of merging failed

Remarks

Examples

CopyHow to merge PDF documents in memory using C#
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Result.Text = "";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SautinSoft.PdfMetamorphosis p = new SautinSoft.PdfMetamorphosis();
        //After purchasing the license, please insert your serial number here to activate the component
        //p.Serial = "XXXXXXXXXXX";

        ArrayList pdfs = new ArrayList();
        int files = 0;

        if (FileUpload1.FileBytes.Length > 0)
        {
            pdfs.Add(FileUpload1.FileBytes);
            files++;
        }
        if (FileUpload2.FileBytes.Length > 0)
        {
            pdfs.Add(FileUpload2.FileBytes);
            files++;
        }
        if (FileUpload3.FileBytes.Length > 0)
        {
            pdfs.Add(FileUpload3.FileBytes);
            files++;
        }

        if (files < 2)
        {
            Result.Text = "Error! Before merging please select at least two PDF documents!";
            return;
        }

        byte[] pdfBytes = null;

        //merge
        pdfBytes = p.MergePDFStreamArrayToPDFStream(pdfs);


        //show PDF
        if (pdfBytes != null)
        {
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = "application/PDF";
            Response.AppendHeader("content-disposition", "attachment; filename=single.pdf");
            Response.BinaryWrite(pdfBytes);
            Response.Flush();
            Response.End();
        }
        else
        {
            Result.Text = "Error in merging these PDF files, email to support@sautinsoft.com!";
        }
    }
}
CopyHow to merge PDF documents in memory using VB.Net
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Collections

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Result.Text = ""
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim p As New SautinSoft.PdfMetamorphosis()

        'After purchasing the license, please insert your serial number here to activate the component
        'p.Serial = "XXXXXXXXXXX"


        Dim pdfs As New ArrayList()
        Dim files As Integer = 0

        If FileUpload1.FileBytes.Length > 0 Then
            pdfs.Add(FileUpload1.FileBytes)
            files += 1
        End If
        If FileUpload2.FileBytes.Length > 0 Then
            pdfs.Add(FileUpload2.FileBytes)
            files += 1
        End If
        If FileUpload3.FileBytes.Length > 0 Then
            pdfs.Add(FileUpload3.FileBytes)
            files += 1
        End If

        If files < 2 Then
            Result.Text = "Error! Before merging please select at least two PDF documents!"
            Return
        End If

        Dim pdfBytes() As Byte = Nothing

        'merge
        pdfBytes = p.MergePDFStreamArrayToPDFStream(pdfs)


        'show PDF
        If pdfBytes IsNot Nothing Then
            Response.Buffer = True
            Response.Clear()
            Response.ContentType = "application/PDF"
            Response.AppendHeader("content-disposition", "attachment; filename=single.pdf")
            Response.BinaryWrite(pdfBytes)
            Response.Flush()
            Response.End()
        Else
            Result.Text = "Error in merging these PDF files, email to support@sautinsoft.com!"
        End If
    End Sub
End Class

See Also