Scyther312
Scyther312

Reputation: 73

PDFsharp - Open PDF from System.Byte[] Variable

I am working with PDFsharp, and I am trying to open a PDF that is represented as Byte[] variable. Here is a simplified example:

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

I can read a PDF from its path, but I had rather work with the PDF as Byte array. Is it better option to save the Byte array as a PDF and then read it, and afterward delete the file that I created? This approach does not feel right to me.

Upvotes: 4

Views: 9771

Answers (1)

You can write the byte array into a MemoryStream or create a MemoryStream for that byte array, and then use PDFsharp to open the PDF from that stream.

No need to mess with temporary files.

Update: The solution found by the OP (meanwhile deleted from the question):

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

Upvotes: 9

Related Questions