PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

pdf file is getting corrupted when converting it to bytes
http://forum.pdfsharp.com/viewtopic.php?f=2&t=957
Page 1 of 1

Author:  rashid007 [ Thu Nov 26, 2009 5:36 am ]
Post subject:  pdf file is getting corrupted when converting it to bytes

Hi Thomas ,

A very good morning to you :D

I am facing a problem with opening the pdf file.

My code is :

Document document = invoiceForm.CreateDocument();

// Create a renderer for PDF that uses Unicode font encoding
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);

// Set the MigraDoc document
pdfRenderer.Document = document;

// Create the PDF document
pdfRenderer.RenderDocument();

// Save the PDF document...
string filename = "Invoice.pdf";

MemoryStream stream = new MemoryStream();
stream.Seek(0, SeekOrigin.Begin);

pdfRenderer.PdfDocument.Save(stream, false);


byte[] data = new byte[stream.Length];
stream.Read(data, 0, data.Length);
stream.Close();

InsertBlob("testblob", data, "application/pdf", "invoicedoc");

byte[] result = GetBlob("testblob", "invoicedoc");


Now when i convert the byte array to pdf file and try to open it then i am getting this error :
"The document does not start with %PDF-"

Please help me out...I tried to open the pdf both using the blob URL as well as by a file stream but it didn't work.

Thanks !

Regards
Rashid Ali

Author:  Thomas Hoevel [ Thu Nov 26, 2009 9:18 am ]
Post subject:  Re: pdf file is getting corrupted when converting it to bytes

Maybe you should try the
Code:
stream.Seek(0, SeekOrigin.Begin);

after the Save() {no longer needed with PDFsharp 1.31 coming out very soon now}.

Or rely on the trusted old code from the WebClock sample:
Code:
MemoryStream stream = new MemoryStream();
document.Save(stream, false);
byte[] data = stream.ToArray();

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/