PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jun 16, 2024 1:29 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Tue May 12, 2009 10:04 pm 
Offline

Joined: Mon May 04, 2009 12:45 pm
Posts: 13
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?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed May 13, 2009 7:54 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu May 14, 2009 5:50 pm 
Offline

Joined: Mon May 04, 2009 12:45 pm
Posts: 13
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?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu May 14, 2009 7:25 pm 
Offline

Joined: Mon May 04, 2009 12:45 pm
Posts: 13
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...


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri May 15, 2009 4:59 pm 
Offline

Joined: Mon May 04, 2009 12:45 pm
Posts: 13
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon May 18, 2009 8:22 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
Maybe you have to specify the correct encoding when converting the byte array to a memory stream.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon May 18, 2009 7:19 pm 
Offline

Joined: Mon May 04, 2009 12:45 pm
Posts: 13
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue May 19, 2009 7:35 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed May 27, 2009 6:52 pm 
Offline

Joined: Mon May 04, 2009 12:45 pm
Posts: 13
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 183 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group