We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Save several pdf to disk and finally concatenate all pdf and stream it to the browser

Hello

I have a little program that produces invoices from a database. It creates the invoices from a wordtemplate and then it adds and replaces som information on it. It creates all the invoices in a loop and then stores each document in an array.

foreach( Invoice invoice in invoices){
Document doc = new Document(System.Web.HttpContext.Current.Server.MapPath("templates/invoicetemplate.doc"));

Aspose.Words.DocumentBuilder builder = new DocumentBuilder(doc);
//Does some replacement and stuff

SendToDiskAsPdf(doc, System.Web.HttpContext.Current.Server.MapPath("../invoices/" + f.Fakturanr + ".pdf"));

//Add the stream to a collection so that we can send a concatenate document to the browser
streamColl.Add(GetPDFStream(doc));
}

MemoryStream outStream = new MemoryStream();
PdfFileEditor pdfEditor = new PdfFileEditor();

pdfEditor.Concatenate(streamColl.ToArray(), outStream);

//now what to do how do I stream it to the browser???

private static MemoryStream GetPDFStream(Document doc)
{
MemoryStream stream = new MemoryStream();
doc.Save(stream, SaveFormat.AsposePdf);
stream.Seek(0, SeekOrigin.Begin);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(stream);
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
pdf.IsImagesInXmlDeleteNeeded = true;
pdf.BindXML(xmlDoc, null);

pdf.IsTruetypeFontMapCached = false;

MemoryStream stream2 = new MemoryStream();
pdf.Save(stream2);
return stream2;
}

private static void SendToDiskAsPdf(Document doc, string filename){

GenericCollection<MemoryStream> streamColl = new GenericCollection<MemoryStream>();

MemoryStream stream = new MemoryStream();

doc.Save(stream, SaveFormat.AsposePdf);

stream.Seek(0, SeekOrigin.Begin);

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(stream);

Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();

pdf.IsImagesInXmlDeleteNeeded = true;

pdf.BindXML(xmlDoc, null);

pdf.IsTruetypeFontMapCached = false;

MemoryStream stream2 = new MemoryStream();

pdf.Save(stream2);

streamColl.Add(stream2);

FileStream outStream = new FileStream(filename, FileMode.Create);

PdfFileEditor pdfEditor = new PdfFileEditor();

pdfEditor.Concatenate(streamColl.ToArray(), outStream);

outStream.Close();

outStream.Dispose();

stream2.Close();

stream2.Dispose();

stream.Close();

stream.Dispose();

}

I think this could be done with much more simpler code and a litle fewer objects. And how do a send the concatenate document to the browser?
pdfEditor.Concatenate(streamColl.ToArray(), outStream);

Hi,

Please try the following code for sending pdf file to browser:

Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Length",
outStream.GetBuffer().Length.ToString());
Response.OutputStream.Write(outStream.GetBuffer(), 0, outStream.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();

Thanks.