Merging PDFs from iText7 - unable to download.

Dinesh Bali
Dinesh Bali used Ask the Experts™

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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Architect - Coder - Mentor
You can have a look at the community edition of Spire.PDF for .NET:


Many thanks for this.
If I wanted to use iText7.
Anyway, If I  can download the PDF from the above code.

I already have the PDFDocument generated as per my requirenment.

Please advise.
Eduard GherguArchitect - Coder - Mentor

Can you be more explicit?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial