protected internal override void CheckForm(PdfDictionary form) { if (form == null) { return; } PdfBoolean needAppearances = form.GetAsBoolean(PdfName.NeedAppearances); if (needAppearances != null && needAppearances.GetValue()) { throw new PdfAConformanceException(PdfAConformanceException.NeedAppearancesFlagOfTheInteractiveFormDictionaryShallEitherNotBePresentedOrShallBeFalse ); } CheckResources(form.GetAsDictionary(PdfName.DR)); PdfArray fields = form.GetAsArray(PdfName.Fields); if (fields != null) { fields = GetFormFields(fields); foreach (PdfObject field in fields) { PdfDictionary fieldDic = (PdfDictionary)field; if (fieldDic.ContainsKey(PdfName.A) || fieldDic.ContainsKey(PdfName.AA)) { throw new PdfAConformanceException(PdfAConformanceException.WidgetAnnotationDictionaryOrFieldDictionaryShallNotIncludeAOrAAEntry ); } CheckResources(fieldDic.GetAsDictionary(PdfName.DR)); } } }
protected internal override void CheckForm(PdfDictionary form) { if (form == null) { return; } PdfBoolean needAppearances = form.GetAsBoolean(PdfName.NeedAppearances); if (needAppearances != null && needAppearances.GetValue()) { throw new PdfAConformanceException(PdfAConformanceException.NEEDAPPEARANCES_FLAG_OF_THE_INTERACTIVE_FORM_DICTIONARY_SHALL_EITHER_NOT_BE_PRESENTED_OR_SHALL_BE_FALSE ); } CheckResources(form.GetAsDictionary(PdfName.DR)); PdfArray fields = form.GetAsArray(PdfName.Fields); if (fields != null) { fields = GetFormFields(fields); foreach (PdfObject field in fields) { PdfDictionary fieldDic = (PdfDictionary)field; if (fieldDic.ContainsKey(PdfName.A) || fieldDic.ContainsKey(PdfName.AA)) { throw new PdfAConformanceException(PdfAConformanceException.WIDGET_ANNOTATION_DICTIONARY_OR_FIELD_DICTIONARY_SHALL_NOT_INCLUDE_A_OR_AA_ENTRY ); } CheckResources(fieldDic.GetAsDictionary(PdfName.DR)); } } }
/** * Deletes redact annotations from the page and substitutes them with either OverlayText or RO object if it's needed. */ private void DeleteRedactAnnots(int pageNum) { HashSet2 <String> indirRefs; redactAnnotIndirRefs.TryGetValue(pageNum, out indirRefs); if (indirRefs == null || indirRefs.Count == 0) { return; } PdfReader reader = pdfStamper.Reader; PdfContentByte canvas = pdfStamper.GetOverContent(pageNum); PdfDictionary pageDict = reader.GetPageN(pageNum); PdfArray annotsArray = pageDict.GetAsArray(PdfName.ANNOTS); // j is for access annotRect (i can be decreased, so we need to store additional index, // indicating current position in ANNOTS array in case if we don't remove anything for (int i = 0, j = 0; i < annotsArray.Size; ++i, ++j) { PdfIndirectReference annotIndRef = annotsArray.GetAsIndirectObject(i); PdfDictionary annotDict = annotsArray.GetAsDict(i); if (indirRefs.Contains(annotIndRef.ToString()) || indirRefs.Contains(GetParentIndRefStr(annotDict))) { PdfStream formXObj = annotDict.GetAsStream(PdfName.RO); PdfString overlayText = annotDict.GetAsString(PdfName.OVERLAYTEXT); if (FillCleanedArea && formXObj != null) { PdfArray rectArray = annotDict.GetAsArray(PdfName.RECT); Rectangle annotRect = new Rectangle(rectArray.GetAsNumber(0).FloatValue, rectArray.GetAsNumber(1).FloatValue, rectArray.GetAsNumber(2).FloatValue, rectArray.GetAsNumber(3).FloatValue); InsertFormXObj(canvas, pageDict, formXObj, clippingRects[j], annotRect); } else if (FillCleanedArea && overlayText != null && overlayText.ToUnicodeString().Length > 0) { DrawOverlayText(canvas, clippingRects[j], overlayText, annotDict.GetAsString(PdfName.DA), annotDict.GetAsNumber(PdfName.Q), annotDict.GetAsBoolean(PdfName.REPEAT)); } annotsArray.Remove(i--); // array size is changed, so we need to decrease i } } if (annotsArray.Size == 0) { pageDict.Remove(PdfName.ANNOTS); } }
protected override void CheckPdfObject(PdfWriter writer, int key, Object obj1) { if (obj1 is PdfNumber) { PdfNumber number = (PdfNumber)obj1; if (Math.Abs(number.DoubleValue) > maxRealValue && number.ToString().Contains(".")) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("real.number.is.out.of.range")); } } else if (obj1 is PdfString) { PdfString str = (PdfString)obj1; if (str.GetBytes().Length > maxStringLength) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.string.is.too.long")); } } else if (obj1 is PdfArray) { PdfArray array = (PdfArray)obj1; if (array.Size > maxArrayLength) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.array.is.out.of.bounds")); } } else if (obj1 is PdfDictionary) { PdfDictionary dictionary = (PdfDictionary)obj1; if (dictionary.Size > maxDictionaryLength) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.dictionary.is.out.of.bounds")); } if (PdfName.CATALOG.Equals(dictionary.GetAsName(PdfName.TYPE))) { if (dictionary.Contains(PdfName.AA)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.document.catalog.dictionary.shall.not.include.an.aa.entry")); } if (CheckStructure(conformanceLevel)) { PdfDictionary markInfo = dictionary.GetAsDict(PdfName.MARKINFO); if (markInfo == null || markInfo.GetAsBoolean(PdfName.MARKED) == null || markInfo.GetAsBoolean(PdfName.MARKED).BooleanValue == false) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("document.catalog.dictionary.shall.include.a.markinfo.dictionary.whose.entry.marked.shall.have.a.value.of.true")); } if (!dictionary.Contains(PdfName.LANG)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("document.catalog.dictionary.should.contain.lang.entry")); } } } } }
protected internal override void CheckLogicalStructure(PdfDictionary catalog) { if (CheckStructure(conformanceLevel)) { PdfDictionary markInfo = catalog.GetAsDictionary(PdfName.MarkInfo); if (markInfo == null || markInfo.GetAsBoolean(PdfName.Marked) == null || !markInfo.GetAsBoolean(PdfName.Marked ).GetValue()) { throw new PdfAConformanceException(PdfAConformanceException.CatalogShallIncludeMarkInfoDictionaryWithMarkedTrueValue ); } if (!catalog.ContainsKey(PdfName.Lang)) { ILogger logger = LoggerFactory.GetLogger(typeof(PdfAChecker)); logger.Warn(PdfAConformanceException.CatalogShallContainLangEntry); } } }
protected internal override void CheckLogicalStructure(PdfDictionary catalog) { if (CheckStructure(conformanceLevel)) { PdfDictionary markInfo = catalog.GetAsDictionary(PdfName.MarkInfo); if (markInfo == null || markInfo.GetAsBoolean(PdfName.Marked) == null || !markInfo.GetAsBoolean(PdfName.Marked ).GetValue()) { throw new PdfAConformanceException(PdfAConformanceException.A_CATALOG_SHALL_INCLUDE_MARK_INFO_DICTIONARY_WITH_MARKED_TRUE_VALUE ); } if (!catalog.ContainsKey(PdfName.Lang)) { ILog logger = LogManager.GetLogger(typeof(PdfAChecker)); logger.Warn(PdfAConformanceLogMessageConstant.CATALOG_SHOULD_CONTAIN_LANG_ENTRY); } } }
protected override void CheckPdfObject(PdfWriter writer, int key, Object obj1) { if (obj1 is PdfNumber) { PdfNumber number = (PdfNumber)obj1; if (Math.Abs(number.DoubleValue) > maxRealValue && number.ToString().Contains(".")) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("real.number.is.out.of.range")); } } else if (obj1 is PdfString) { PdfString str = (PdfString)obj1; if (str.GetBytes().Length > maxStringLength) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.string.is.too.long")); } } else if (obj1 is PdfArray) { PdfArray array = (PdfArray)obj1; if (array.Size > maxArrayLength) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.array.is.out.of.bounds")); } } else if (obj1 is PdfDictionary) { PdfDictionary dictionary = (PdfDictionary)obj1; if (dictionary.Size > maxDictionaryLength) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.dictionary.is.out.of.bounds")); } PdfName type = dictionary.GetAsName(PdfName.TYPE); if (PdfName.CATALOG.Equals(type)) { if (!dictionary.Contains(PdfName.METADATA)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.document.catalog.dictionary.shall.contain.metadata")); } if (dictionary.Contains(PdfName.AA)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.document.catalog.dictionary.shall.not.include.an.aa.entry")); } if (dictionary.Contains(PdfName.NAMES)) { PdfDictionary names = GetDirectDictionary(dictionary.Get(PdfName.NAMES)); if (names != null && names.Contains(PdfName.EMBEDDEDFILES)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.document.catalog.dictionary.shall.not.include.embeddedfiles.names.entry")); } } if (CheckStructure(conformanceLevel)) { PdfDictionary markInfo = GetDirectDictionary(dictionary.Get(PdfName.MARKINFO)); if (markInfo == null || markInfo.GetAsBoolean(PdfName.MARKED) == null || markInfo.GetAsBoolean(PdfName.MARKED).BooleanValue == false) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("document.catalog.dictionary.shall.include.a.markinfo.dictionary.whose.entry.marked.shall.have.a.value.of.true")); } if (!dictionary.Contains(PdfName.LANG)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("document.catalog.dictionary.should.contain.lang.entry")); } } } else if (PdfName.PAGE.Equals(type)) { if (dictionary.Contains(PdfName.AA)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("page.dictionary.shall.not.include.aa.entry")); } } else if (PdfName.OUTPUTINTENT.Equals(type)) { PdfObject destOutputIntent = dictionary.Get(PdfName.DESTOUTPUTPROFILE); if (destOutputIntent != null && pdfaDestOutputIntent != null) { if (pdfaDestOutputIntent.IndRef != destOutputIntent.IndRef) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage( "if.outputintents.array.more.than.one.entry.the.same.indirect.object")); } } else { pdfaDestOutputIntent = destOutputIntent; } PdfName gts = dictionary.GetAsName(PdfName.S); if (pdfaDestOutputIntent != null) { if (PdfName.GTS_PDFA1.Equals(gts)) { if (pdfaOutputIntentColorSpace != null) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("a.pdfa.file.may.have.only.one.pdfa.outputintent")); } pdfaOutputIntentColorSpace = ""; ICC_Profile icc_profile = writer.ColorProfile; pdfaOutputIntentColorSpace = Encoding.GetEncoding("US-ASCII").GetString(icc_profile.Data, 16, 4); } } else { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("outputintent.shall.have.gtspdfa1.and.destoutputintent")); } } } }
protected override void CheckPdfObject(PdfWriter writer, int key, Object obj1) { if (obj1 is PdfNumber) { PdfNumber number = (PdfNumber)obj1; if (Math.Abs(number.DoubleValue) > maxRealValue && number.ToString().Contains(".")) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("real.number.is.out.of.range")); } } else if (obj1 is PdfString) { PdfString str = (PdfString)obj1; if (str.GetBytes().Length > maxStringLength) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.string.is.too.long")); } } else if (obj1 is PdfArray) { PdfArray array = (PdfArray)obj1; if (array.Size > maxArrayLength) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.array.is.out.of.bounds")); } } else if (obj1 is PdfDictionary) { PdfDictionary dictionary = (PdfDictionary)obj1; if (dictionary.Size > maxDictionaryLength) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.dictionary.is.out.of.bounds")); } PdfName type = dictionary.GetAsName(PdfName.TYPE); if (PdfName.CATALOG.Equals(type)) { if (!dictionary.Contains(PdfName.METADATA)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.document.catalog.dictionary.shall.contain.metadata")); } if (dictionary.Contains(PdfName.AA)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.document.catalog.dictionary.shall.not.include.an.aa.entry")); } if (dictionary.Contains(PdfName.NAMES)) { PdfDictionary names = GetDirectDictionary(dictionary.Get(PdfName.NAMES)); if (names != null && names.Contains(PdfName.EMBEDDEDFILES)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.document.catalog.dictionary.shall.not.include.embeddedfiles.names.entry")); } } if (CheckStructure(conformanceLevel)) { PdfDictionary markInfo = GetDirectDictionary(dictionary.Get(PdfName.MARKINFO)); if (markInfo == null || markInfo.GetAsBoolean(PdfName.MARKED) == null || markInfo.GetAsBoolean(PdfName.MARKED).BooleanValue == false) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("document.catalog.dictionary.shall.include.a.markinfo.dictionary.whose.entry.marked.shall.have.a.value.of.true")); } if (!dictionary.Contains(PdfName.LANG)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("document.catalog.dictionary.should.contain.lang.entry")); } } PdfArray outputIntents = GetDirectArray(dictionary.Get(PdfName.OUTPUTINTENTS)); bool pdfa1OutputIntentFound = false; if (outputIntents != null && outputIntents.Size > 0) { for (int i = 0; i < outputIntents.Size; i++) { PdfDictionary outputIntentDictionary = GetDirectDictionary(outputIntents[i]); PdfName gts = outputIntentDictionary.GetAsName(PdfName.S); if (PdfName.GTS_PDFA1.Equals(gts)) { if (pdfa1OutputIntentFound) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("a.pdfa.file.may.have.only.one.pdfa.outputintent")); } pdfa1OutputIntentFound = true; } if (outputIntentDictionary != null) { PdfObject destOutputIntent = outputIntentDictionary.Get(PdfName.DESTOUTPUTPROFILE); if (destOutputIntent == null && PdfName.GTS_PDFA1.Equals(gts)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("outputintent.shall.have.gtspdfa1.and.destoutputintent")); } } } } if ((rgbUsed || cmykUsed || grayUsed) && !pdfa1OutputIntentFound) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("if.device.rgb.cmyk.gray.used.in.file.that.file.shall.contain.pdfa.outputintent")); } } else if (PdfName.PAGE.Equals(type)) { if (dictionary.Contains(PdfName.AA)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("page.dictionary.shall.not.include.aa.entry")); } } else if (PdfName.OUTPUTINTENT.Equals(type)) { PdfObject iccProfileStream = dictionary.Get(PdfName.DESTOUTPUTPROFILE); String inputColorSpace = ""; if (iccProfileStream != null) { ICC_Profile icc_profile = writer.ColorProfile; inputColorSpace = Encoding.GetEncoding("US-ASCII").GetString(icc_profile.Data, 16, 4); } PdfName gts = dictionary.GetAsName(PdfName.S); if (!PdfName.GTS_PDFA1.Equals(gts)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("outputintent.shall.have.gtspdfa1.and.destoutputintent")); } if ("RGB ".Equals(inputColorSpace)) { if (cmykUsed) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("devicecmyk.may.be.used.only.if.the.file.has.a.cmyk.pdfa.outputIntent")); } } else if ("CMYK".Equals(inputColorSpace)) { if (rgbUsed) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("devicergb.may.be.used.only.if.the.file.has.a.rgb.pdfa.outputIntent")); } } else { if (cmykUsed) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("devicecmyk.may.be.used.only.if.the.file.has.a.cmyk.pdfa.outputIntent")); } if (rgbUsed) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("devicergb.may.be.used.only.if.the.file.has.a.rgb.pdfa.outputIntent")); } } } } }
public virtual byte[] Decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDictionary streamDictionary ) { PdfNumber wn = streamDictionary.GetAsNumber(PdfName.Width); PdfNumber hn = streamDictionary.GetAsNumber(PdfName.Height); if (wn == null || hn == null) { throw new PdfException(PdfException.FilterCcittfaxdecodeIsOnlySupportedForImages); } int width = wn.IntValue(); int height = hn.IntValue(); PdfDictionary param = decodeParams is PdfDictionary ? (PdfDictionary)decodeParams : null; int k = 0; bool blackIs1 = false; bool byteAlign = false; if (param != null) { PdfNumber kn = param.GetAsNumber(PdfName.K); if (kn != null) { k = kn.IntValue(); } PdfBoolean bo = param.GetAsBoolean(PdfName.BlackIs1); if (bo != null) { blackIs1 = bo.GetValue(); } bo = param.GetAsBoolean(PdfName.EncodedByteAlign); if (bo != null) { byteAlign = bo.GetValue(); } } byte[] outBuf = new byte[(width + 7) / 8 * height]; TIFFFaxDecompressor decoder = new TIFFFaxDecompressor(); if (k == 0 || k > 0) { int tiffT4Options = k > 0 ? TIFFConstants.GROUP3OPT_2DENCODING : 0; tiffT4Options |= byteAlign ? TIFFConstants.GROUP3OPT_FILLBITS : 0; decoder.SetOptions(1, TIFFConstants.COMPRESSION_CCITTFAX3, tiffT4Options, 0); decoder.DecodeRaw(outBuf, b, width, height); if (decoder.fails > 0) { byte[] outBuf2 = new byte[(width + 7) / 8 * height]; int oldFails = decoder.fails; decoder.SetOptions(1, TIFFConstants.COMPRESSION_CCITTRLE, tiffT4Options, 0); decoder.DecodeRaw(outBuf2, b, width, height); if (decoder.fails < oldFails) { outBuf = outBuf2; } } } else { TIFFFaxDecoder deca = new TIFFFaxDecoder(1, width, height); deca.DecodeT6(outBuf, b, 0, height, 0); } if (!blackIs1) { int len = outBuf.Length; for (int t = 0; t < len; ++t) { outBuf[t] ^= 0xff; } } b = outBuf; return(b); }