PDFsharp save to MemoryStream

前端 未结 5 662
星月不相逢
星月不相逢 2021-01-31 15:12

I want to save a PdfSharp.Pdf.PdfDocument by its Save method to a Stream, but it doesn\'t attach the PDF header settings to it. So when I read back the Stream and return it to t

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-31 15:51

    So the solution:

    MigraDoc.DocumentObjectModel.Document doc = new MigraDoc.DocumentObjectModel.Document();
    MigraDoc.Rendering.DocumentRenderer renderer = new DocumentRenderer(doc);
    MigraDoc.Rendering.PdfDocumentRenderer pdfRenderer = new MigraDoc.Rendering.PdfDocumentRenderer();
    pdfRenderer.PdfDocument = pDoc;
    pdfRenderer.DocumentRenderer = renderer;
    using (MemoryStream ms = new MemoryStream())
    {
      pdfRenderer.Save(ms, false);
      byte[] buffer = new byte[ms.Length];
      ms.Seek(0, SeekOrigin.Begin);
      ms.Flush();
      ms.Read(buffer, 0, (int)ms.Length);
    }
    

    There is this MigraDoc stuff which comes with PdfSharp, but i hardly found any proper doc/faq for it. After hours of googling i've found a snippet which was something like this. Now it works.

提交回复
热议问题