protected internal override void CheckPdfStream(PdfStream stream) { if (stream.ContainsKey(PdfName.F) || stream.ContainsKey(PdfName.FFilter) || stream.ContainsKey(PdfName.FDecodeParams )) { throw new PdfAConformanceException(PdfAConformanceException.StreamObjDictShallNotContainForFFilterOrFDecodeParams ); } PdfObject filter = stream.Get(PdfName.Filter); if (filter is PdfName) { if (filter.Equals(PdfName.LZWDecode)) { throw new PdfAConformanceException(PdfAConformanceException.LZWDecodeFilterIsNotPermitted); } } else { if (filter is PdfArray) { foreach (PdfObject f in ((PdfArray)filter)) { if (f.Equals(PdfName.LZWDecode)) { throw new PdfAConformanceException(PdfAConformanceException.LZWDecodeFilterIsNotPermitted); } } } } }
protected internal override void CheckPdfStream(PdfStream stream) { if (stream.ContainsKey(PdfName.F) || stream.ContainsKey(PdfName.FFilter) || stream.ContainsKey(PdfName.FDecodeParams )) { throw new PdfAConformanceException(PdfAConformanceException.STREAM_OBJECT_DICTIONARY_SHALL_NOT_CONTAIN_THE_F_FFILTER_OR_FDECODEPARAMS_KEYS ); } PdfObject filter = stream.Get(PdfName.Filter); if (filter is PdfName) { if (filter.Equals(PdfName.LZWDecode)) { throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); } } else { if (filter is PdfArray) { foreach (PdfObject f in ((PdfArray)filter)) { if (f.Equals(PdfName.LZWDecode)) { throw new PdfAConformanceException(PdfAConformanceException.LZWDECODE_FILTER_IS_NOT_PERMITTED); } } } } }
public override void CheckInlineImage(PdfStream inlineImage, PdfDictionary currentColorSpaces) { PdfObject filter = inlineImage.Get(PdfName.Filter); if (filter is PdfName) { if (filter.Equals(PdfName.LZWDecode)) { throw new PdfAConformanceException(PdfAConformanceException.LZWDecodeFilterIsNotPermitted); } } else { if (filter is PdfArray) { for (int i = 0; i < ((PdfArray)filter).Size(); i++) { PdfName f = ((PdfArray)filter).GetAsName(i); if (f.Equals(PdfName.LZWDecode)) { throw new PdfAConformanceException(PdfAConformanceException.LZWDecodeFilterIsNotPermitted); } } } } CheckImage(inlineImage, currentColorSpaces); }
public ICompressElement Create(PdfObject pdfObj, string filename, double?compressionLevel) { string clearFilter; if (pdfObj == null) { pdfObj = PdfName.CCITTFAXDECODE; clearFilter = ""; } else { clearFilter = pdfObj.ToString().Trim('[', ']'); } if (pdfObj.Equals(PdfName.CCITTFAXDECODE)) { var compressionPercent = compressionLevel ?? SetBestCompressionByDocName(filename); if (compressionPercent != null) { _ccitFaxCompressor.SetCompressionPercent((float)compressionPercent.Value); } return(_ccitFaxCompressor); } if (pdfObj.Equals(PdfName.DCTDECODE) || clearFilter.Equals(PdfName.DCTDECODE.ToString()) || pdfObj.Equals(PdfName.FLATEDECODE) || pdfObj.IsArray()) //when we have two filters simultaneously: Flat and DCTD //|| pdfObj.Equals(PdfName.JBIG2DECODE)) that filter doesn't supported in .net, iTextSharp, FreeImage { var compressionPercent = compressionLevel ?? SetBestCompressionByDocName(filename); if (compressionPercent != null) { _dctCompressor.SetCompressionPercent((float)compressionPercent.Value); } var preferredImageFormat = SetPreferredOutImageFormat(filename); if (preferredImageFormat != null) { _dctCompressor.SetPreferredOutputImageType(preferredImageFormat); } return(_dctCompressor); } return(null); }
protected override void CheckStream(PdfWriter writer, int key, Object obj1) { if (obj1 is PdfStream) { PdfStream stream = (PdfStream)obj1; if (stream.Contains(PdfName.F) || stream.Contains(PdfName.FFILTER) || stream.Contains(PdfName.FDECODEPARMS)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("stream.object.dictionary.shall.not.contain.the.f.ffilter.or.fdecodeparams.keys")); } PdfObject filter = GetDirectObject(stream.Get(PdfName.FILTER)); if (filter is PdfName) { if (filter.Equals(PdfName.LZWDECODE)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("lzwdecode.filter.is.not.permitted")); } } else if (filter is PdfArray) { foreach (PdfObject f in ((PdfArray)filter)) { if (f.Equals(PdfName.LZWDECODE)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("lzwdecode.filter.is.not.permitted")); } } } if (PdfName.FORM.Equals(stream.GetAsName(PdfName.SUBTYPE))) { if (stream.Contains(PdfName.OPI)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("a.form.xobject.dictionary.shall.not.contain.opi.key")); } if (stream.Contains(PdfName.PS)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("a.form.xobject.dictionary.shall.not.contain.ps.key")); } PdfDictionary group = GetDirectDictionary(stream.Get(PdfName.GROUP)); if (group != null) { PdfName s = group.GetAsName(PdfName.S); if (PdfName.TRANSPARENCY.Equals(s)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("a.group.object.with.an.s.key.with.a.value.of.transparency.shall.not.be.included.in.a.form.xobject")); } } } if (PdfName.PS.Equals(stream.GetAsName(PdfName.SUBTYPE))) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("postscript.xobjects.are.not.allowed")); } } }
private ImageFilter _DetectImageFilter() { ImageFilter result = ImageFilter.Unknown; PdfObject value = _fltr; if (_fltr == null) { return(ImageFilter.None); } if (_fltr is PdfArray) { value = ((PdfArray)_fltr).First(); } if (value.Equals(PdfName.FLATEDECODE)) { result = ImageFilter.FlateDecode; } else if (value.Equals(PdfName.DCTDECODE)) { result = ImageFilter.DCTDecode; } else if (value.Equals(PdfName.CCITTFAXDECODE)) { result = ImageFilter.CCITTFaxDecode; } else if (value.Equals(PdfName.JBIG2DECODE)) { result = ImageFilter.JBIG2DECODE; } else if (value.Equals(PdfName.JPXDECODE)) { result = ImageFilter.JPXDECODE; } else if (value.Equals(PdfName.ASCII85DECODE)) { result = ImageFilter.ASCII85DECODE; } if (result == ImageFilter.Unknown) { throw new Exception("Unknown Pixel Format"); } return(result); }
public virtual void RenderImage(ImageRenderInfo renderInfo) { IList <Rectangle> areasToBeCleaned = GetImageAreasToBeCleaned(renderInfo); if (areasToBeCleaned == null) { chunks.Add(new PdfCleanUpContentChunk.Image(false, null)); } else { PdfImageObject pdfImage = renderInfo.GetImage(); byte[] imageBytes = ProcessImage(pdfImage.GetImageAsBytes(), areasToBeCleaned); if (renderInfo.GetRef() == null && pdfImage != null) // true => inline image { PdfDictionary dict = pdfImage.GetDictionary(); PdfObject imageMask = dict.Get(PdfName.IMAGEMASK); Image image = Image.GetInstance(imageBytes); if (imageMask == null) { imageMask = dict.Get(PdfName.IM); } if (imageMask != null && imageMask.Equals(PdfBoolean.PDFTRUE)) { image.MakeMask(); } PdfContentByte canvas = Context.Canvas; canvas.AddImage(image, 1, 0, 0, 1, 0, 0, true); } else if (pdfImage != null && imageBytes != pdfImage.GetImageAsBytes()) { chunks.Add(new PdfCleanUpContentChunk.Image(true, imageBytes)); } } }
public PdfObject FindObjectByGuid(Guid guid) { PdfDictionary resources = (PdfDictionary)PdfReader.GetPdfObject(_page.Get(PdfName.RESOURCES)); PdfDictionary xobject = (PdfDictionary)PdfReader.GetPdfObject(resources.Get(PdfName.XOBJECT)); if (xobject == null) { return(null); } PdfName valueToFind = new PdfName(guid.ToString()); foreach (PdfName name in xobject.Keys) { PdfObject obj = xobject.Get(name); if (!obj.IsIndirect()) { continue; } PdfDictionary childObject = (PdfDictionary)PdfReader.GetPdfObject(obj); if (childObject == null) { continue; } PdfObject curValue = childObject.Get(new PdfName("ITXT_ObjectId")); if (curValue.Equals(valueToFind)) { return(obj); } } return(null); }
/// <summary>Gets the annotation specified by /A and /P entry values.</summary> /// <param name="pdfDocument">specifies the corresponding document</param> /// <returns>the annotation specified by /A and /P entry value.</returns> public virtual PdfFileAttachmentAnnotation GetAnnotation(PdfDocument pdfDocument) { PdfObject pValue = GetPdfObject().Get(PdfName.P); PdfPage page = null; if (pValue is PdfNumber) { // zero-based index is used page = pdfDocument.GetPage(((PdfNumber)pValue).IntValue() + 1); } else { if (pValue is PdfString) { PdfNameTree destsTree = pdfDocument.GetCatalog().GetNameTree(PdfName.Dests); IDictionary <String, PdfObject> dests = destsTree.GetNames(); PdfArray pdfArray = (PdfArray)dests.Get(((PdfString)pValue).GetValue()); if (null != pdfArray) { if (pdfArray.Get(0) is PdfNumber) { page = pdfDocument.GetPage(((PdfNumber)pdfArray.Get(0)).IntValue()); } else { page = pdfDocument.GetPage((PdfDictionary)pdfArray.Get(0)); } } } } IList <PdfAnnotation> pageAnnotations = null; if (null != page) { pageAnnotations = page.GetAnnotations(); } PdfObject aValue = GetPdfObject().Get(PdfName.A); PdfFileAttachmentAnnotation resultAnnotation = null; if (null != pageAnnotations) { if (aValue is PdfNumber) { resultAnnotation = (PdfFileAttachmentAnnotation)pageAnnotations[((PdfNumber)aValue).IntValue()]; } else { if (aValue is PdfString) { foreach (PdfAnnotation annotation in pageAnnotations) { if (aValue.Equals(annotation.GetName())) { resultAnnotation = (PdfFileAttachmentAnnotation)annotation; break; } } } } } if (null == resultAnnotation) { ILog logger = LogManager.GetLogger(typeof(iText.Kernel.Pdf.Action.PdfTarget)); logger.Error(iText.IO.LogMessageConstant.SOME_TARGET_FIELDS_ARE_NOT_SET_OR_INCORRECT); } return(resultAnnotation); }
//private Bitmap parseDict(PdfDictionary dict) //{ //} private Bitmap GetImagesFromPdfDict(PdfDictionary dict) { PdfDictionary res = (PdfDictionary)(PdfReader.GetPdfObject(dict.Get(PdfName.RESOURCES))); PdfDictionary xobj = (PdfDictionary)(PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT))); Bitmap bm = null; if (xobj != null) { foreach (PdfName name in xobj.Keys) { PdfObject obj = xobj.Get(name); if (obj.IsIndirect()) { PdfDictionary tg = (PdfDictionary)(PdfReader.GetPdfObject(obj)); PdfName subtype = (PdfName)(PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE))); if (PdfName.IMAGE.Equals(subtype)) { int xrefIdx = ((PRIndirectReference)obj).Number; PdfObject pdfObj = reader.GetPdfObject(xrefIdx); PRStream str = (PRStream)(pdfObj); PdfArray decode = tg.GetAsArray(PdfName.DECODE); int width = tg.GetAsNumber(PdfName.WIDTH).IntValue; int height = tg.GetAsNumber(PdfName.HEIGHT).IntValue; int bpc = tg.GetAsNumber(PdfName.BITSPERCOMPONENT).IntValue; PdfObject filter = tg.Get(PdfName.FILTER); if (filter.Equals(PdfName.FLATEDECODE)) { byte[] imageBytes = PdfReader.GetStreamBytesRaw(str); byte[] decodedBytes = PdfReader.FlateDecode(imageBytes); //decode the raw image byte[] streamBytes = PdfReader.DecodePredictor(decodedBytes, str.GetAsDict(PdfName.DECODEPARMS)); //decode predict to filter the bytes PixelFormat pixelFormat = PixelFormat.Format1bppIndexed; switch (bpc) //determine the BPC { case 1: pixelFormat = PixelFormat.Format1bppIndexed; break; case 8: pixelFormat = PixelFormat.Format8bppIndexed; break; case 24: pixelFormat = PixelFormat.Format24bppRgb; break; } bm = new Bitmap(width, height, pixelFormat); { BitmapData bmpData = bm.LockBits(new System.Drawing.Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, pixelFormat); int length = (int)Math.Ceiling(width * bpc / 8.0); for (int i = 0; i < height; i++) { int offset = i * length; int scanOffset = i * bmpData.Stride; Marshal.Copy(streamBytes, offset, new IntPtr(bmpData.Scan0.ToInt32() + scanOffset), length); } bm.UnlockBits(bmpData); } } else { iTextSharp.text.pdf.parser.PdfImageObject pdfImage = new iTextSharp.text.pdf.parser.PdfImageObject(str); bm = (System.Drawing.Bitmap)pdfImage.GetDrawingImage(); } int yDPI = bm.Height / 11; int xDPI = (bm.Width * 2) / 17; xDPI = Math.Abs(xDPI - 300) < 10 ? 300 : xDPI; yDPI = Math.Abs(yDPI - 300) < 10 ? 300 : yDPI; xDPI = Math.Abs(xDPI - 600) < 10 ? 600 : xDPI; yDPI = Math.Abs(yDPI - 600) < 10 ? 600 : yDPI; if (xDPI == yDPI) { bm.SetResolution(xDPI, yDPI); } else { } break; } else if (PdfName.FORM.Equals(subtype) || PdfName.GROUP.Equals(subtype)) { GetImagesFromPdfDict(tg); } } } } return(bm); }