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); writer.SetSmartMode(true); PdfDocument pdfDoc = new PdfDocument(writer); pdfDoc.InitializeOutlines(); 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(); pdfInnerDoc.Close(); DownloadPDF(arrb, "MergedPdf"); // end loop } private void DownloadPDF(byte[] labelBuffer, string awbNumber) { Stream LabelFile = new MemoryStream(labelBuffer); try { // 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); HttpContext.Current.Response.Flush(); // We have already read some bytes.. need to read // only the remaining. bytesToRead = bytesToRead - length; } else { // 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); } finally { if (LabelFile != null) { LabelFile.Close(); } } } }}
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.