troubleshooting Question

Merging PDFs from iText7 - unable to download.

Avatar of Dinesh Bali
Dinesh Bali asked on
.NET ProgrammingC#
3 Comments1 Solution353 ViewsLast Modified:

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)

Eduard Ghergu
Architect - Coder - Mentor

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros