-1

I have generated a pdf using PDFSharp.

I call the save method, and save it to disk, and the file is perfect.

I then need to get the file into a MemorySteam, in preparion of sending it to my website to download. However, the file ends up invalid. corrupt.

So, to see where it's going wrong, I have put the file into a MemoryStream, and then I try write that steam to a file, to confirm all is OK. It isn't.

Here I sav the file to disk, to check it (debugging), and then put it into a stream:

            document.Save("c:\\temp\\ggg.pdf");

            MemoryStream ms = new MemoryStream();
                document.Save(ms, false);
                byte[] buffer = new byte[ms.Length];
                ms.Seek(0, SeekOrigin.Begin);
                ms.Flush();
                ms.Read(buffer, 0, (int)ms.Length);
                return ms;

I then return 'ms' to my calling function, and attempt to write the stream to a file:

 var doc = GeneratePdf(1);

            using (FileStream file = new FileStream("c:\\temp\\222.pdf", FileMode.Create, System.IO.FileAccess.Write))
            {
                byte[] bytes = new byte[doc.Length];
                doc.Read(bytes, 0, (int)doc.Length);
                file.Write(bytes, 0, bytes.Length);
                doc.Close();
            }

But 222.pdf is not a valid pdf. ggg.pdf was fine. So I am doing something wrong when I write to stream, and write to disk. Why is the file getting corrupted?

Craig
  • 18,074
  • 38
  • 147
  • 248
  • Where are you filling the `buffer`? – Ian H. Jul 15 '17 at 12:46
  • @IanH.- I'm not sure. I thought it was "ms.Read(buffer, 0, (int)ms.Length);", but clearly not? You may have spotted the issue. – Craig Jul 15 '17 at 12:54
  • 1
    Why don't you just use `Stream.CopyTo` instead of manually coping those streams? Also what's the purpose of `Flush()` and `Read(..)` *after* you have already saved the document into the `MemoryStream`? – Federico Dipuma Jul 15 '17 at 12:54
  • Ah, forget it, I forgot that the `Read` method does actually fill the buffer, my bad. – Ian H. Jul 15 '17 at 12:57
  • @FedericoDipuma - this is using an example I found: https://stackoverflow.com/questions/8624071/save-and-load-memorystream-to-from-a-file ... it had 159 'likes' so thought it was right. – Craig Jul 15 '17 at 12:58
  • It looks like pdfSharp can save to stream.... I ams trying this now, but get the same results. MemoryStream stream = new MemoryStream(); document.Save(stream, false); return stream; – Craig Jul 15 '17 at 13:02
  • You have to call `doc.Seek` to set the pointer to the start of the stream before calling `doc.Read`. You do not even check the result of `doc.Read` to see how many bytes were read. Might well be 0 bytes, assuming the stream is at the end. Did you look at the contents of 222.PDF? Correct size, but only 0 bytes maybe? – I liked the old Stack Overflow Jul 15 '17 at 17:48

1 Answers1

1

I cannot reproduce your issue (PdfSharp 1.32.3057.0). It seems to me that you are messing too much with manual stream copying.

Try the following code, which correctly creates a pdf, streams it into a MemoryStream and than saves it into a file:

var pdf = new PdfSharp.Pdf.PdfDocument();

var page = pdf.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);

var ms = new MemoryStream();
pdf.Save(ms, false);
ms.Position = 0;

using (var file = File.OpenWrite("test.pdf"))
    ms.CopyTo(file); // no need for manual stream copy or buffers
Federico Dipuma
  • 17,655
  • 4
  • 39
  • 56