View previous topic :: View next topic |
Author |
Message |
nwebster
Joined: 04 May 2009 Posts: 13
|
Posted: Tue May 12, 2009 10:04 pm Post subject: Create PDF Document from byte stream? |
|
|
Is it possible to create a pdf document from a byte[] stream? I am taking in multiple byte[] streams, each a pdf file originally, and want to take them in as PDFdocuments, and then merge them into a single pdf document. Is this possible? |
|
Back to top |
|
|
Thomas Hoevel
Joined: 16 Oct 2006 Posts: 387 Location: Cologne, Germany
|
Posted: Wed May 13, 2009 7:54 am Post subject: |
|
|
PdfReader.Open can take a Stream as its first parameter - it should also work with a MemoryStream based on a byte[]. _________________ Regards
Thomas Hoevel
PDFsharp Team |
|
Back to top |
|
|
nwebster
Joined: 04 May 2009 Posts: 13
|
Posted: Thu May 14, 2009 5:50 pm Post subject: |
|
|
Thanks. I was able to generate the following code:
// results is my byte arrary
Stream s = new MemoryStream(results);
PdfDocument document = new PdfDocument();
document = PdfReader.Open(s);
Running this code threw this error:
Token '/ExtGState' was not expected.
Running a different pdf, I got this error:
Token '' was not expected.
What does the error mean? |
|
Back to top |
|
|
nwebster
Joined: 04 May 2009 Posts: 13
|
Posted: Thu May 14, 2009 7:25 pm Post subject: |
|
|
I dug through and found that the error that returned this:
Token '/ExtGState' was not expected.
was due to a weird situation. Instead of being followed by a << on the next line, there was a
/CA 1
line. When I removed that, I was able to open the file up still (i.e. I wrote the bytes out to a file and was able to open it before and after in Acrobat Reader I edited the pdf document). When I removed that line though, I got another error of '' not being a valid character. I search for a case of it and it doesn't find any with the text editor. Is there a way I can basically ignore this error?
Also, I tried just saving the byte array to a file, and then tried loading the file. pdfsharp would not load the file properly. I could open the file in Acrobat reader, but could not load the file using pdfsharp. It seems that when the pdf goes through the byte array, something happens to it that pdfsharp does not like... |
|
Back to top |
|
|
nwebster
Joined: 04 May 2009 Posts: 13
|
Posted: Fri May 15, 2009 4:59 pm Post subject: |
|
|
It seems the problem is that the data is coming through our SQL DB using an OPENROWSET retrieval call to return the PDF as a varbinary(max). It must be switching the bits around somehow to throw something off. Is there some change I could make so pdfsharp could handle this file? Adobe and Foxit can handle it, but pdfsharp keeps throwing the error. |
|
Back to top |
|
|
Thomas Hoevel
Joined: 16 Oct 2006 Posts: 387 Location: Cologne, Germany
|
Posted: Mon May 18, 2009 8:22 am Post subject: |
|
|
Maybe you have to specify the correct encoding when converting the byte array to a memory stream. _________________ Regards
Thomas Hoevel
PDFsharp Team |
|
Back to top |
|
|
nwebster
Joined: 04 May 2009 Posts: 13
|
Posted: Mon May 18, 2009 7:19 pm Post subject: |
|
|
How would I specify a content type? I know how to specify one for what the client receives, but not for an internal memory stream or byte array. |
|
Back to top |
|
|
Thomas Hoevel
Joined: 16 Oct 2006 Posts: 387 Location: Cologne, Germany
|
Posted: Tue May 19, 2009 7:35 am Post subject: |
|
|
Encoding, not content type.
But I see it's a feature of the reader, not a feature of the stream:
Code: | public StreamReader(
Stream stream,
Encoding encoding
) |
If you take the PDF file, write it to database, read it from database, then save the stream to a file, and finally call "FC /b" to compare it with the original file: will you find differences?
If yes: something's changed somewhere.
If no: mysterious ... (no idea yet) _________________ Regards
Thomas Hoevel
PDFsharp Team |
|
Back to top |
|
|
nwebster
Joined: 04 May 2009 Posts: 13
|
Posted: Wed May 27, 2009 6:52 pm Post subject: |
|
|
Thanks for the help. It turns out I was able to get it working. Apparently when the data is loaded using the openrowset, it is inserting 27 characters in the front that is throwing the file off.
So now I do the following:
1. Read in byte array from DB
2. Use Binary writer to write to MemoryStream1
3. Read from MemoryStream1 (it's closed when it is written to) into MemoryStream2
4. Create PDFDocument using MemoryStream2 in Modify mode.
At this point, I have the data loaded and working, and can do with it what I need to. Wanted to post this in case anyone else ran into these issues. |
|
Back to top |
|
|
|