PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index PDFsharp - moved to http://forum.pdfsharp.net/
Please visit the new PDFsharp forum at http://forum.pdfsharp.net/
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Create PDF Document from byte stream?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index -> Support - moved to http://forum.pdfsharp.net/
View previous topic :: View next topic  
Author Message
nwebster



Joined: 04 May 2009
Posts: 13

PostPosted: Tue May 12, 2009 10:04 pm    Post subject: Create PDF Document from byte stream? Reply with quote

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
View user's profile Send private message
Thomas Hoevel



Joined: 16 Oct 2006
Posts: 387
Location: Cologne, Germany

PostPosted: Wed May 13, 2009 7:54 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
nwebster



Joined: 04 May 2009
Posts: 13

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

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
View user's profile Send private message
nwebster



Joined: 04 May 2009
Posts: 13

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

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
View user's profile Send private message
nwebster



Joined: 04 May 2009
Posts: 13

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

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
View user's profile Send private message
Thomas Hoevel



Joined: 16 Oct 2006
Posts: 387
Location: Cologne, Germany

PostPosted: Mon May 18, 2009 8:22 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
nwebster



Joined: 04 May 2009
Posts: 13

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

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
View user's profile Send private message
Thomas Hoevel



Joined: 16 Oct 2006
Posts: 387
Location: Cologne, Germany

PostPosted: Tue May 19, 2009 7:35 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
nwebster



Joined: 04 May 2009
Posts: 13

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

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index -> Support - moved to http://forum.pdfsharp.net/ All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © phpBB Group. Hosted by phpBB.BizHat.com


Start Your Own YouTube Clone

Free Web Hosting | Free Forum Hosting | FlashWebHost.com | Image Hosting | Photo Gallery | FreeMarriage.com

Powered by PhpBBweb.com, setup your forum now!
For Support, visit Forums.BizHat.com