Reputation: 10221
I am in trouble with a .ashx
which should merge some PDF memory-stream, in a simplest case, where I have in output only one stream, the output PDF stream are corrupted, but when I call another ashx which get in output only the single stream works, what I miss in the following code?
I collect memory-streams:
Dim streamDocument As MemoryStream = FumForm.CreatePdfDocument(context, _fumForm, _formTemplate, _match)
lReader.Add(New PdfReader(streamDocument))
then I would like append all pdf pages in an other pdf:
Dim document As Document = New Document(PageSize.A4, 0, 0, 0, 0)
Dim writer As PdfWriter = PdfWriter.GetInstance(document, context.Response.OutputStream)
document.Open()
For Each r As PdfReader In lReader
For i As Integer = 1 To r.NumberOfPages
Dim page As PdfImportedPage = writer.GetImportedPage(r, i)
document.Add(Image.GetInstance(page))
Next
Next
Dim filename = String.Format("{0}{1}.pdf", "pippo", "test")
document.Close()
context.Response.Clear()
context.Response.ContentType = "application/pdf"
context.Response.AppendHeader("content-disposition", "inline; filename=""" & filename & """")
context.Response.Flush()
context.Response.Close()
context.Response.End()
CreatePdfDocument
works well, and have this signature
static public MemoryStream CreatePdfDocument(HttpContext context,
FumForm form,
FumFormTemplate formTemplate,
Match match)
Any help will be really appreciated
Upvotes: 1
Views: 619
Reputation: 96064
The code first writes the whole PDF to context.Response.OutputStream
Dim document As Document = New Document(PageSize.A4, 0, 0, 0, 0)
Dim writer As PdfWriter = PdfWriter.GetInstance(document, context.Response.OutputStream)
...
document.Close()
and thereafter clears and changes headers of the context.Response
context.Response.Clear()
context.Response.ContentType = "application/pdf"
context.Response.AppendHeader("content-disposition", "inline; filename=""" & filename & """")
To work correctly, all response object clearing and header manipulations have to be finished before the data may be written to the response stream.
Upvotes: 2