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

PDFsharp - Open PDF from System.Byte[] Variable

我正在使用 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 作为字节数组。 将 Byte 数组保存为 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();
}