Welcome Guest Search | Active Topics | Sign In | Register

Cannot save pdf to memorystream, it's always corrupted Options
Mark
Posted: Monday, May 18, 2015 2:07:16 PM
Rank: Newbie
Groups: Member

Joined: 5/18/2015
Posts: 2
I cannot use the PdfDocument's Save() overload that writes to a memorystream instead of a disk file. The output is always an invalid PDF. In fact, it appears to be a file of space characters (ascii 32).

I'm using a very simple piece of code (just 4 lines) to read a KNOWN VALID template pdf file from disk then save it to a memorystream for additional processing. However, the stream is always invalid. My code is below. I've added a few lines of code to save the memorystream back to disk AS A TEMPORARY WAY to view the output.

The size of the output file is always identical to the size of the original (template) file in bytes... it's just that the output is apparently a file of spaces (char 32).

I'm using Visual Studio 2013; .Net Framework 4.5; MVC 5.


--- Begin sample code ---


// Read a valid PDF file from disk
TemplateRoot = HttpContext.Current.Server.MapPath("~/Resources/Template.pdf");

// Create an EO PDF document from the pdf file we just read
PdfDocument ESADoc = new PdfDocument(TemplateRoot);

// Create a new memory stream
MemoryStream PdfStream = new MemoryStream();

// Save the pdf document to the memory stream
ESADoc.Save(PdfStream);


// DEBUG CODE ADDED TO SAVE STREAM TO DISK. THIS ISN'T PART OF MY REAL APP. I JUST WANT TO LOOK AT WHAT'S IN THE STREAM
//
string debugfile = HttpContext.Current.Server.MapPath("~/Resources/temp.pdf");
using (FileStream file = new FileStream(debugfile, FileMode.Create, System.IO.FileAccess.Write))
{
byte[] bytes = new byte[PdfStream.Length];
PdfStream.Read(bytes, 0, (int)PdfStream.Length);
file.Write(bytes, 0, bytes.Length);
}
eo_support
Posted: Tuesday, May 19, 2015 10:24:09 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Hi,

Your code looks fine. The problem must be somewhere else. You can put a break point after Save(pdfStream) and then check the content of PdfStream in the debugger, you should see it contains the right contents. It is not possible that PdfDocument.Save will only give you space characters. It either succeeds or fails. When it succeeds, PdfStream should have valid content. When it fails, you should get an exception. Another way to test it is to use code like this to write the content of PdfStream to disk:

Code: C#
//Write the contents of the MemoryStream object to a file
File.WriteAllBytes(file_name, pdfStream.ToArray());


You should see the file has the correct contents as well.

Thanks!
Mark
Posted: Tuesday, May 19, 2015 3:17:03 PM
Rank: Newbie
Groups: Member

Joined: 5/18/2015
Posts: 2
Before working with the memorystream returned by Save(pdfstream), it is necessary to set the position of the stream back to the start (.Position = 0). If you don't do this, saving the stream to disk (or trying to work with it in a view by converting it to a filestream) will a string of empty spaces equal to the length of the PDF file. That's what I am able to reproduce here, repeatedly. Thanks for the reply. Updated code that's working is shown below...

// Read a valid PDF file from disk
TemplateRoot = HttpContext.Current.Server.MapPath("~/Resources/Template.pdf");

// Create an EO PDF document from the pdf file we just read
PdfDocument ESADoc = new PdfDocument(TemplateRoot);

// Create a new memory stream
MemoryStream PdfStream = new MemoryStream();

// Save the pdf document to the memory stream
ESADoc.Save(PdfStream);

PdfStream.Position = 0; // <----- Required or else the document will
eo_support
Posted: Tuesday, May 19, 2015 4:31:56 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Thanks for the sharing. Yes. That makes perfect sense!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.