PDF to MemoryStream and then back

I have produced a pdf utilizing PDFSharp.

I call the conserve approach, and save it to disk, and the file is ideal.

I then have to get the pdf into a MemorySteam, in preparion of sending it to my site to download. The file ends up invalid. corrupt.

So, to see where it’s failing, I have put the file into a MemoryStream, and after that I attempt compose that steam to a file, to confirm all is OK. It isn’t really.

Here I sav the file to disk, to examine it (debugging), and after that put it into a stream.

Try the following code, which properly 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

The code specified under is producing true pdf report.

Just how do one develop PDF in memorystream somewhat of bodily data making use of itextsharp.

That I can return it with a functionality

I possess really certainly never ever before made use of iTextPDF prior to yet it appeared interesting so I took upon the obstacle and also carried out some study on my personal. Below’s ways to stream the PDF report with memory.

Where your code possesses new FileStream, come on a MemoryStream you have actually presently produced. (Perform not simply produce it inline in decision to PdfWriter.GetInstance – you’ll desire to have the capability to describe it in the future.).

PdfWriter carries out certainly not implement IDisposable so you can certainly not utilize it in an using declaration. Possibly this is actually only in the variety I am actually utilizing though (5.0.5) as I recognize there have actually been some lesson adjustments coming from variant 4

Call ToArray() on the MemoryStream when you’ve completed writing to it to get a byte []:
using (MemoryStream output = new MemoryStream())
PdfWriter wri = PdfWriter.GetInstance(doc, output);

// Write to document
// …
return output.ToArray();

I haven’t used iTextSharp, however I think some of these types implement IDisposable – where case you need to be producing them in utilizing statements too.

I am composing an application in MVC4.

I have a physical pdf file on the server. I wish to transform this to a memory stream and send it back to the user like this: return File(stream, "application/pdf", "myPDF.pdf");

How do I convert a pdf file to a memory stream?

You do not need MemoryStream. Easiest method is to utilize overload that accepts file name: return File(@"C:\MyFile.pdf", "application/pdf");

another option is to utilize overload that accepts byte []: return File(System.IO.File.ReadAllBytes(@"C:\Myfile.pdf"), "application/pdf");

Leave a Reply

Your email address will not be published. Required fields are marked *