/** * If the child of a structured element is a dictionary, we inspect the * child; we may also draw a tag. * * @param k * the child dictionary to inspect */ public void InspectChildDictionary(PdfDictionary k) { if (k == null) { return; } PdfName s = k.GetAsName(PdfName.S); if (s != null) { String tagN = PdfName.DecodeName(s.ToString()); String tag = FixTagName(tagN); outp.Write("<"); outp.Write(tag); outp.Write(">"); PdfDictionary dict = k.GetAsDict(PdfName.PG); if (dict != null) { ParseTag(tagN, k.GetDirectObject(PdfName.K), dict); } InspectChild(k.Get(PdfName.K)); outp.Write("</"); outp.Write(tag); outp.WriteLine(">"); } else { InspectChild(k.Get(PdfName.K)); } }
/** * Processes a dictionary. * In case of font dictionaries, the dictionary is processed. */ public void UnembedTTF(PdfDictionary dict) { // we ignore all dictionaries that aren't font dictionaries if (!dict.IsFont()) { return; } // we only remove TTF fonts if (dict.GetAsDict(PdfName.FONTFILE2) != null) { return; } // check if a subset was used (in which case we remove the prefix) PdfName baseFont = dict.GetAsName(PdfName.BASEFONT); if (baseFont.GetBytes()[7] == '+') { baseFont = new PdfName(baseFont.ToString().Substring(8)); dict.Put(PdfName.BASEFONT, baseFont); } // we check if there's a font descriptor PdfDictionary fontDescriptor = dict.GetAsDict(PdfName.FONTDESCRIPTOR); if (fontDescriptor == null) { return; } // is there is, we replace the fontname and remove the font file fontDescriptor.Put(PdfName.FONTNAME, baseFont); fontDescriptor.Remove(PdfName.FONTFILE2); }
public virtual void Copying1() { PdfDocument pdfDoc1 = new PdfDocument(new PdfWriter(destinationFolder + "copying1_1.pdf")); pdfDoc1.GetDocumentInfo().SetAuthor("Alexander Chingarev").SetCreator("iText 6").SetTitle("Empty iText 6 Document" ); pdfDoc1.GetCatalog().Put(new PdfName("a"), new PdfName("b").MakeIndirect(pdfDoc1)); PdfPage page1 = pdfDoc1.AddNewPage(); page1.Flush(); pdfDoc1.Close(); pdfDoc1 = new PdfDocument(new PdfReader(destinationFolder + "copying1_1.pdf")); PdfDocument pdfDoc2 = new PdfDocument(new PdfWriter(destinationFolder + "copying1_2.pdf")); pdfDoc2.AddNewPage(); pdfDoc2.GetDocumentInfo().GetPdfObject().Put(new PdfName("a"), pdfDoc1.GetCatalog().GetPdfObject().Get(new PdfName("a")).CopyTo(pdfDoc2)); pdfDoc2.Close(); pdfDoc1.Close(); PdfReader reader = new PdfReader(destinationFolder + "copying1_2.pdf"); PdfDocument pdfDocument = new PdfDocument(reader); NUnit.Framework.Assert.AreEqual(false, reader.HasRebuiltXref(), "Rebuilt"); PdfDictionary trailer = pdfDocument.GetTrailer(); PdfDictionary info = trailer.GetAsDictionary(PdfName.Info); PdfName b = info.GetAsName(new PdfName("a")); NUnit.Framework.Assert.AreEqual("/b", b.ToString()); pdfDocument.Close(); }
/// <exception cref="System.IO.IOException"/> protected internal virtual void ParseTag(PdfMcr kid) { int mcid = kid.GetMcid(); PdfDictionary pageDic = kid.GetPageObject(); String tagContent = ""; if (mcid != -1) { if (!parsedTags.ContainsKey(pageDic)) { TaggedPdfReaderTool.MarkedContentEventListener listener = new TaggedPdfReaderTool.MarkedContentEventListener (this); PdfCanvasProcessor processor = new PdfCanvasProcessor(listener); PdfPage page = document.GetPage(pageDic); processor.ProcessContent(page.GetContentBytes(), page.GetResources()); parsedTags[pageDic] = listener.GetMcidContent(); } if (parsedTags.Get(pageDic).ContainsKey(mcid)) { tagContent = parsedTags.Get(pageDic).Get(mcid); } } else { PdfObjRef objRef = (PdfObjRef)kid; PdfObject @object = objRef.GetReferencedObject(); if (@object.IsDictionary()) { PdfName subtype = ((PdfDictionary)@object).GetAsName(PdfName.Subtype); tagContent = subtype.ToString(); } } @out.Write(EscapeXML(tagContent, true)); }
protected String XmlName(PdfName name) { String oldName = name.ToString(); String xmlName = oldName.Remove(oldName.IndexOf("/"), 1); xmlName = (xmlName.ToLower()[0]) + xmlName.Substring(1); return(xmlName); }
/** * If the child of a structured element is a dictionary, we inspect the * child; we may also draw a tag. * * @param k * the child dictionary to inspect */ virtual public void InspectChildDictionary(PdfDictionary k, bool inspectAttributes) { if (k == null) { return; } PdfName s = k.GetAsName(PdfName.S); if (s != null) { String tagN = PdfName.DecodeName(s.ToString()); String tag = FixTagName(tagN); outp.Write("<"); outp.Write(tag); if (inspectAttributes) { PdfDictionary a = k.GetAsDict(PdfName.A); if (a != null) { Dictionary <PdfName, PdfObject> .KeyCollection keys = a.Keys; foreach (PdfName key in keys) { outp.Write(' '); PdfObject value = a.Get(key); value = PdfReader.GetPdfObject(value); outp.Write(XmlName(key)); outp.Write("=\""); outp.Write(value.ToString()); outp.Write("\""); } } } outp.Write(">"); PdfObject alt = k.Get(PdfName.ALT); if (alt != null && alt.ToString() != null) { outp.Write("<alt><![CDATA["); outp.Write(Regex.Replace(alt.ToString(), "[\\000]*", "")); outp.Write("]]></alt>"); } PdfDictionary dict = k.GetAsDict(PdfName.PG); if (dict != null) { ParseTag(tagN, k.GetDirectObject(PdfName.K), dict); } InspectChild(k.GetDirectObject(PdfName.K)); outp.Write("</"); outp.Write(tag); outp.WriteLine(">"); } else { InspectChild(k.GetDirectObject(PdfName.K)); } }
private int GetXObjNum(PdfName xobjName) { String decodedPdfName = PdfName.DecodeName(xobjName.ToString()); if (decodedPdfName.LastIndexOf(XOBJ_NAME_PREFIX) == -1) { return(0); } String numStr = decodedPdfName.Substring(decodedPdfName.LastIndexOf(XOBJ_NAME_PREFIX) + XOBJ_NAME_PREFIX.Length); return(Int32.Parse(numStr)); }
public static ViewerPreferences.PaperModeEnum?Get(PdfName code, ViewerPreferences.PaperModeEnum? defaultValue) { if (code == null) { return(defaultValue); } ViewerPreferences.PaperModeEnum?value = codes.GetKey(code); if (!value.HasValue) { throw new ArgumentException(code.ToString()); } return(value.Value); }
private List <PdfObject> Do(PdfLiteral oper, List <PdfObject> operands) { PdfName xobjectName = (PdfName)operands[0]; System.Diagnostics.Debug.WriteLine("O: " + xobjectName.ToString()); PdfDictionary xobjects = _modifier.ResourceDictionary.GetAsDict(PdfName.XOBJECT); PdfObject po = xobjects.Get(xobjectName); int n = (po as PdfIndirectReference).Number; if (_convertedIndirectList.Contains(n)) { return(operands); } else { _convertedIndirectList.Add(n); } PdfObject xobject = xobjects.GetDirectObject(xobjectName); if (xobject.IsStream()) { PdfStream xobjectStream = (PdfStream)xobject; PdfName subType = xobjectStream.GetAsName(PdfName.SUBTYPE); if (subType == PdfName.FORM) { this.Do_Form(xobjectStream); } else if (subType == PdfName.IMAGE) { this.Do_Image(xobjectStream); } } return(operands); }
// See http://stackoverflow.com/questions/4491156/get-the-export-value-of-a-checkbox-using-itextsharp/ public static string GetExportValue(AcroFields.Item item) { PdfDictionary valueDict = item.GetValue(0); PdfDictionary appearanceDict = valueDict.GetAsDict(PdfName.AP); if (appearanceDict != null) { PdfDictionary normalAppearances = appearanceDict.GetAsDict(PdfName.N); // /D is for the "down" appearances. // if there are normal appearances, one key will be "Off", and the other // will be the export value... there should only be two. if (normalAppearances != null) { foreach (PdfName curKey in normalAppearances.Keys) { if (!PdfName.OFF.Equals(curKey)) { return(curKey.ToString().Substring(1)); // string will have a leading '/' character, so remove it! } } } } // if that doesn't work, there might be an /AS key, whose value is a name with // the export value, again with a leading '/', so remove it! PdfName curVal = valueDict.GetAsName(PdfName.AS); if (curVal != null) { return(curVal.ToString().Substring(1)); } else { return(string.Empty); } }
protected override void CheckStructElem(PdfWriter writer, int key, Object obj1) { if (obj1 is PdfStructureElement) { PdfStructureElement structElem = (PdfStructureElement)obj1; PdfName role = structElem.StructureType; if (PdfName.FIGURE.Equals(role) || PdfName.FORMULA.Equals(role) || PdfName.FORM.Equals(role)) { PdfObject o = structElem.Get(PdfName.ALT); if (o is PdfString && o.ToString().Length > 0) { } else { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("alt.entry.should.specify.alternate.description.for.1.element", role.ToString())); } } } }
protected override void CheckAction(PdfWriter writer, int key, Object obj1) { if (obj1 is PdfAction) { PdfAction action = (PdfAction)obj1; PdfName s = action.GetAsName(PdfName.S); if (setState.Equals(s) || noOp.Equals(s)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("deprecated.setstate.and.noop.actions.are.not.allowed")); } if (restrictedActions.Contains(s)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("launch.sound.movie.resetform.importdata.and.javascript.actions.are.not.allowed")); } if (PdfName.NAMED.Equals(s)) { PdfName n = action.GetAsName(PdfName.N); if (n != null && !allowedNamedActions.Contains(n)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("named.action.type.1.not.allowed", n.ToString())); } } } }
protected override void CheckAnnotation(PdfWriter writer, int key, Object obj1) { if (obj1 is PdfFormField) { PdfFormField field = (PdfFormField)obj1; if (!field.Contains(PdfName.SUBTYPE)) { return; } if (field.Contains(PdfName.AA) || field.Contains(PdfName.A)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("widget.annotation.dictionary.or.field.dictionary.shall.not.include.a.or.aa.entry")); } } if (obj1 is PdfAnnotation) { PdfAnnotation annot = (PdfAnnotation)obj1; PdfName subtype = annot.Get(PdfName.SUBTYPE) as PdfName; if (subtype != null && !allowedAnnotTypes.Contains(subtype)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("annotation.type.1.not.allowed", subtype.ToString())); } PdfNumber ca = annot.GetAsNumber(PdfName.CA); if (ca != null && ca.FloatValue != 1.0) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("an.annotation.dictionary.shall.not.contain.the.ca.key.with.a.value.other.than.1")); } PdfNumber f = annot.GetAsNumber(PdfName.F); if (f == null) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("an.annotation.dictionary.shall.contain.the.f.key")); } int flags = f.IntValue; if (CheckFlag(flags, PdfAnnotation.FLAGS_PRINT) == false || CheckFlag(flags, PdfAnnotation.FLAGS_HIDDEN) || CheckFlag(flags, PdfAnnotation.FLAGS_INVISIBLE) || CheckFlag(flags, PdfAnnotation.FLAGS_NOVIEW)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.f.keys.print.flag.bit.shall.be.set.to.1.and.its.hidden.invisible.and.noview.flag.bits.shall.be.set.to.0")); } if (PdfName.TEXT.Equals(annot.GetAsName(PdfName.SUBTYPE))) { if (CheckFlag(flags, PdfAnnotation.FLAGS_NOZOOM) == false || CheckFlag(flags, PdfAnnotation.FLAGS_NOROTATE) == false) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("text.annotations.should.set.the.nozoom.and.norotate.flag.bits.of.the.f.key.to.1")); } } if (annot.Contains(PdfName.C) || annot.Contains(PdfName.IC)) { ICC_Profile colorProfile = ((PdfAWriter)writer).ColorProfile; String cs = ""; cs = System.Text.Encoding.ASCII.GetString(colorProfile.Data, 16, 4); if (!"RGB".Equals(cs.ToUpper())) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("destoutputprofile.in.the.pdfa1.outputintent.dictionary.shall.be.rgb")); } } PdfDictionary ap = GetDirectDictionary(annot.Get(PdfName.AP)); if (ap != null) { if (ap.Contains(PdfName.R) || ap.Contains(PdfName.D)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("appearance.dictionary.shall.contain.only.the.n.key.with.stream.value")); } PdfObject n = ap.Get(PdfName.N); if (!(n is PdfIndirectReference)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("appearance.dictionary.shall.contain.only.the.n.key.with.stream.value")); } } if (PdfName.WIDGET.Equals(annot.GetAsName(PdfName.SUBTYPE)) && (annot.Contains(PdfName.AA) || annot.Contains(PdfName.A))) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("widget.annotation.dictionary.or.field.dictionary.shall.not.include.a.or.aa.entry")); } if (CheckStructure(conformanceLevel)) { if (contentAnnotations.Contains(subtype) && !annot.Contains(PdfName.CONTENTS)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("annotation.of.type.1.should.have.contents.key", subtype.ToString())); } } } }
protected override void CheckGState(PdfWriter writer, int key, Object obj1) { if (obj1 is PdfDictionary) { PdfDictionary gs = (PdfDictionary)obj1; PdfObject obj = gs.Get(PdfName.BM); if (obj != null && !PdfGState.BM_NORMAL.Equals(obj) && !PdfGState.BM_COMPATIBLE.Equals(obj)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("blend.mode.1.not.allowed", obj.ToString())); } obj = gs.Get(PdfName.CA); double v = 0.0; if (obj != null && (v = ((PdfNumber)obj).DoubleValue) != 1.0) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("transparency.is.not.allowed.ca.eq.1", v.ToString())); } obj = gs.Get(PdfName.ca); v = 0.0; if (obj != null && (v = ((PdfNumber)obj).DoubleValue) != 1.0) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("transparency.is.not.allowed.ca.eq.1", v.ToString())); } if (gs.Contains(PdfName.TR)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("an.extgstate.dictionary.shall.not.contain.the.tr.key")); } PdfName tr2 = gs.GetAsName(PdfName.TR2); if (tr2 != null && !tr2.Equals(PdfName.DEFAULT)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage( "an.extgstate.dictionary.shall.not.contain.the.TR2.key.with.a.value.other.than.default")); } PdfName ri = gs.GetAsName(PdfName.RI); if (ri != null && !(PdfName.RELATIVECOLORIMETRIC.Equals(ri) || PdfName.ABSOLUTECOLORIMETRIC.Equals(ri) || PdfName.PERCEPTUAL.Equals(ri) || PdfName.SATURATION.Equals(ri))) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("1.value.of.ri.key.is.not.allowed", ri.ToString())); } if (gs.Get(PdfName.SMASK) != null && !PdfName.NONE.Equals(gs.GetAsName(PdfName.SMASK))) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.smask.key.is.not.allowed.in.extgstate")); } } }
protected override void CheckImage(PdfWriter writer, int key, Object obj1) { PdfImage image = (PdfImage)obj1; if (image.Get(PdfName.SMASK) != null && !PdfName.NONE.Equals(image.GetAsName(PdfName.SMASK))) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.smask.key.is.not.allowed.in.images")); } if (image.Contains(PdfName.ALTERNATES)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("an.image.dictionary.shall.not.contain.alternates.key")); } if (image.Contains(PdfName.OPI)) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("an.image.dictionary.shall.not.contain.opi.key")); } PdfBoolean interpolate = image.GetAsBoolean(PdfName.INTERPOLATE); if (interpolate != null && interpolate.BooleanValue) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.value.of.interpolate.key.shall.not.be.true")); } PdfName intent = image.GetAsName(PdfName.INTENT); if (intent != null && !(PdfName.RELATIVECOLORIMETRIC.Equals(intent) || PdfName.ABSOLUTECOLORIMETRIC.Equals(intent) || PdfName.PERCEPTUAL.Equals(intent) || PdfName.SATURATION.Equals(intent))) { throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("1.value.of.intent.key.is.not.allowed", intent.ToString())); } }
/// <summary> /// Write a <see cref="PdfName"/> to the stream /// </summary> /// <param name="stream">The <see cref="PdfStream"/> to write to.</param> /// <param name="name">The pdf name to write.</param> /// <returns>The <see cref="PdfStream"/> to support chaining operations.</returns> public static PdfStream Write(this PdfStream stream, PdfName name) => stream.Write(name.ToString());
private SignedDocumentInfo CollectInfo(String documentPath) { SignedDocumentInfo docInfo = new SignedDocumentInfo(); PdfDocument pdfDoc = new PdfDocument(new PdfReader(documentPath)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false); SignatureUtil signUtil = new SignatureUtil(pdfDoc); IList <String> names = signUtil.GetSignatureNames(); docInfo.SetNumberOfTotalRevisions(signUtil.GetTotalRevisions()); SignaturePermissions perms = null; IList <SignatureInfo> signInfos = new List <SignatureInfo>(); foreach (String name in names) { SignatureInfo sigInfo = new SignatureInfo(); sigInfo.SetSignatureName(name); sigInfo.SetRevisionNumber(signUtil.GetRevision(name)); sigInfo.SetSignatureCoversWholeDocument(signUtil.SignatureCoversWholeDocument(name)); IList <PdfWidgetAnnotation> widgetAnnotationsList = form.GetField(name).GetWidgets(); if (widgetAnnotationsList != null && widgetAnnotationsList.Count > 0) { sigInfo.SetSignaturePosition(widgetAnnotationsList[0].GetRectangle().ToRectangle()); } PdfPKCS7 pkcs7 = signUtil.ReadSignatureData(name); sigInfo.SetDigestAlgorithm(pkcs7.GetHashAlgorithm()); sigInfo.SetEncryptionAlgorithm(pkcs7.GetEncryptionAlgorithm()); PdfName filterSubtype = pkcs7.GetFilterSubtype(); if (filterSubtype != null) { sigInfo.SetFilterSubtype(filterSubtype.ToString()); } X509Certificate signCert = pkcs7.GetSigningCertificate(); sigInfo.SetSignerName(iText.Signatures.CertificateInfo.GetSubjectFields(signCert).GetField("CN")); sigInfo.SetAlternativeSignerName(pkcs7.GetSignName()); sigInfo.SetSignDate(pkcs7.GetSignDate().ToUniversalTime()); if (TimestampConstants.UNDEFINED_TIMESTAMP_DATE != pkcs7.GetTimeStampDate()) { sigInfo.SetTimeStamp(pkcs7.GetTimeStampDate().ToUniversalTime()); TimeStampToken ts = pkcs7.GetTimeStampToken(); sigInfo.SetTimeStampService(ts.TimeStampInfo.Tsa.ToString()); } sigInfo.SetLocation(pkcs7.GetLocation()); sigInfo.SetReason(pkcs7.GetReason()); PdfDictionary sigDict = signUtil.GetSignatureDictionary(name); PdfString contactInfo = sigDict.GetAsString(PdfName.ContactInfo); if (contactInfo != null) { sigInfo.SetContactInfo(contactInfo.ToString()); } perms = new SignaturePermissions(sigDict, perms); sigInfo.SetIsCertifiaction(perms.IsCertification()); sigInfo.SetIsFieldsFillAllowed(perms.IsFillInAllowed()); sigInfo.SetIsAddingAnnotationsAllowed(perms.IsAnnotationsAllowed()); IList <String> fieldLocks = new List <String>(); foreach (SignaturePermissions.FieldLock Lock in perms.GetFieldLocks()) { fieldLocks.Add(Lock.ToString()); } sigInfo.SetFieldsLocks(fieldLocks); X509Certificate[] certs = pkcs7.GetSignCertificateChain(); IList <CertificateInfo> certInfos = new List <CertificateInfo>(); for (int i = 0; i < certs.Length; i++) { X509Certificate cert = (X509Certificate)certs[i]; CertificateInfo certInfo = new CertificateInfo(); certInfo.SetIssuer(cert.IssuerDN); certInfo.SetSubject(cert.SubjectDN); certInfo.SetValidFrom(cert.NotBefore); certInfo.SetValidTo(cert.NotAfter); certInfos.Add(certInfo); } sigInfo.SetCertificateInfos(certInfos); signInfos.Add(sigInfo); } docInfo.SetSignatureInfos(signInfos); return(docInfo); }
public virtual void SetDefaultColorSpace(PdfName defaultCsKey, PdfColorSpace defaultCsValue) { if (!defaultCsKey.Equals(PdfName.DefaultCMYK) && !defaultCsKey.Equals(PdfName.DefaultGray) && !defaultCsKey .Equals(PdfName.DefaultRGB)) { throw new PdfException(PdfException.UnsupportedDefaultColorSpaceName1).SetMessageParams(defaultCsKey.ToString ()); } AddResource(defaultCsValue.GetPdfObject(), PdfName.ColorSpace, defaultCsKey); }
virtual protected String XmlName(PdfName name) { String oldName = name.ToString(); String xmlName = oldName.Remove(oldName.IndexOf("/"), 1); xmlName = (xmlName.ToLower()[0]) + xmlName.Substring(1); return xmlName; }
/** * <summary>Gets the character map corresponding to the given name.</summary> * <param name="name">Predefined character map name.</param> * <returns>null, in case no name matching occurs.</returns> */ public static CMap Get(PdfName name) { return(Get(name.ToString())); }