Exemple #1
0
        public PdfFile(Stream stream, string?password)
        {
            PdfLibrary.EnsureLoaded();

            _stream = stream ?? throw new ArgumentNullException(nameof(stream));
            _id     = StreamManager.Register(stream);

            var document = NativeMethods.FPDF_LoadCustomDocument(stream, password, _id);

            if (document == IntPtr.Zero)
            {
                throw new PdfException((PdfError)NativeMethods.FPDF_GetLastError());
            }

            LoadDocument(document);
        }
Exemple #2
0
        private static int FPDF_GetBlock(IntPtr param, uint position, IntPtr buffer, uint size)
        {
            var stream = StreamManager.Get((int)param);

            if (stream == null)
            {
                return(0);
            }
            byte[] managedBuffer = new byte[size];

            stream.Position = position;
            int read = stream.Read(managedBuffer, 0, (int)size);

            if (read != size)
            {
                return(0);
            }

            Marshal.Copy(managedBuffer, 0, buffer, (int)size);
            return(1);
        }
Exemple #3
0
        private void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                StreamManager.Unregister(_id);

                if (_form != IntPtr.Zero)
                {
                    NativeMethods.FORM_DoDocumentAAction(_form, NativeMethods.FPDFDOC_AACTION.WC);
                    // this call is needed for PDFium builds with PDF_ENABLE_XFA enabled
                    // and PDF_ENABLE_XFA implies JS support (PDF_ENABLE_V8 is needed for that)
                    // since we don't ship PDFium with V8 we can skip this
                    // otherwise we have to deal with some nasty unmanaged memory corruption
                    //NativeMethods.FPDFDOC_ExitFormFillEnvironment(_form);
                    _form = IntPtr.Zero;
                }

                if (_document != IntPtr.Zero)
                {
                    NativeMethods.FPDF_CloseDocument(_document);
                    _document = IntPtr.Zero;
                }

                if (_formCallbacksHandle !.IsAllocated)
                {
                    _formCallbacksHandle !.Free();
                }

                if (_stream != null)
                {
                    _stream.Dispose();
                    _stream = null;
                }

                _disposed = true;
            }
        }