ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Combine (Merge) multiple PDF files into one (single) using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="KatieNgoc">KatieNgoc</a>,</p> <p>Refer below sample code.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:Button ID=&#34;Button1&#34; Text=&#34;Merge&#34; runat=&#34;server&#34; OnClick=&#34;Merging&#34; /&gt;</pre> <p>&nbsp;<span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using iTextSharp.text.pdf; using System.IO; using iTextSharp.text;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports iTextSharp.text Imports iTextSharp.text.pdf Imports System.IO</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Merging(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; List&lt;PdfReader&gt; pdfReaderList = new List&lt;PdfReader&gt;(); &nbsp;&nbsp;&nbsp; foreach (string filePath in Directory.GetFiles(Server.MapPath(&#34;~/Files&#34;))) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PdfReader pdfReader = new PdfReader(filePath); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfReaderList.Add(pdfReader); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; Response.ClearContent(); &nbsp;&nbsp;&nbsp; Response.ClearHeaders(); &nbsp;&nbsp;&nbsp; Response.Buffer = true; &nbsp;&nbsp;&nbsp; Document document = new Document(PageSize.A4, 0, 0, 0, 0); &nbsp;&nbsp;&nbsp; PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath(&#34;~/Folder/&#34;) + &#34;OutPut.pdf&#34;, FileMode.Create)); &nbsp;&nbsp;&nbsp; document.Open(); &nbsp;&nbsp;&nbsp; foreach (PdfReader reader in pdfReaderList) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 1; i &lt;= reader.NumberOfPages; i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PdfImportedPage page = writer.GetImportedPage(reader, i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; document.Add(iTextSharp.text.Image.GetInstance(page)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; document.Close(); &nbsp;&nbsp;&nbsp; Byte[] FileBuffer = File.ReadAllBytes(Server.MapPath(&#34;~/Folder/&#34;) + &#34;OutPut.pdf&#34;); &nbsp;&nbsp;&nbsp; if (FileBuffer != null) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.ContentType = &#34;application/pdf&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.AddHeader(&#34;content-length&#34;, FileBuffer.Length.ToString()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.BinaryWrite(FileBuffer); &nbsp;&nbsp;&nbsp; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Merging(ByVal sender As Object, ByVal e As EventArgs) &nbsp;&nbsp;&nbsp; Dim pdfReaderList As List(Of PdfReader) = New List(Of PdfReader)() &nbsp;&nbsp;&nbsp; For Each filePath As String In Directory.GetFiles(Server.MapPath(&#34;~/Files&#34;)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim pdfReader As PdfReader = New PdfReader(filePath) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfReaderList.Add(pdfReader) &nbsp;&nbsp;&nbsp; Next &nbsp;&nbsp;&nbsp; Response.ClearContent() &nbsp;&nbsp;&nbsp; Response.ClearHeaders() &nbsp;&nbsp;&nbsp; Response.Buffer = True &nbsp;&nbsp;&nbsp; Dim document As Document = New Document(PageSize.A4, 0, 0, 0, 0) &nbsp;&nbsp;&nbsp; Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream(Server.MapPath(&#34;~/Folder/&#34;) &amp; &#34;OutPut.pdf&#34;, FileMode.Create)) &nbsp;&nbsp;&nbsp; document.Open() &nbsp;&nbsp;&nbsp; For Each reader As PdfReader In pdfReaderList &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For i As Integer = 1 To reader.NumberOfPages &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim page As PdfImportedPage = writer.GetImportedPage(reader, i) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; document.Add(iTextSharp.text.Image.GetInstance(page)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next &nbsp;&nbsp;&nbsp; Next &nbsp;&nbsp;&nbsp; document.Close() &nbsp;&nbsp;&nbsp; Dim FileBuffer As Byte() = File.ReadAllBytes(Server.MapPath(&#34;~/Folder/&#34;) &amp; &#34;OutPut.pdf&#34;) &nbsp;&nbsp;&nbsp; If FileBuffer IsNot Nothing Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.ContentType = &#34;application/pdf&#34; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.AddHeader(&#34;content-length&#34;, FileBuffer.Length.ToString()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.BinaryWrite(FileBuffer) &nbsp;&nbsp;&nbsp; End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong> </span></p> <p><img src="https://i.imgur.com/iUaGe1j.gif" alt="" width="334" height="398" /></p> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/451231/Combine-Merge-multiple-PDF-files-into-one-single-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/451231/Combine-Merge-multiple-PDF-files-into-one-single-using-C-and-VBNet-in-ASPNet/Tue, 27 Aug 2019 02:04:59 GMT