private static bool InlineImageStreamBytesAreComplete(byte[] samples, PdfDictionary imageDictionary) { try { PdfReader.DecodeBytes(samples, imageDictionary, FilterHandlers.GetDefaultFilterHandlers()); return(true); } catch (IOException e) { return(false); } }
/** * Creats a PdfImage object using an explicitly provided dictionary and image bytes * @param dictionary the dictionary for the image * @param samples the samples * @since 5.0.3 */ protected internal PdfImageObject(PdfDictionary dictionary, byte[] samples, PdfDictionary colorSpaceDic) { this.dictionary = dictionary; this.colorSpaceDic = colorSpaceDic; TrackingFilter trackingFilter = new TrackingFilter(); IDictionary <PdfName, FilterHandlers.IFilterHandler> handlers = new Dictionary <PdfName, FilterHandlers.IFilterHandler>(FilterHandlers.GetDefaultFilterHandlers()); handlers[PdfName.JBIG2DECODE] = trackingFilter; handlers[PdfName.DCTDECODE] = trackingFilter; handlers[PdfName.JPXDECODE] = trackingFilter; imageBytes = PdfReader.DecodeBytes(samples, dictionary, handlers); if (trackingFilter.lastFilterName != null) { if (PdfName.JBIG2DECODE.Equals(trackingFilter.lastFilterName)) { streamContentType = ImageBytesType.JBIG2; } else if (PdfName.DCTDECODE.Equals(trackingFilter.lastFilterName)) { streamContentType = ImageBytesType.JPG; } else if (PdfName.JPXDECODE.Equals(trackingFilter.lastFilterName)) { streamContentType = ImageBytesType.JP2; } } else { DecodeImageBytes(); } }
/// <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); }
/// <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>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); }