Combine (Merge) multiple PDF files into one (single) using C# and VB.Net in ASP.Net

Last Reply 21 days ago By pandeyism

Posted 21 days ago

How to comebine 20 separated pdf files combine into 1 PDF file ?

 

Posted 21 days ago Modified on 20 days ago

Hi KatieNgoc,

Refer below sample code.

HTML

<asp:Button ID="Button1" Text="Merge" runat="server" OnClick="Merging" />

 Namespaces

C#

using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text;

VB.Net

Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO

Code

C#

protected void Merging(object sender, EventArgs e)
{
    List<PdfReader> pdfReaderList = new List<PdfReader>();
    foreach (string filePath in Directory.GetFiles(Server.MapPath("~/Files")))
    {
        PdfReader pdfReader = new PdfReader(filePath);
        pdfReaderList.Add(pdfReader);
    }
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Buffer = true;
    Document document = new Document(PageSize.A4, 0, 0, 0, 0);
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath("~/Folder/") + "OutPut.pdf", FileMode.Create));
    document.Open();
    foreach (PdfReader reader in pdfReaderList)
    {
        for (int i = 1; i <= reader.NumberOfPages; i++)
        {
            PdfImportedPage page = writer.GetImportedPage(reader, i);
            document.Add(iTextSharp.text.Image.GetInstance(page));
        }
    }
    document.Close();
    Byte[] FileBuffer = File.ReadAllBytes(Server.MapPath("~/Folder/") + "OutPut.pdf");
    if (FileBuffer != null)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-length", FileBuffer.Length.ToString());
        Response.BinaryWrite(FileBuffer);
    }
}

VB.Net

Protected Sub Merging(ByVal sender As Object, ByVal e As EventArgs)
    Dim pdfReaderList As List(Of PdfReader) = New List(Of PdfReader)()
    For Each filePath As String In Directory.GetFiles(Server.MapPath("~/Files"))
        Dim pdfReader As PdfReader = New PdfReader(filePath)
        pdfReaderList.Add(pdfReader)
    Next
    Response.ClearContent()
    Response.ClearHeaders()
    Response.Buffer = True
    Dim document As Document = New Document(PageSize.A4, 0, 0, 0, 0)
    Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream(Server.MapPath("~/Folder/") & "OutPut.pdf", FileMode.Create))
    document.Open()
    For Each reader As PdfReader In pdfReaderList
        For i As Integer = 1 To reader.NumberOfPages
            Dim page As PdfImportedPage = writer.GetImportedPage(reader, i)
            document.Add(iTextSharp.text.Image.GetInstance(page))
        Next
    Next
    document.Close()
    Dim FileBuffer As Byte() = File.ReadAllBytes(Server.MapPath("~/Folder/") & "OutPut.pdf")
    If FileBuffer IsNot Nothing Then
        Response.ContentType = "application/pdf"
        Response.AddHeader("content-length", FileBuffer.Length.ToString())
        Response.BinaryWrite(FileBuffer)
    End If
End Sub

Screenshot