Merge pdf files in c#

Merge pdf files in c#

In this tutorial we will learn How To Merge Multiple PDF Files using iTextsharp In Asp.Net C#.  or you can say merge pdf files in c#.

To Merge pdf files in, first you have to download iTextSharp.dll class library and include to our project.

You could download iTextSharp.dll class library here:

Then open your project and add iTextSharp DLL to your project, by right click on your project name > Add Reference > Browse > select iTextSharp Dll >OK.

We have already discussed How to Export GridView to PDF in Asp.Net C# Using iTextSharp. In this article you can pass direct input file stream, output stream and it wiil write merged pdf into output stream. This will helps you when you are using web application and want to convert pdf direct uploaded by the client and get it downloaded into client machine without saving it into server (without consuming the space of server).

Earlier we had discussed  ,  , .


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<style type="text/css">
body {
width: 980px;
margin: 0px auto;
text-align: center;
padding-top: 50px;
font-size: 20px;
<title>How To Merge Multiple PDF Files using iTextsharp In Asp.Net C#</title>
<form id="form1" runat="server">
<h1>How To Merge Multiple PDF Files using iTextsharp In Asp.Net C#</h1>   
File 1:    <asp:FileUpload ID="file1" runat="server" />File 2:<asp:FileUpload ID="file2" runat="server" />
<br /><br />
<asp:Button ID="merge" runat="server" Text="Merge PDF" OnClick="merge_Click" />
<br /><br />
<br /><br />
All rights reserved by <a href=""></a> |
Hosting partner <a href="" target="_blank">Grootstech</a>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text;
using System.IO;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    protected void merge_Click(object sender, EventArgs e)
        if (file1.HasFile && file2.HasFile)
            PdfReader pdfReader1 = new PdfReader(file1.PostedFile.InputStream);
            PdfReader pdfReader2 = new PdfReader(file2.PostedFile.InputStream);

            List<PdfReader> readerList = new List<PdfReader>();

            //Define a new output document and its size, type
            Document document = new Document(PageSize.A4, 0, 0, 0, 0);
            //Get instance response output stream to write output file.
            PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);

            foreach (PdfReader reader in readerList)
                for (int i = 1; i <= reader.NumberOfPages; i++)
                    PdfImportedPage page = writer.GetImportedPage(reader, i);

            Response.AppendHeader("content-disposition", "inline; filename=OutPut.pdf");
            Response.ContentType = "application/pdf";



merge pdf files in c#