PDFsharp - 从 System.Byte[] 变量打开 PDF

Scy*_*312 3 .net pdf byte filestream pdfsharp

我正在使用 PDFsharp,并且尝试打开一个表示为 Byte[] 变量的 PDF。这是一个简化的示例:

public Byte[] myFunc(Byte[] PDF)
{
    PdfDocument document = PdfReader.Open(PDF_Path); // <-- My problem is here
    // do some modification on the document
    return document;
}
Run Code Online (Sandbox Code Playgroud)

我可以从 PDF 的路径中读取 PDF,但我宁愿将 PDF 作为字节数组来使用。将字节数组另存为 PDF 然后读取它,然后删除我创建的文件是否是更好的选择?我觉得这种方法不合适。

Je *_*not 7

您可以将字节数组写入或为该字节数组MemoryStream创建一个,然后使用 PDFsharp 从该流打开 PDF。MemoryStream

无需弄乱临时文件。

更新:OP找到的解决方案(同时从问题中删除):

public Byte[] myFunc(Byte[] bytePDF)
{
    MemoryStream stream = new MemoryStream(bytePDF);
    PdfDocument document = PdfReader.Open(stream, PdfDocumentOpenMode.Import); //You might not need  PdfDocumentOpenMode.Import
    // do some modification on the document
    document.Save(stream, false);
    return stream.ToArray();
}
Run Code Online (Sandbox Code Playgroud)