public override bool ContainsGlyph(int unicode)
 {
     if (cidFontType == CID_FONT_TYPE_0)
     {
         if (cmapEncoding.IsDirect())
         {
             return(fontProgram.GetGlyphByCode(unicode) != null);
         }
         else
         {
             return(GetFontProgram().GetGlyph(unicode) != null);
         }
     }
     else
     {
         if (cidFontType == CID_FONT_TYPE_2)
         {
             if (fontProgram.IsFontSpecific())
             {
                 byte[] b = PdfEncodings.ConvertToBytes((char)unicode, "symboltt");
                 return(b.Length > 0 && fontProgram.GetGlyph(b[0] & 0xff) != null);
             }
             else
             {
                 return(GetFontProgram().GetGlyph(unicode) != null);
             }
         }
         else
         {
             throw new PdfException("Invalid CID font type: " + cidFontType);
         }
     }
 }
Example #2
0
        public override byte[] ConvertToBytes(Glyph glyph)
        {
            int code = glyph.GetCode();

            if (longTag.Get(code) == null)
            {
                longTag[code] = new int[] { code, glyph.GetWidth(), glyph.HasValidUnicode() ? glyph.GetUnicode() : 0 };
            }
            return(PdfEncodings.ConvertToBytes(new String(new char[] { (char)glyph.GetCode() }, 0, 1), PdfEncodings.UNICODE_BIG_UNMARKED
                                               ));
        }
Example #3
0
        public virtual void EncodingTest()
        {
            RandomAccessSourceFactory factory;
            PdfTokenizer tok;
            PdfString    pdfString;
            // hex string parse and check
            String testHexString = "<0D0A09557365729073204775696465>";

            factory = new RandomAccessSourceFactory();
            tok     = new PdfTokenizer(new RandomAccessFileOrArray(factory.CreateSource(testHexString.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1
                                                                                                               ))));
            tok.NextToken();
            pdfString = new PdfString(tok.GetByteContent(), tok.IsHexString());
            NUnit.Framework.Assert.AreEqual("\r\n\tUser\u0090s Guide", pdfString.GetValue());
            String testUnicodeString = "ΑΒΓΗ€•♣⋅";

            pdfString = new PdfString(PdfEncodings.ConvertToBytes(testUnicodeString, PdfEncodings.UNICODE_BIG), false);
            NUnit.Framework.Assert.AreEqual(testUnicodeString, pdfString.ToUnicodeString());
            pdfString = new PdfString("FEFF041F04400438043204350442".GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1),
                                      true);
            NUnit.Framework.Assert.AreEqual("\u041F\u0440\u0438\u0432\u0435\u0442", pdfString.ToUnicodeString());
            pdfString = new PdfString("FEFF041F04400438043204350442".GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1),
                                      false);
            NUnit.Framework.Assert.AreEqual("FEFF041F04400438043204350442", pdfString.ToUnicodeString());
            String specialCharacter = "\r\n\t\\n\\r\\t\\f";

            pdfString = new PdfString(specialCharacter.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1), false);
            NUnit.Framework.Assert.AreEqual("\n\t\n\r\t\f", pdfString.ToUnicodeString());
            String symbol = "\u0001\u0004\u0006\u000E\u001F";

            pdfString = new PdfString(symbol.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1), false);
            NUnit.Framework.Assert.AreEqual(symbol, pdfString.ToUnicodeString());
            String testString1 = "These\\\n two\\\r strings\\\n are the same";

            pdfString = new PdfString(testString1.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1), false);
            NUnit.Framework.Assert.AreEqual("These two strings are the same", pdfString.GetValue());
            String testString2 = "This string contains \\245two octal characters\\307";

            pdfString = new PdfString(testString2.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1), false);
            NUnit.Framework.Assert.AreEqual("This string contains \u00A5two octal characters\u00C7", pdfString.GetValue
                                                ());
            String testString3 = "\\0053";

            pdfString = new PdfString(testString3.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1), false);
            NUnit.Framework.Assert.AreEqual("\u00053", pdfString.GetValue());
            String testString4 = "\\053";

            pdfString = new PdfString(testString4.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1), false);
            NUnit.Framework.Assert.AreEqual("+", pdfString.GetValue());
            byte[] b = new byte[] { (byte)46, (byte)56, (byte)40 };
            pdfString = new PdfString(b, false);
            NUnit.Framework.Assert.AreEqual(iText.IO.Util.JavaUtil.GetStringForBytes(b), pdfString.GetValue());
        }
Example #4
0
        /// <summary>Creates a ToUnicode CMap to allow copy and paste from Acrobat.</summary>
        /// <param name="metrics">
        /// metrics[0] contains the glyph index and metrics[2]
        /// contains the Unicode code
        /// </param>
        /// <returns>the stream representing this CMap or <CODE>null</CODE></returns>
        public virtual PdfStream GetToUnicode(Object[] metrics)
        {
            List <int> unicodeGlyphs = new List <int>(metrics.Length);

            for (int i = 0; i < metrics.Length; i++)
            {
                int[] metric = (int[])metrics[i];
                if (fontProgram.GetGlyphByCode(metric[0]).GetChars() != null)
                {
                    unicodeGlyphs.Add(metric[0]);
                }
            }
            if (unicodeGlyphs.Count == 0)
            {
                return(null);
            }
            StringBuilder buf = new StringBuilder("/CIDInit /ProcSet findresource begin\n" + "12 dict begin\n" + "begincmap\n"
                                                  + "/CIDSystemInfo\n" + "<< /Registry (Adobe)\n" + "/Ordering (UCS)\n" + "/Supplement 0\n" + ">> def\n"
                                                  + "/CMapName /Adobe-Identity-UCS def\n" + "/CMapType 2 def\n" + "1 begincodespacerange\n" + "<0000><FFFF>\n"
                                                  + "endcodespacerange\n");
            int size = 0;

            for (int k = 0; k < unicodeGlyphs.Count; ++k)
            {
                if (size == 0)
                {
                    if (k != 0)
                    {
                        buf.Append("endbfrange\n");
                    }
                    size = Math.Min(100, unicodeGlyphs.Count - k);
                    buf.Append(size).Append(" beginbfrange\n");
                }
                --size;
                String fromTo = CMapContentParser.ToHex((int)unicodeGlyphs[k]);
                Glyph  glyph  = fontProgram.GetGlyphByCode((int)unicodeGlyphs[k]);
                if (glyph.GetChars() != null)
                {
                    StringBuilder uni = new StringBuilder(glyph.GetChars().Length);
                    foreach (char ch in glyph.GetChars())
                    {
                        uni.Append(ToHex4(ch));
                    }
                    buf.Append(fromTo).Append(fromTo).Append('<').Append(uni.ToString()).Append('>').Append('\n');
                }
            }
            buf.Append("endbfrange\n" + "endcmap\n" + "CMapName currentdict /CMap defineresource pop\n" + "end end\n");
            PdfStream toUnicode = new PdfStream(PdfEncodings.ConvertToBytes(buf.ToString(), null));

            MakeObjectIndirect(toUnicode);
            return(toUnicode);
        }
Example #5
0
        public override byte[] ConvertToBytes(String text)
        {
            int len = text.Length;

            char[] glyphs = new char[len];
            int    i      = 0;

            if (fontProgram.IsFontSpecific())
            {
                byte[] b = PdfEncodings.ConvertToBytes(text, "symboltt");
                len = b.Length;
                for (int k = 0; k < len; ++k)
                {
                    Glyph glyph = fontProgram.GetGlyph(b[k] & 0xff);
                    if (glyph != null && !longTag.ContainsKey(glyph.GetCode()))
                    {
                        longTag[glyph.GetCode()] = new int[] { glyph.GetCode(), glyph.GetWidth(), glyph.HasValidUnicode() ? glyph.
                                                               GetUnicode() : 0 };
                        glyphs[i++] = (char)cmapEncoding.GetCmapCode(glyph.GetCode());
                    }
                }
            }
            else
            {
                for (int k = 0; k < len; ++k)
                {
                    int val;
                    if (TextUtil.IsSurrogatePair(text, k))
                    {
                        val = TextUtil.ConvertToUtf32(text, k);
                        k++;
                    }
                    else
                    {
                        val = text[k];
                    }
                    Glyph glyph = fontProgram.GetGlyph(val);
                    if (glyph == null)
                    {
                        glyph = fontProgram.GetGlyphByCode(0);
                    }
                    if (!longTag.ContainsKey(glyph.GetCode()))
                    {
                        longTag[glyph.GetCode()] = new int[] { glyph.GetCode(), glyph.GetWidth(), glyph.HasValidUnicode() ? glyph.
                                                               GetUnicode() : 0 };
                    }
                    glyphs[i++] = (char)cmapEncoding.GetCmapCode(glyph.GetCode());
                }
            }
            return(PdfEncodings.ConvertToBytes(new String(glyphs, 0, i), PdfEncodings.UNICODE_BIG_UNMARKED));
        }
Example #6
0
        /// <summary>
        /// Prepares an
        /// <see cref="PdfPKCS7"/>
        /// instance for the given signature.
        /// This method handles signature parsing and might throw an exception if
        /// signature is malformed.
        /// <p>
        /// The returned
        /// <see cref="PdfPKCS7"/>
        /// can be used to fetch additional info about the signature
        /// and also to perform integrity check of data signed by the given signature field.
        /// </p>
        /// Prepared
        /// <see cref="PdfPKCS7"/>
        /// instance calculates digest based on signature's /ByteRange entry.
        /// In order to check that /ByteRange is properly defined and given signature indeed covers the current PDF document
        /// revision please use
        /// <see cref="SignatureCoversWholeDocument(System.String)"/>
        /// method.
        /// </summary>
        /// <param name="signatureFieldName">the signature field name</param>
        /// <param name="securityProvider">the security provider or null for the default provider</param>
        /// <returns>
        /// a
        /// <see cref="PdfPKCS7"/>
        /// instance which can be used to fetch additional info about the signature
        /// and also to perform integrity check of data signed by the given signature field.
        /// </returns>
        public virtual PdfPKCS7 ReadSignatureData(String signatureFieldName)
        {
            PdfSignature signature = GetSignature(signatureFieldName);

            if (signature == null)
            {
                return(null);
            }
            try {
                PdfName   sub      = signature.GetSubFilter();
                PdfString contents = signature.GetContents();
                PdfPKCS7  pk       = null;
                if (sub.Equals(PdfName.Adbe_x509_rsa_sha1))
                {
                    PdfString cert = signature.GetPdfObject().GetAsString(PdfName.Cert);
                    if (cert == null)
                    {
                        cert = signature.GetPdfObject().GetAsArray(PdfName.Cert).GetAsString(0);
                    }
                    pk = new PdfPKCS7(PdfEncodings.ConvertToBytes(contents.GetValue(), null), cert.GetValueBytes());
                }
                else
                {
                    pk = new PdfPKCS7(PdfEncodings.ConvertToBytes(contents.GetValue(), null), sub);
                }
                UpdateByteRange(pk, signature);
                PdfString date = signature.GetDate();
                if (date != null)
                {
                    pk.SetSignDate(PdfDate.Decode(date.ToString()));
                }
                String signName = signature.GetName();
                pk.SetSignName(signName);
                String reason = signature.GetReason();
                if (reason != null)
                {
                    pk.SetReason(reason);
                }
                String location = signature.GetLocation();
                if (location != null)
                {
                    pk.SetLocation(location);
                }
                return(pk);
            }
            catch (Exception e) {
                throw new PdfException(e);
            }
        }
        public override byte[] ConvertToBytes(String text)
        {
            int        len    = text.Length;
            ByteBuffer buffer = new ByteBuffer();
            int        i      = 0;

            if (fontProgram.IsFontSpecific())
            {
                byte[] b = PdfEncodings.ConvertToBytes(text, "symboltt");
                len = b.Length;
                for (int k = 0; k < len; ++k)
                {
                    Glyph glyph = fontProgram.GetGlyph(b[k] & 0xff);
                    if (glyph != null)
                    {
                        ConvertToBytes(glyph, buffer);
                    }
                }
            }
            else
            {
                for (int k = 0; k < len; ++k)
                {
                    int val;
                    if (iText.IO.Util.TextUtil.IsSurrogatePair(text, k))
                    {
                        val = iText.IO.Util.TextUtil.ConvertToUtf32(text, k);
                        k++;
                    }
                    else
                    {
                        val = text[k];
                    }
                    Glyph glyph = GetGlyph(val);
                    if (glyph.GetCode() > 0)
                    {
                        ConvertToBytes(glyph, buffer);
                    }
                    else
                    {
                        //getCode() could be either -1 or 0
                        int nullCode = cmapEncoding.GetCmapCode(0);
                        buffer.Append(nullCode >> 8);
                        buffer.Append(nullCode);
                    }
                }
            }
            return(buffer.ToByteArray());
        }
Example #8
0
 /// <summary>Gets bytes of String-value considering encoding.</summary>
 /// <returns>byte array</returns>
 public virtual byte[] GetValueBytes()
 {
     // Analog of com.itextpdf.text.pdf.PdfString.getBytes() method in iText5.
     if (value == null)
     {
         GenerateValue();
     }
     if (encoding != null && encoding.Equals(PdfEncodings.UNICODE_BIG) && PdfEncodings.IsPdfDocEncoding(value))
     {
         return(PdfEncodings.ConvertToBytes(value, PdfEncodings.PDF_DOC_ENCODING));
     }
     else
     {
         return(PdfEncodings.ConvertToBytes(value, encoding));
     }
 }
Example #9
0
        /// <exception cref="Org.BouncyCastle.Security.SecurityUtilityException"/>
        /// <exception cref="System.IO.IOException"/>
        private PdfName GetSignatureHashKey(String signatureName)
        {
            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 MemoryStream(bc));
                Asn1Object      pkcs = din.ReadObject();
                bc = pkcs.GetEncoded();
            }
            bt = HashBytesSha1(bc);
            return(new PdfName(ConvertToHex(bt)));
        }
Example #10
0
        public override void WriteText(GlyphLine text, int from, int to, PdfOutputStream stream)
        {
            StringBuilder bytes = new StringBuilder();

            for (int i = from; i <= to; i++)
            {
                Glyph glyph = text.Get(i);
                int   code  = glyph.GetCode();
                bytes.Append((char)cmapEncoding.GetCmapCode(glyph.GetCode()));
                if (longTag.Get(code) == null)
                {
                    longTag[code] = new int[] { code, glyph.GetWidth(), glyph.HasValidUnicode() ? glyph.GetUnicode() : 0 };
                }
            }
            //TODO improve converting chars to hexed string
            StreamUtil.WriteHexedString(stream, PdfEncodings.ConvertToBytes(bytes.ToString(), PdfEncodings.UNICODE_BIG_UNMARKED
                                                                            ));
        }
Example #11
0
 public override byte[] ConvertToBytes(GlyphLine glyphLine)
 {
     if (glyphLine != null)
     {
         char[] glyphs = new char[glyphLine.Size()];
         for (int i = 0; i < glyphLine.Size(); i++)
         {
             Glyph glyph = glyphLine.Get(i);
             glyphs[i] = (char)cmapEncoding.GetCmapCode(glyph.GetCode());
             int code = glyph.GetCode();
             if (longTag.Get(code) == null)
             {
                 longTag[code] = new int[] { code, glyph.GetWidth(), glyph.HasValidUnicode() ? glyph.GetUnicode() : 0 };
             }
         }
         return(PdfEncodings.ConvertToBytes(new String(glyphs, 0, glyphs.Length), PdfEncodings.UNICODE_BIG_UNMARKED
                                            ));
     }
     else
     {
         return(null);
     }
 }
Example #12
0
        public override GlyphLine CreateGlyphLine(String content)
        {
            IList <Glyph> glyphs = new List <Glyph>();

            if (cidFontType == CID_FONT_TYPE_0)
            {
                int len = content.Length;
                if (cmapEncoding.IsDirect())
                {
                    for (int k = 0; k < len; ++k)
                    {
                        Glyph glyph = fontProgram.GetGlyphByCode((int)content[k]);
                        if (glyph != null)
                        {
                            glyphs.Add(glyph);
                        }
                    }
                }
                else
                {
                    for (int k = 0; k < len; ++k)
                    {
                        int ch;
                        if (TextUtil.IsSurrogatePair(content, k))
                        {
                            ch = TextUtil.ConvertToUtf32(content, k);
                            k++;
                        }
                        else
                        {
                            ch = content[k];
                        }
                        glyphs.Add(GetGlyph(ch));
                    }
                }
            }
            else
            {
                if (cidFontType == CID_FONT_TYPE_2)
                {
                    TrueTypeFont ttf = (TrueTypeFont)fontProgram;
                    int          len = content.Length;
                    if (ttf.IsFontSpecific())
                    {
                        byte[] b = PdfEncodings.ConvertToBytes(content, "symboltt");
                        len = b.Length;
                        for (int k = 0; k < len; ++k)
                        {
                            Glyph glyph = fontProgram.GetGlyph(b[k] & 0xff);
                            if (glyph != null)
                            {
                                glyphs.Add(glyph);
                            }
                        }
                    }
                    else
                    {
                        for (int k = 0; k < len; ++k)
                        {
                            int val;
                            if (TextUtil.IsSurrogatePair(content, k))
                            {
                                val = TextUtil.ConvertToUtf32(content, k);
                                k++;
                            }
                            else
                            {
                                val = content[k];
                            }
                            glyphs.Add(GetGlyph(val));
                        }
                    }
                }
                else
                {
                    throw new PdfException("font.has.no.suitable.cmap");
                }
            }
            return(new GlyphLine(glyphs));
        }
Example #13
0
 public PdfLiteral(String content)
     : this(PdfEncodings.ConvertToBytes(content, null))
 {
 }
Example #14
0
        /// <summary>Verifies a signature.</summary>
        /// <remarks>
        /// Verifies a signature. Further verification can be done on the returned
        /// <see cref="PdfPKCS7"/>
        /// object.
        /// </remarks>
        /// <param name="name">the signature field name</param>
        /// <param name="provider">the provider or null for the default provider</param>
        /// <returns>PdfPKCS7 object to continue the verification</returns>
        public virtual PdfPKCS7 VerifySignature(String name)
        {
            PdfDictionary v = GetSignatureDictionary(name);

            if (v == null)
            {
                return(null);
            }
            try {
                PdfName   sub      = v.GetAsName(PdfName.SubFilter);
                PdfString contents = v.GetAsString(PdfName.Contents);
                PdfPKCS7  pk       = null;
                if (sub.Equals(PdfName.Adbe_x509_rsa_sha1))
                {
                    PdfString cert = v.GetAsString(PdfName.Cert);
                    if (cert == null)
                    {
                        cert = v.GetAsArray(PdfName.Cert).GetAsString(0);
                    }
                    pk = new PdfPKCS7(PdfEncodings.ConvertToBytes(contents.GetValue(), null), cert.GetValueBytes());
                }
                else
                {
                    pk = new PdfPKCS7(PdfEncodings.ConvertToBytes(contents.GetValue(), null), sub);
                }
                UpdateByteRange(pk, v);
                PdfString str = v.GetAsString(PdfName.M);
                if (str != null)
                {
                    pk.SetSignDate(PdfDate.Decode(str.ToString()));
                }
                PdfObject obj = v.Get(PdfName.Name);
                if (obj != null)
                {
                    if (obj.IsString())
                    {
                        pk.SetSignName(((PdfString)obj).ToUnicodeString());
                    }
                    else
                    {
                        if (obj.IsName())
                        {
                            pk.SetSignName(((PdfName)obj).GetValue());
                        }
                    }
                }
                str = v.GetAsString(PdfName.Reason);
                if (str != null)
                {
                    pk.SetReason(str.ToUnicodeString());
                }
                str = v.GetAsString(PdfName.Location);
                if (str != null)
                {
                    pk.SetLocation(str.ToUnicodeString());
                }
                return(pk);
            }
            catch (Exception e) {
                throw new PdfException(e);
            }
        }
        public override int AppendAnyGlyph(String text, int from, IList <Glyph> glyphs)
        {
            int process = 1;

            if (cidFontType == CID_FONT_TYPE_0)
            {
                if (cmapEncoding.IsDirect())
                {
                    Glyph glyph = fontProgram.GetGlyphByCode((int)text[from]);
                    if (glyph != null)
                    {
                        glyphs.Add(glyph);
                    }
                }
                else
                {
                    int ch;
                    if (iText.IO.Util.TextUtil.IsSurrogatePair(text, from))
                    {
                        ch      = iText.IO.Util.TextUtil.ConvertToUtf32(text, from);
                        process = 2;
                    }
                    else
                    {
                        ch = text[from];
                    }
                    glyphs.Add(GetGlyph(ch));
                }
            }
            else
            {
                if (cidFontType == CID_FONT_TYPE_2)
                {
                    TrueTypeFont ttf = (TrueTypeFont)fontProgram;
                    if (ttf.IsFontSpecific())
                    {
                        byte[] b = PdfEncodings.ConvertToBytes(text, "symboltt");
                        if (b.Length > 0)
                        {
                            Glyph glyph = fontProgram.GetGlyph(b[0] & 0xff);
                            if (glyph != null)
                            {
                                glyphs.Add(glyph);
                            }
                        }
                    }
                    else
                    {
                        int ch;
                        if (iText.IO.Util.TextUtil.IsSurrogatePair(text, from))
                        {
                            ch      = iText.IO.Util.TextUtil.ConvertToUtf32(text, from);
                            process = 2;
                        }
                        else
                        {
                            ch = text[from];
                        }
                        glyphs.Add(GetGlyph(ch));
                    }
                }
                else
                {
                    throw new PdfException("Font has no suitable cmap.");
                }
            }
            return(process);
        }