How To Merge Multiple PDF Files using iTextsharp In Asp.Net

How To Merge Multiple PDF Files using iTextsharp In Asp.Net

In this tutorial we will learn How To Merge Multiple PDF Files using iTextsharp In Asp.Net C#. First you have to download iTextSharp.dll class library and include to our project.

You could download iTextSharp.dll class library here: http://sourceforge.net/projects/itextsharp/

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  ,  , .

Design:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<style type="text/css">
body {
width: 980px;
margin: 0px auto;
text-align: center;
padding-top: 50px;
font-size: 20px;
}
</style>
<title>How To Merge Multiple PDF Files using iTextsharp In Asp.Net C#</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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="http://www.hightechnology.in">www.Hightechnology.in</a> |
Hosting partner <a href="http://www.grootstech.com" target="_blank">Grootstech</a>
</div>
</form>
</body>
</html>

Code:

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>();
            readerList.Add(pdfReader1);
            readerList.Add(pdfReader2);


            //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);
            document.Open();

            foreach (PdfReader reader in readerList)
            {
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    PdfImportedPage page = writer.GetImportedPage(reader, i);
                    document.Add(iTextSharp.text.Image.GetInstance(page));
                }
            }
            document.Close();

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

        }

    }
    
}

How To Merge Multiple PDF Files using iTextsharp In Asp.Net C#
download