Multiple PDFs in BinaryWrite

D

DanG

I have several PDF documents available in my app, and I'm trying to
write them all out in sequence. The first is a generated report
rendered to the MemoryStream, and the others are supporting documents
stored in the database as images.

Response.ContentType = "application/pdf"
Response.Buffer = True
Dim b() As Byte = memStream.ToArray
Response.BinaryWrite(b)
Response.BinaryWrite(Image1)
Response.BinaryWrite(Image2)
Response.End()

When I do this, I only see the contents of the last image.

I also tried adding the images to memory stream.

memStream.Write(Image1, 0, ~Image1.Length~)
memStream.Write(Image2, 0, ~Image2.Length~)
Dim b() As Byte = memStream.ToArray
Response.BinaryWrite(b)
Response.End()

The length of memStream incremented correctly, but I get the same
results where I only see the last image.

Any clues on how to concatenate the PDFs?


THEN...
if that can be done, is there a way to add FormFeeds between the
images? I tried writing out an Chr(12), though I didn't really expect
it to work.

Thanks
Dan
 
G

George Ter-Saakov

You can only output one file per request.
You can merge 2 PDF documents into one with free library ITextSharp.
You can actually find a sample on how to do it. google it.

George.
 
D

DanG

Thanks George

Looks like it's going to take some work. The examples are for merging
PDF files that already exist. In my situation, the documents only
exist in memory, and will be sent directly to the screen. It will
never actually be saved, unless the user saves the merged document
from the Adobe screen.

I'll keep searching the iText exampled to see if they have one that
will fit my needs.

Dan
 
G

George Ter-Saakov

iTextSharp is pretty good library and usually for any method that takes file
there is same method that takes Stream.
So it should not take long to modify the sample from file to Stream.

George.
 
B

Ben

Looks like it's going to take some work. The examples are for merging
PDF files that already exist. In my situation, the documents only exist
in memory, and will be sent directly to the screen. It will never
actually be saved, unless the user saves the merged document from the
Adobe screen.

Dan,

I've used PdfSharp (open source, pdfsharp.com) to do that in the past,
it's very simple, something like that (snippet is loading from byte[],
saving to memory stream)

PdfSharp.Pdf.PdfDocument mergedPdf = new PdfSharp.Pdf.PdfDocument();
Stream pdfStream = new MemoryStream(currentPdfData, 0, (int)pdfDataSize,
false, true);
PdfSharp.Pdf.PdfDocument currentPdf = PdfSharp.Pdf.IO.PdfReader.Open
(pdfStream, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import);
PdfSharp.Pdf.PdfPage currentPage = currentPdf.Pages[ii];
mergedPdf.AddPage(currentPage);
MemoryStream mergedPdfStream = new MemoryStream();
mergedPdf.Save(mergedPdfStream);


Don't know ITextSharp, I can't say anything about dis-/advantages using
one or the other.

Ben
 
D

DanG

I found a great iTextSharp example, and I only had to change a bit to
fit my needs. Unfortunately, I'm getting a "damaged file" message

The example is at http://geekswithblogs.net/bsherwin/archive/2007/06/29/113566.aspx
..

Seems like the MemoryStream is created, and used to generate the
PdfWriter. At that point, my memStream has 15 bytes in it. Possibly
header info?

Anyway, no matter what PDF images I process in the loop, the size of
memStream never changes. I know the PDF images are good, because I
can convert them to bytes and print it with Response.BinaryWrite.

Here is the meat of my code:
*************************************************
Dim arrDocuments As New ArrayList
arrDocuments = Session("myDocs")
If arrDocuments.Count > 0 Then
Dim docController As WebDoc.DocumentController
Dim docInfo As New WebDoc.DocumentInfo
docController = New WebDoc.DocumentController
docController.Connection = myConnection

Dim i As Integer
For i = 0 To arrDocuments.Count - 1
docInfo = CType(arrDocuments(i), WebDoc.DocInfo)

Dim bytes() As Byte
bytes = docInfo.Document
reader = New iTextSharp.text.pdf.PdfReader(bytes)
numberOfPages = reader.NumberOfPages
curPageNo = 0

Do While (curPageNo < numberOfPages)
curPageNo += 1
doc.SetPageSize(PageSize.LETTER)
doc.NewPage()
page = writer.GetImportedPage(reader, curPageNo)
rotation = reader.GetPageRotation(curPageNo)
cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0)
Loop
Next
End If
*************************************************


I feel like I'm really close to getting this to work. Any clues? In
the meantime, I'll see what I can do with Ben's SharpPDF suggestion.

Thanks
Dan
 
D

DanG

Figured it out. The example I found didn't have a doc.Close() in it.
Apparently required before the MemoryStream will show the added pages.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top