We have an application in which at certain points a report is made as pdf. Although everything seemed to work perfect at the start, we found later that the fonts we used were not embedded in the pdf's we created. As such we could view the pdf's ourselves, but when someone else used our application they got a blank document.
We tried using PdfSharp to embed the fonts using the following code:
public byte[] EmbedPdf (byte[] originalPdf)
{
//open existing pdf through stream
MemoryStream preStream = new MemoryStream();
preStream.Write(originalPdf, 0, originalPdf.Length);
PdfDocument pdf = PdfReader.Open(preStream, PdfDocumentOpenMode.Import);
preStream.Close();
//open second document to paste the original data into which should be including embedding
PdfDocument nieuwePdf = new PdfDocument();
var options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
//loop through pages to insert them
for (int page = 0; page < pdf.Pages.Count; page++)
{
PdfPage pdfPage = nieuwePdf.AddPage(pdf.Pages[page]);
//the following line is the point where the application throws the error
XGraphics xgr = XGraphics.FromPdfPage(pdfPage);
XFont font = new XFont("Times New Roman", 12, XFontStyle.Regular, options);
}
//put new pdf back to the stream so it can be returned in the same format the original came in
MemoryStream postStream = new MemoryStream();
nieuwePdf.Save(postStream);
originalPdf = postStream.ToArray();
//end the stream
postStream.Close();
return originalPdf;
}
The error we get is: "Deflated stream ends early" at the line where the new pdf is supposed to be getting pages from the original. We thought the error meant that something in the stream went wrong when reading it, but we were unable to discover how to resolve the error. Can you help us fix this problem?
As reference, the following link shows another who had a similar problem and functioned as an example for the code above: PDFsharp edit a pdf file
EDIT: Here is a test pdf I made using the application. As I've put multiple fonts in the template for this, I noticed that it turns all fonts into Calibri on windows (header was Arial Black, footer was Verdana and main text was Times New Roman)
Download from here: http://www.wikiupload.com/L5GMAN88J9JXAYR
Also thanks for your suggestions, I tried both, however neither seemed to help. For closing the stream I could even comment it out and get the same error.