Example #1
0
 /// <summary>Gets image bytes.</summary>
 /// <remarks>
 /// Gets image bytes.
 /// Note,
 /// <see cref="iText.Kernel.Pdf.PdfName.DCTDecode"/>
 /// ,
 /// <see cref="iText.Kernel.Pdf.PdfName.JBIG2Decode"/>
 /// and
 /// <see cref="iText.Kernel.Pdf.PdfName.JPXDecode"/>
 /// filters will be ignored.
 /// </remarks>
 /// <param name="decoded">
 /// if
 /// <see langword="true"/>
 /// , decodes stream bytes.
 /// </param>
 /// <returns>byte array.</returns>
 public virtual byte[] GetImageBytes(bool decoded)
 {
     byte[] bytes;
     bytes = GetPdfObject().GetBytes(false);
     if (decoded)
     {
         IDictionary <PdfName, IFilterHandler> filters = new Dictionary <PdfName, IFilterHandler>(FilterHandlers.GetDefaultFilterHandlers
                                                                                                      ());
         DoNothingFilter stubFilter = new DoNothingFilter();
         filters.Put(PdfName.DCTDecode, stubFilter);
         filters.Put(PdfName.JBIG2Decode, stubFilter);
         filters.Put(PdfName.JPXDecode, stubFilter);
         bytes = PdfReader.DecodeBytes(bytes, GetPdfObject(), filters);
         if (stubFilter.GetLastFilterName() == null)
         {
             try {
                 bytes = new ImagePdfBytesInfo(this).DecodeTiffAndPngBytes(bytes);
             }
             catch (System.IO.IOException e) {
                 throw new Exception("IO exception in PdfImageXObject", e);
             }
         }
     }
     return(bytes);
 }
Example #2
0
        /// <summary>
        /// Identifies the type of the image that is stored in the bytes of this
        /// <see cref="PdfImageXObject"/>
        /// .
        /// Note that this has nothing to do with the original type of the image. For instance, the return value
        /// of this method will never be
        /// <see cref="iText.IO.Image.ImageType.PNG"/>
        /// as we loose this information when converting a
        /// PNG image into something that can be put into a PDF file.
        /// The possible values are:
        /// <see cref="iText.IO.Image.ImageType.JPEG"/>
        /// ,
        /// <see cref="iText.IO.Image.ImageType.JPEG2000"/>
        /// ,
        /// <see cref="iText.IO.Image.ImageType.JBIG2"/>
        /// ,
        /// <see cref="iText.IO.Image.ImageType.TIFF"/>
        /// ,
        /// <see cref="iText.IO.Image.ImageType.PNG"/>
        /// </summary>
        /// <returns>the identified type of image</returns>
        public virtual ImageType IdentifyImageType()
        {
            PdfObject filter  = GetPdfObject().Get(PdfName.Filter);
            PdfArray  filters = new PdfArray();

            if (filter != null)
            {
                if (filter.GetObjectType() == PdfObject.NAME)
                {
                    filters.Add(filter);
                }
                else
                {
                    if (filter.GetObjectType() == PdfObject.ARRAY)
                    {
                        filters = ((PdfArray)filter);
                    }
                }
            }
            for (int i = filters.Size() - 1; i >= 0; i--)
            {
                PdfName filterName = (PdfName)filters.Get(i);
                if (PdfName.DCTDecode.Equals(filterName))
                {
                    return(ImageType.JPEG);
                }
                else
                {
                    if (PdfName.JBIG2Decode.Equals(filterName))
                    {
                        return(ImageType.JBIG2);
                    }
                    else
                    {
                        if (PdfName.JPXDecode.Equals(filterName))
                        {
                            return(ImageType.JPEG2000);
                        }
                    }
                }
            }
            // None of the previous types match
            ImagePdfBytesInfo imageInfo = new ImagePdfBytesInfo(this);

            if (imageInfo.GetPngColorType() < 0)
            {
                return(ImageType.TIFF);
            }
            else
            {
                return(ImageType.PNG);
            }
        }
Example #3
0
 /// <summary>Gets image bytes.</summary>
 /// <remarks>
 /// Gets image bytes.
 /// Note,
 /// <see cref="iText.Kernel.Pdf.PdfName.DCTDecode"/>
 /// ,
 /// <see cref="iText.Kernel.Pdf.PdfName.JBIG2Decode"/>
 /// and
 /// <see cref="iText.Kernel.Pdf.PdfName.JPXDecode"/>
 /// filters will be ignored.
 /// </remarks>
 /// <param name="decoded">
 /// if
 /// <see langword="true"/>
 /// , decodes stream bytes.
 /// </param>
 /// <returns>byte array.</returns>
 public virtual byte[] GetImageBytes(bool decoded)
 {
     // TODO: DEVSIX-1792 replace `.getBytes(false)` with `getBytes(true) and remove manual decoding
     byte[] bytes = GetPdfObject().GetBytes(false);
     if (decoded)
     {
         IDictionary <PdfName, IFilterHandler> filters = new Dictionary <PdfName, IFilterHandler>(FilterHandlers.GetDefaultFilterHandlers
                                                                                                      ());
         filters.Put(PdfName.JBIG2Decode, new DoNothingFilter());
         bytes = PdfReader.DecodeBytes(bytes, GetPdfObject(), filters);
         ImageType imageType = IdentifyImageType();
         if (imageType == ImageType.TIFF || imageType == ImageType.PNG)
         {
             try {
                 bytes = new ImagePdfBytesInfo(this).DecodeTiffAndPngBytes(bytes);
             }
             catch (System.IO.IOException e) {
                 throw new Exception("IO exception in PdfImageXObject", e);
             }
         }
     }
     return(bytes);
 }