We help IT Professionals succeed at work.
Get Started

Merging PDFs from iText7 - unable to download.

Dinesh Bali
Dinesh Bali asked
Last Modified: 2019-12-23

I tried creating sample web project to merge 2 PDFs and download.
I am able to download PDF but the PDF is corrupt. Might be there is no content
I have used iText7 from https://itextpdf.com
Is it good third party solution to merge PDFs and show?

Not sure I am reading/merging PdfDocument pdfInnerDoc correctly to download.

Please can you see that what can be the possible reason.

using iText.IO.Source;
using iText.Kernel.Pdf;
using iText.Kernel.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using iText.Forms;

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

            FileInfo f1 = new FileInfo(Server.MapPath("~/") + "documents/794670405936.pdf");
            FileInfo f2 = new FileInfo(Server.MapPath("~/") + "documents/794670405947.pdf");

            Stream myStream1 = File.OpenRead(Server.MapPath("~/") + "documents/794670405947.pdf");

            PdfWriter writer = new PdfWriter(f1);
            PdfDocument pdfDoc = new PdfDocument(writer);
            ByteArrayOutputStream baos;
            PdfReader reader;
            PdfDocument pdfInnerDoc;

            BufferedStream br = new BufferedStream(myStream1);
            // String line = br.readLine();
            // loop over readers
            // create a PDF in memory
            baos = new ByteArrayOutputStream();
            reader = new PdfReader(f2);
            pdfInnerDoc = new PdfDocument(reader, new PdfWriter(baos));
            // form = PdfAcroForm.getAcroForm(pdfInnerDoc, true);
            //fill and flatten form...
            //add the PDF using copyPagesTo
            pdfInnerDoc = new PdfDocument(new PdfReader(myStream1));
            pdfInnerDoc.CopyPagesTo(1, pdfInnerDoc.GetNumberOfPages(), pdfDoc, new PdfPageFormCopier());

            byte[] arrb = baos.ToArray();
            DownloadPDF(arrb, "MergedPdf");
            // end loop


        private void DownloadPDF(byte[] labelBuffer, string awbNumber)

            Stream LabelFile = new MemoryStream(labelBuffer);
                // Total bytes to read:
                long bytesToRead = labelBuffer.Length;
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + awbNumber + ".pdf");
                // Read the bytes from the stream in small portions.
                while (bytesToRead > 0)
                    // Make sure the client is still connected.
                    if (HttpContext.Current.Response.IsClientConnected)
                        // Read the data into the buffer and write into the
                        // output stream.
                        //  byte[] buffer = new Byte[10000];
                        int length = LabelFile.Read(labelBuffer, 0, 10000);
                        HttpContext.Current.Response.OutputStream.Write(labelBuffer, 0, length);
                        // We have already read some bytes.. need to read
                        // only the remaining.
                        bytesToRead = bytesToRead - length;
                        // Get out of the loop, if user is not connected anymore..
                        bytesToRead = -1;
            catch (Exception ex)
                //Logger.Error("Error occured while download pdf in Function DownloadAWB(): ", ex.Message);
                if (LabelFile != null)


Open in new window

Watch Question
Architect - Coder - Mentor
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE