/** * Simple method that compares two given PdfStrings. * * @param outString string to compare. * @param cmpString string to compare. * @return true if strings are equal, otherwise false. */ public boolean compareStrings(PdfString outString, PdfString cmpString) { return cmpString.getValue().equals(outString.getValue()); }
if (cert == null) cert = signature.getPdfObject().getAsArray(PdfName.Cert).getAsString(0); pk = new PdfPKCS7(PdfEncodings.convertToBytes(contents.getValue(), null), cert.getValueBytes(), provider); } else pk = new PdfPKCS7(PdfEncodings.convertToBytes(contents.getValue(), null), sub, provider); updateByteRange(pk, signature); PdfString date = signature.getDate();
if (cert == null) cert = signature.getPdfObject().getAsArray(PdfName.Cert).getAsString(0); pk = new PdfPKCS7(PdfEncodings.convertToBytes(contents.getValue(), null), cert.getValueBytes(), provider); } else pk = new PdfPKCS7(PdfEncodings.convertToBytes(contents.getValue(), null), sub, provider); updateByteRange(pk, signature); PdfString date = signature.getDate();
@Override public String toString() { if (value == null) { return new String(decodeContent()); } else { return getValue(); } }
@Override public int hashCode() { String v = getValue(); String e = getEncoding(); int result = v != null ? v.hashCode() : 0; return 31 * result + (e != null ? e.hashCode() : 0); }
/** * Gets bytes of String-value without considering encoding. * * @return byte array */ protected byte[] getIsoBytes(PdfString string) { return ByteUtils.getIsoBytes(string.getValue()); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PdfString that = (PdfString) o; String v1 = getValue(); String v2 = that.getValue(); if (v1 != null && v1.equals(v2)) { String e1 = getEncoding(); String e2 = that.getEncoding(); if ((e1 == null && e2 == null) || (e1 != null && e1.equals(e2))) { return true; } } return false; }
/** * Gives the W3C format of the PdfDate. * @return a formatted date */ public String getW3CDate() { return getW3CDate(getPdfObject().getValue()); }
public boolean add(byte[] text) { return add(new PdfString(text).getValue()); }
/** * Split PDF string into array of single character PDF strings. * * @param string PDF string to be split. * @return split PDF string. */ private PdfString[] splitString(PdfString string) { checkGraphicsState(); PdfFont font = gs.getFont(); if (font instanceof PdfType0Font) { // Number of bytes forming one glyph can be arbitrary from [1; 4] range List<PdfString> strings = new ArrayList<>(); GlyphLine glyphLine = gs.getFont().decodeIntoGlyphLine(string); for (int i = glyphLine.start; i < glyphLine.end; i++) { strings.add(new PdfString(gs.getFont().convertToBytes(glyphLine.get(i)))); } return strings.toArray(new PdfString[strings.size()]); } else { // One byte corresponds to one character PdfString[] strings = new PdfString[string.getValue().length()]; for (int i = 0; i < string.getValue().length(); i++) { strings[i] = new PdfString(string.getValue().substring(i, i + 1), string.getEncoding()); } return strings; } }
protected void inspectKid(IStructureNode kid) { try { if (kid instanceof PdfStructElem) { PdfStructElem structElemKid = (PdfStructElem) kid; PdfName s = structElemKid.getRole(); String tagN = s.getValue(); String tag = fixTagName(tagN); out.write("<"); out.write(tag); inspectAttributes(structElemKid); out.write(">" + System.lineSeparator()); PdfString alt = (structElemKid).getAlt(); if (alt != null) { out.write("<alt><![CDATA["); out.write(alt.getValue().replaceAll("[\\000]*", "")); out.write("]]></alt>" + System.lineSeparator()); } inspectKids(structElemKid.getKids()); out.write("</"); out.write(tag); out.write(">" + System.lineSeparator()); } else if (kid instanceof PdfMcr) { parseTag((PdfMcr) kid); } else { out.write(" <flushedKid/> "); } } catch (java.io.IOException e) { throw new com.itextpdf.io.IOException(com.itextpdf.io.IOException.UnknownIOException, e); } }
private byte[] convertPdfStringToBytes(PdfString pdfString) { byte[] bytes; String value = pdfString.getValue(); String encoding = pdfString.getEncoding(); if (encoding != null && encoding.equals(PdfEncodings.UNICODE_BIG) && PdfEncodings.isPdfDocEncoding(value)) bytes = PdfEncodings.convertToBytes(value, PdfEncodings.PDF_DOC_ENCODING); else bytes = PdfEncodings.convertToBytes(value, encoding); return bytes; }
/** * Calculates width and word spacing of a single character PDF string. * IMPORTANT: Shall ONLY be used for a single character pdf strings. * * @param string a character to calculate width. * @return array of 2 items: first item is a character width, second item is a calculated word spacing. */ private float[] getWidthAndWordSpacing(PdfString string) { checkGraphicsState(); float[] result = new float[2]; result[0] = (float) ((gs.getFont().getContentWidth(string) * fontMatrix[0])); result[1] = " ".equals(string.getValue()) ? gs.getWordSpacing() : 0; return result; }
/** * Gets file ID, either {@link PdfName#ID} key of trailer or a newly generated id. * * @return byte array represents file ID. * @see PdfEncryption#generateNewDocumentId() */ public byte[] getOriginalFileId() { PdfArray id = trailer.getAsArray(PdfName.ID); if (id != null) { return ByteUtils.getIsoBytes(id.getAsString(0).getValue()); } else { return PdfEncryption.generateNewDocumentId(); } }
/** * Provides detail useful if a listener needs access to the position of each individual glyph in the text render operation * * @return A list of {@link TextRenderInfo} objects that represent each glyph used in the draw operation. The next effect is if there was a separate Tj opertion for each character in the rendered string */ public List<TextRenderInfo> getCharacterRenderInfos() { checkGraphicsState(); List<TextRenderInfo> rslt = new ArrayList<>(string.getValue().length()); PdfString[] strings = splitString(string); float totalWidth = 0; for (PdfString str : strings) { float[] widthAndWordSpacing = getWidthAndWordSpacing(str); TextRenderInfo subInfo = new TextRenderInfo(this, str, totalWidth); rslt.add(subInfo); totalWidth += (widthAndWordSpacing[0] * gs.getFontSize() + gs.getCharSpacing() + widthAndWordSpacing[1]) * (gs.getHorizontalScaling() / 100f); } for (TextRenderInfo tri : rslt) tri.getUnscaledWidth(); return rslt; }
/** * Returns the Unicode {@code String} value of this * {@code PdfString}-object. */ public String toUnicodeString() { if (encoding != null && encoding.length() != 0) { return getValue(); } if (content == null) { generateContent(); } byte[] b = decodeContent(); if (b.length >= 2 && b[0] == (byte) 0xFE && b[1] == (byte) 0xFF) { return PdfEncodings.convertToString(b, PdfEncodings.UNICODE_BIG); } else if (b.length >= 3 && b[0] == (byte)0xEF && b[1] == (byte)0xBB && b[2] == (byte)0xBF) { return PdfEncodings.convertToString(b, PdfEncodings.UTF8); } else { return PdfEncodings.convertToString(b, PdfEncodings.PDF_DOC_ENCODING); } }
private PdfName getSignatureHashKey(String signatureName) throws NoSuchAlgorithmException, IOException { PdfSignature sig = sgnUtil.getSignature(signatureName); PdfString contents = sig.getContents(); byte[] bc = PdfEncodings.convertToBytes(contents.getValue(), null); byte[] bt = null; if (PdfName.ETSI_RFC3161.equals(sig.getSubFilter())) { ASN1InputStream din = new ASN1InputStream(new ByteArrayInputStream(bc)); ASN1Primitive pkcs = din.readObject(); bc = pkcs.getEncoded(); } bt = hashBytesSha1(bc); return new PdfName(convertToHex(bt)); }
private PdfName getSignatureHashKey(String signatureName) throws NoSuchAlgorithmException, IOException { PdfSignature sig = sgnUtil.getSignature(signatureName); PdfString contents = sig.getContents(); byte[] bc = PdfEncodings.convertToBytes(contents.getValue(), null); byte[] bt = null; if (PdfName.ETSI_RFC3161.equals(sig.getSubFilter())) { ASN1InputStream din = new ASN1InputStream(new ByteArrayInputStream(bc)); ASN1Primitive pkcs = din.readObject(); bc = pkcs.getEncoded(); } bt = hashBytesSha1(bc); return new PdfName(convertToHex(bt)); }
private void fillFontDescriptor(PdfDictionary fontDesc) { if (fontDesc == null) { return; } PdfNumber v = fontDesc.getAsNumber(PdfName.ItalicAngle); if (v != null) { setItalicAngle(v.intValue()); } v = fontDesc.getAsNumber(PdfName.FontWeight); if (v != null) { setFontWeight(v.intValue()); } PdfName fontStretch = fontDesc.getAsName(PdfName.FontStretch); if (fontStretch != null) { setFontStretch(fontStretch.getValue()); } PdfName fontName = fontDesc.getAsName(PdfName.FontName); if (fontName != null) { setFontName(fontName.getValue()); } PdfString fontFamily = fontDesc.getAsString(PdfName.FontFamily); if (fontFamily != null) { setFontFamily(fontFamily.getValue()); } }