示例#1
0
        }         // func Open

        /// <summary>Pdf data as an memory block.</summary>
        /// <param name="data">Pdf bytes.</param>
        /// <param name="offset">Offset in the data stream.</param>
        /// <param name="length">Length of the pdf file.</param>
        /// <param name="password">Password of the pdf</param>
        /// <param name="name">Name of the pdf-data stream.</param>
        /// <returns>Pdf document reader</returns>
        public static PdfReader Open(byte[] data, int offset = 0, int length = -1, string name = null, SecureString password = null)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (length < 0)
            {
                length = data.Length - offset;
            }
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(length), length, "Length");
            }

            name = name ?? "bytes.pdf";
            var mem = new PdfMemoryBlock(data, offset, length);

            lock (FPDF_LibraryLock)
            {
                var documentHandle = new PdfDocumentHandle(FPDF_LoadMemDocument(mem.Memory, mem.Size, IntPtr.Zero));
                if (documentHandle.IsInvalid)
                {
                    throw PdfReaderException.Create(name);
                }

                return(new PdfReader(documentHandle, name, mem));
            }
        }         // func Open
示例#2
0
        }         // func Open

        /// <summary>Open the file from an stream.</summary>
        /// <param name="data">Data stream (the must seek- and readable).</param>
        /// <param name="name">Name of the pdf.</param>
        /// <param name="password">Password of the pdf</param>
        /// <returns>Pdf document reader</returns>
        public static PdfReader Open(Stream data, string name = null, SecureString password = null)
        {
            var documentAccess = new PdfDocumentAccess(data);
            var fileAccess     = new FPDF_FILEACCESS()
            {
                getBlock  = documentAccess.GetBlockDelegateObject,
                m_FileLen = (uint)documentAccess.FileLength,
                param     = IntPtr.Zero
            };

            if (String.IsNullOrEmpty(name))
            {
                if (data is FileStream fs)
                {
                    name = fs.Name;
                }
                else
                {
                    name = "stream.pdf";
                }
            }

            lock (FPDF_LibraryLock)
            {
                var documentHandle = new PdfDocumentHandle(FPDF_LoadCustomDocument(ref fileAccess, IntPtr.Zero));
                if (documentHandle.IsInvalid)
                {
                    throw PdfReaderException.Create(name);
                }

                return(new PdfReader(documentHandle, name, documentAccess));
            }
        }         // func Open
示例#3
0
        }         // proc SetPrintTextWithGDI

        #region -- Open ---------------------------------------------------------------

        /// <summary>Open a pdf-file</summary>
        /// <param name="fileName">Filename</param>
        /// <param name="password">Password of the pdf</param>
        /// <returns>Pdf document reader</returns>
        public static PdfReader Open(string fileName, SecureString password = null)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            lock (FPDF_LibraryLock)
            {
                var documentHandle = new PdfDocumentHandle(FPDF_LoadDocument(fileName, IntPtr.Zero));
                if (documentHandle.IsInvalid)
                {
                    throw PdfReaderException.Create(fileName);
                }

                return(new PdfReader(documentHandle, fileName, null));
            }
        }         // func Open
示例#4
0
        private PdfReader(PdfDocumentHandle documentHandle, string pdfName, IDisposable dispose)
        {
            this.documentHandle = documentHandle ?? throw new ArgumentNullException(nameof(documentHandle));
            this.pdfName        = pdfName ?? "unknown.pdf";
            this.dispose        = dispose;

            Properties = new PdfProperties(this);

            // read pdf version
            if (!FPDF_GetFileVersion(documentHandle.DangerousGetHandle(), out var tmp))
            {
                throw PdfReaderException.Create(pdfName);
            }
            pdfVersion = new Version(tmp / 10, tmp % 10);

            // read page count
            pageCount = FPDF_GetPageCount(documentHandle.DangerousGetHandle());
            if (pageCount < 0)
            {
                throw PdfReaderException.Create(pdfName);
            }
        }         // ctor