/// <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 = DecodeTiffAndPngBytes(bytes); } catch (System.IO.IOException e) { throw new Exception("IO exception in PdfImageXObject", e); } } } return(bytes); }
/// <summary>This method acts like a check that bytes that were parsed are really all image bytes.</summary> /// <remarks> /// This method acts like a check that bytes that were parsed are really all image bytes. If it's true, /// then decoding will succeed, but if not all image bytes were read and "<ws>EI<ws>" bytes were just a part of the image, /// then decoding should fail. /// Not the best solution, but probably there is no better and more reliable way to check this. /// <p> /// Drawbacks: slow; images with DCTDecode, JBIG2Decode and JPXDecode filters couldn't be checked as iText doesn't /// support these filters; what if decoding will succeed eventhough it's not all bytes?; also I'm not sure that all /// filters throw an exception in case data is corrupted (For example, FlateDecodeFilter seems not to throw an exception). /// </remarks> private static bool InlineImageStreamBytesAreComplete(byte[] samples, PdfDictionary imageDictionary) { try { 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); PdfReader.DecodeBytes(samples, imageDictionary, filters); } catch (Exception) { return(false); } return(true); }