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();
}
}
}
}
}
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”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.
Our community of experts have been thoroughly vetted for their expertise and industry experience.