Skip to content

Fixes for loading document from stream#10

Open
avonwyss wants to merge 3 commits intoZubastic:masterfrom
avonwyss:stream
Open

Fixes for loading document from stream#10
avonwyss wants to merge 3 commits intoZubastic:masterfrom
avonwyss:stream

Conversation

@avonwyss
Copy link

The PdfDocument constructor which accepts a stream and the FPDF_LoadDocument methods were confusing, since they did not use their "stream" and "count" parameters, but instead the low-level undocumented "fileRead".

I cleaned this up, and added a small improvement when using MemoryStream streams (direcly copy from buffer instead of reading from stream). Successfully tested the functionality with the Test.Console application:

using (var docStream = new MemoryStream()) {
    using (var srcStream = new FileStream("TestDoc.pdf", FileMode.Open))
        srcStream.CopyTo(docStream);
    docStream.Seek(0, SeekOrigin.Begin);
    using (var doc = new PdfDocument(docStream, 0, "password")) {

@bradleypeet
Copy link

bradleypeet commented Jul 29, 2025

This is how I do it in my fork of this project. (I have not submitted a pull request.)

	public PdfDocument(Stream stream, int count = 0, string password = null)
            : this(FPDF_FILEREAD.FromStream(stream, count), password)
        {
        }

        internal PdfDocument(FPDF_FILEREAD reader, string password = null)
            : this(PDFium.FPDF_LoadCustomDocument(reader, password))
        {
            // must maintain reference to reader to prevent the callback delegate (passed as an
            // unmanaged function pointer to native pdfium lib) from getting garbage collected.
            streamReader = reader;
        }

@avonwyss
Copy link
Author

            // must maintain reference to reader to prevent the callback delegate (passed as an
            // unmanaged function pointer to native pdfium lib) from getting garbage collected.

That's indeed a very good point! I have adjusted my PR accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants