【问题标题】:Convert pdf to byte[] before it is saved在保存之前将 pdf 转换为 byte[]
【发布时间】:2014-07-28 15:25:43
【问题描述】:

我正在使用 PDFSharp,我正在从位图图像创建 pdf。我可以将图像保存和查看为 pdf 没问题。但现在我想做的是在将 pdf 保存为实际文件之前,我想将其转换为 byte[]。在我保存它之后,我可以将它保存为 byte[] 。我将有两种不同的方法,一种将 pdf 保存到用户可以打开的文件中,另一种将 pdf 保存为将发送到数据库的字节,但我可能不会两者都做。我可能需要一天保存一个文件,另一天将它保存为一个字节,而无需将其保存为文件。我有什么:

    public void DrawImage(Bitmap thumbnail)//thumbnail is created with another method
    {
        PdfDocument document = new PdfDocument();
           document.Info.Title = "Created with PDFsharp";
          PdfPage page = document.AddPage();
          XGraphics gfx = XGraphics.FromPdfPage(page);

        XImage image = XImage.FromGdiPlusImage(thumbnail);
        gfx.DrawImage(image, 0, 0, 612, 792);

        string filename = string.Format(@"{0}.pdf", Guid.NewGuid());
        //This is Save(string path), there is also a Save(memoryStream stream)
         document.Save(filename);
        //so before I save it as a *.PDF I would like to save it as byte[] that can be sent to a Database, and eventually read from and convert the byte to a viewable *.pdf
        //This saves a Bitmap as a pdf successfully

     }

我希望我是有道理的,如果需要我可以进一步解释

【问题讨论】:

  • 好的,如果我这样做,我将如何获取建议的这个字节流并将其重新转换为可查看的 pdf?我看到的每个示例都使用 WriteAllBytes() 接受文件路径..

标签: c# pdf


【解决方案1】:

您可以为此使用MemoryStream

byte[] pdfData;
using (var ms = new MemoryStream()) {
    document.Save(ms);
    pdfData = ms.ToArray();
}

【讨论】:

  • 谢谢你,你是第一个,所以我给了你功劳,我想出了如何将字节转回 pdf
【解决方案2】:

尝试使用 MemoryStream:

using(var ms = new MemoryStream()){
  document.Save(ms);
  byte[] bytes = ms.ToArray();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-16
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 2016-09-03
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多