コード例 #1
0
ファイル: Document.cs プロジェクト: waffle-iron/nequeo
        /// <summary>
        /// Extracts all images from a specified page of a PDF file.
        /// </summary>
        /// <param name="pdf">The PDF stream.</param>
        /// <param name="password">The password used to protect the document.</param>
        /// <returns>Returns an array of images
        /// where the key is a suggested file name, in the format: PDF filename without extension,
        /// page number and image index in the page.</returns>
        public Dictionary <string, System.Drawing.Image> ExtractImages(Stream pdf, string password = "")
        {
            byte[] pass = null;
            iTextSharp.text.pdf.PdfReader pdfReader = null;
            var images = new Dictionary <string, System.Drawing.Image>();

            try
            {
                // If no password.
                if (String.IsNullOrEmpty(password))
                {
                    pdfReader = new iTextSharp.text.pdf.PdfReader(pdf);
                }
                else
                {
                    pass      = Encoding.Default.GetBytes(password);
                    pdfReader = new iTextSharp.text.pdf.PdfReader(pdf, pass);
                }

                // Create the pdf parser.
                var parser = new PdfReaderContentParser(pdfReader);
                ImageRenderListener listener = null;

                for (var i = 1; i <= pdfReader.NumberOfPages; i++)
                {
                    // Parse the pdf stream.
                    parser.ProcessContent(i, (listener = new ImageRenderListener()));
                    var index = 1;

                    // If images exist.
                    if (listener.Images.Count > 0)
                    {
                        // For each image extracted.
                        foreach (var pair in listener.Images)
                        {
                            // Add the image.
                            images.Add(string.Format("Page_{ 1} Image_{ 2} { 3}", i.ToString("D4"), index.ToString("D4"), pair.Value), pair.Key);
                            index++;
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (pdfReader != null)
                {
                    pdfReader.Close();
                }
            }

            // Return the images.
            return(images);
        }
コード例 #2
0
ファイル: Document.cs プロジェクト: waffle-iron/nequeo
        /// <summary>
        /// Extracts all images from a specified page of a PDF file.
        /// </summary>
        /// <param name="pdf">The PDF stream.</param>
        /// <param name="pageNumber">The page number to look for images.</param>
        /// <param name="password">The password used to protect the document.</param>
        /// <returns>Returns an array of images
        /// where the key is a suggested file name, in the format: PDF filename without extension,
        /// page number and image index in the page.</returns>
        public Dictionary <string, System.Drawing.Image> ExtractImages(Stream pdf, int pageNumber, string password = "")
        {
            byte[] pass = null;
            iTextSharp.text.pdf.PdfReader             pdfReader = null;
            Dictionary <string, System.Drawing.Image> images    = new Dictionary <string, System.Drawing.Image>();

            try
            {
                // If no password.
                if (String.IsNullOrEmpty(password))
                {
                    pdfReader = new iTextSharp.text.pdf.PdfReader(pdf);
                }
                else
                {
                    pass      = Encoding.Default.GetBytes(password);
                    pdfReader = new iTextSharp.text.pdf.PdfReader(pdf, pass);
                }

                // Create the content reader.
                PdfReaderContentParser parser   = new PdfReaderContentParser(pdfReader);
                ImageRenderListener    listener = null;

                // Parse the content.
                parser.ProcessContent(pageNumber, (listener = new ImageRenderListener()));
                int index = 1;

                // If images have been found.
                if (listener.Images.Count > 0)
                {
                    // Add each image to the list.
                    foreach (KeyValuePair <System.Drawing.Image, string> pair in listener.Images)
                    {
                        // Add the image.
                        images.Add(string.Format("Page_{ 1} Image_{ 2} { 3}", pageNumber.ToString("D4"), index.ToString("D4"), pair.Value), pair.Key);
                        index++;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (pdfReader != null)
                {
                    pdfReader.Close();
                }
            }

            // Return the images.
            return(images);
        }
コード例 #3
0
ファイル: Document.cs プロジェクト: waffle-iron/nequeo
        /// <summary>
        /// Checks whether a specified page of a PDF file contains images.
        /// </summary>
        /// <param name="pdf">The PDF stream.</param>
        /// <param name="pageNumber">The page number to look for images.</param>
        /// <param name="password">The password used to protect the document.</param>
        /// <returns>True if the page contains at least one image; false otherwise.</returns>
        public bool PageContainsImages(Stream pdf, int pageNumber, string password = "")
        {
            bool result = false;
            ImageRenderListener listener = null;

            byte[] pass = null;
            iTextSharp.text.pdf.PdfReader pdfReader = null;

            try
            {
                // If no password.
                if (String.IsNullOrEmpty(password))
                {
                    pdfReader = new iTextSharp.text.pdf.PdfReader(pdf);
                }
                else
                {
                    pass      = Encoding.Default.GetBytes(password);
                    pdfReader = new iTextSharp.text.pdf.PdfReader(pdf, pass);
                }

                // Parse the stream.
                var parser = new PdfReaderContentParser(pdfReader);
                parser.ProcessContent(pageNumber, (listener = new ImageRenderListener()));
                result = (listener.Images.Count > 0 ? true : false);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (pdfReader != null)
                {
                    pdfReader.Close();
                }
            }

            return(result);
        }