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

.net PDFsharp-从System.Byte[]变量打开PDF,.net,pdf,byte,filestream,pdfsharp,.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; } 我可以从它的路径读取PDF,但我

我正在使用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;
}
我可以从它的路径读取PDF,但我更愿意使用PDF作为字节数组。
将字节数组保存为PDF,然后读取,然后删除我创建的文件,这是更好的选择吗?我觉得这种方法不对。

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

不需要处理临时文件

更新: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();
}

谢谢你的提示,我找不到memorystream类型。这足以完成我的搜索。用答案编辑了这个问题。