|
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); }
|
|
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!
|
|
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
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Thanks for the sharing. Yes. That makes perfect sense!
|
|