Пример #1
0
        // methods

        public override void ToPdf(PdfWriter writer, Stream os)
        {
            int n = writer.GetNewObjectNumber(reader, number, generation);

            byte[] b = PdfEncodings.ConvertToBytes(new StringBuilder().Append(n).Append(" 0 R").ToString(), null);
            os.Write(b, 0, b.Length);
        }
Пример #2
0
 internal override byte[] ConvertToBytes(int char1)
 {
     if (cjkMirror != null)
     {
         return(PdfEncodings.ConvertToBytes((char)char1, CJKFont.CJK_ENCODING));
     }
     else if (isType0)
     {
         int[] ws = (int[])metrics[(int)char1];
         if (ws != null)
         {
             int g = ws[0];
             return(new byte[] { (byte)(g / 256), (byte)(g) });
         }
         else
         {
             return(new byte[0]);
         }
     }
     else
     {
         if (uni2byte.ContainsKey(char1))
         {
             return new byte[] { (byte)uni2byte[char1] }
         }
         ;
         else
         {
             return(new byte[0]);
         }
     }
 }
Пример #3
0
        /** Creates a JavaScript action. If the JavaScript is smaller than
         * 50 characters it will be placed as a string, otherwise it will
         * be placed as a compressed stream.
         * @param code the JavaScript code
         * @param writer the writer for this action
         * @param unicode select JavaScript unicode. Note that the internal
         * Acrobat JavaScript engine does not support unicode,
         * so this may or may not work for you
         * @return the JavaScript action
         */
        public static PdfAction JavaScript(string code, PdfWriter writer, bool unicode)
        {
            PdfAction js = new PdfAction();

            js.Put(PdfName.S, PdfName.JAVASCRIPT);
            if (unicode && code.Length < 50)
            {
                js.Put(PdfName.JS, new PdfString(code, PdfObject.TEXT_UNICODE));
            }
            else if (!unicode && code.Length < 100)
            {
                js.Put(PdfName.JS, new PdfString(code));
            }
            else
            {
                try {
                    byte[]    b      = PdfEncodings.ConvertToBytes(code, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING);
                    PdfStream stream = new PdfStream(b);
                    stream.FlateCompress(writer.CompressionLevel);
                    js.Put(PdfName.JS, writer.AddToBody(stream).IndirectReference);
                }
                catch {
                    js.Put(PdfName.JS, new PdfString(code));
                }
            }
            return(js);
        }
Пример #4
0
 public byte[] GetOriginalBytes()
 {
     if (originalValue == null)
     {
         return(GetBytes());
     }
     return(PdfEncodings.ConvertToBytes(originalValue, null));
 }
Пример #5
0
 internal override byte[] ConvertToBytes(String text)
 {
     if (cjkMirror != null)
     {
         return(PdfEncodings.ConvertToBytes(text, CJKFont.CJK_ENCODING));
     }
     else if (isType0)
     {
         char[] chars = text.ToCharArray();
         int    len   = chars.Length;
         byte[] b     = new byte[len * 2];
         int    bptr  = 0;
         for (int k = 0; k < len; ++k)
         {
             int[] ws = (int[])metrics[(int)chars[k]];
             if (ws != null)
             {
                 int g = ws[0];
                 b[bptr++] = (byte)(g / 256);
                 b[bptr++] = (byte)(g);
             }
         }
         if (bptr == b.Length)
         {
             return(b);
         }
         else
         {
             byte[] nb = new byte[bptr];
             Array.Copy(b, 0, nb, 0, bptr);
             return(nb);
         }
     }
     else
     {
         char[] cc  = text.ToCharArray();
         byte[] b   = new byte[cc.Length];
         int    ptr = 0;
         for (int k = 0; k < cc.Length; ++k)
         {
             if (uni2byte.ContainsKey(cc[k]))
             {
                 b[ptr++] = (byte)uni2byte[cc[k]];
             }
         }
         if (ptr == b.Length)
         {
             return(b);
         }
         else
         {
             byte[] b2 = new byte[ptr];
             Array.Copy(b, 0, b2, 0, ptr);
             return(b2);
         }
     }
 }
Пример #6
0
        internal void Decrypt(PdfReader reader)
        {
            PdfEncryption decrypt = reader.Decrypt;

            if (decrypt != null)
            {
                originalValue = value;
                decrypt.SetHashKey(objNum, objGen);
                bytes = PdfEncodings.ConvertToBytes(value, null);
                bytes = decrypt.DecryptByteArray(bytes);
                value = PdfEncodings.ConvertToString(bytes, null);
            }
        }
Пример #7
0
        /** Creates a ToUnicode CMap to allow copy and paste from Acrobat.
         * @param metrics metrics[0] contains the glyph index and metrics[2]
         * contains the Unicode code
         * @throws DocumentException on error
         * @return the stream representing this CMap or <CODE>null</CODE>
         */
        private PdfStream GetToUnicode(Object[] metrics)
        {
            if (metrics.Length == 0)
            {
                return(null);
            }
            StringBuilder buf = new StringBuilder(
                "/CIDInit /ProcSet findresource begin\n" +
                "12 dict begin\n" +
                "begincmap\n" +
                "/CIDSystemInfo\n" +
                "<< /Registry (TTX+0)\n" +
                "/Ordering (T42UV)\n" +
                "/Supplement 0\n" +
                ">> def\n" +
                "/CMapName /TTX+0 def\n" +
                "/CMapType 2 def\n" +
                "1 begincodespacerange\n" +
                "<0000><FFFF>\n" +
                "endcodespacerange\n");
            int size = 0;

            for (int k = 0; k < metrics.Length; ++k)
            {
                if (size == 0)
                {
                    if (k != 0)
                    {
                        buf.Append("endbfrange\n");
                    }
                    size = Math.Min(100, metrics.Length - k);
                    buf.Append(size).Append(" beginbfrange\n");
                }
                --size;
                int[]  metric = (int[])metrics[k];
                string fromTo = ToHex(metric[0]);
                buf.Append(fromTo).Append(fromTo).Append(ToHex(metric[2])).Append('\n');
            }
            buf.Append(
                "endbfrange\n" +
                "endcmap\n" +
                "CMapName currentdict /CMap defineresource pop\n" +
                "end end\n");
            string    s      = buf.ToString();
            PdfStream stream = new PdfStream(PdfEncodings.ConvertToBytes(s, null));

            stream.FlateCompress(compressionLevel);
            return(stream);
        }
Пример #8
0
 public override byte[] GetBytes()
 {
     if (bytes == null)
     {
         if (encoding != null && encoding.Equals(TEXT_UNICODE) && PdfEncodings.IsPdfDocEncoding(value))
         {
             bytes = PdfEncodings.ConvertToBytes(value, TEXT_PDFDOCENCODING);
         }
         else
         {
             bytes = PdfEncodings.ConvertToBytes(value, encoding);
         }
     }
     return(bytes);
 }
Пример #9
0
        /** Converts the text into bytes to be placed in the document.
         * The conversion is done according to the font and the encoding and the characters
         * used are stored.
         * @param text the text to convert
         * @return the conversion
         */
        internal byte[] ConvertToBytes(string text)
        {
            byte[] b = null;
            switch (fontType)
            {
            case BaseFont.FONT_TYPE_T3:
                return(baseFont.ConvertToBytes(text));

            case BaseFont.FONT_TYPE_T1:
            case BaseFont.FONT_TYPE_TT: {
                b = baseFont.ConvertToBytes(text);
                int len = b.Length;
                for (int k = 0; k < len; ++k)
                {
                    shortTag[((int)b[k]) & 0xff] = 1;
                }
                break;
            }

            case BaseFont.FONT_TYPE_CJK: {
                int len = text.Length;
                for (int k = 0; k < len; ++k)
                {
                    cjkTag[cjkFont.GetCidCode(text[k])] = 0;
                }
                b = baseFont.ConvertToBytes(text);
                break;
            }

            case BaseFont.FONT_TYPE_DOCUMENT: {
                b = baseFont.ConvertToBytes(text);
                break;
            }

            case BaseFont.FONT_TYPE_TTUNI: {
                int    len     = text.Length;
                int[]  metrics = null;
                char[] glyph   = new char[len];
                int    i       = 0;
                if (symbolic)
                {
                    b   = PdfEncodings.ConvertToBytes(text, "symboltt");
                    len = b.Length;
                    for (int k = 0; k < len; ++k)
                    {
                        metrics = ttu.GetMetricsTT(b[k] & 0xff);
                        if (metrics == null)
                        {
                            continue;
                        }
                        longTag[metrics[0]] = new int[] { metrics[0], metrics[1], ttu.GetUnicodeDifferences(b[k] & 0xff) };
                        glyph[i++]          = (char)metrics[0];
                    }
                }
                else
                {
                    for (int k = 0; k < len; ++k)
                    {
                        int val;
                        if (Utilities.IsSurrogatePair(text, k))
                        {
                            val = Utilities.ConvertToUtf32(text, k);
                            k++;
                        }
                        else
                        {
                            val = (int)text[k];
                        }
                        metrics = ttu.GetMetricsTT(val);
                        if (metrics == null)
                        {
                            continue;
                        }
                        int m0 = metrics[0];
                        int gl = m0;
                        if (!longTag.ContainsKey(gl))
                        {
                            longTag[gl] = new int[] { m0, metrics[1], val }
                        }
                        ;
                        glyph[i++] = (char)m0;
                    }
                }
                string s = new String(glyph, 0, i);
                b = PdfEncodings.ConvertToBytes(s, CJKFont.CJK_ENCODING);
                break;
            }
            }
            return(b);
        }
Пример #10
0
        /** Creates a new Type1 font.
         * @param ttfAfm the AFM file if the input is made with a <CODE>byte</CODE> array
         * @param pfb the PFB file if the input is made with a <CODE>byte</CODE> array
         * @param afmFile the name of one of the 14 built-in fonts or the location of an AFM file. The file must end in '.afm'
         * @param enc the encoding to be applied to this font
         * @param emb true if the font is to be embedded in the PDF
         * @throws DocumentException the AFM file is invalid
         * @throws IOException the AFM file could not be read
         */
        internal Type1Font(string afmFile, string enc, bool emb, byte[] ttfAfm, byte[] pfb, bool forceRead)
        {
            if (emb && ttfAfm != null && pfb == null)
            {
                throw new DocumentException("Two byte arrays are needed if the Type1 font is embedded.");
            }
            if (emb && ttfAfm != null)
            {
                this.pfb = pfb;
            }
            encoding = enc;
            embedded = emb;
            fileName = afmFile;
            FontType = FONT_TYPE_T1;
            RandomAccessFileOrArray rf = null;
            Stream istr = null;

            if (BuiltinFonts14.ContainsKey(afmFile))
            {
                embedded    = false;
                builtinFont = true;
                byte[] buf = new byte[1024];
                try {
                    istr = GetResourceStream(RESOURCE_PATH + afmFile + ".afm");
                    if (istr == null)
                    {
                        Console.Error.WriteLine(afmFile + " not found as resource.");
                        throw new DocumentException(afmFile + " not found as resource.");
                    }
                    MemoryStream ostr = new MemoryStream();
                    while (true)
                    {
                        int size = istr.Read(buf, 0, buf.Length);
                        if (size == 0)
                        {
                            break;
                        }
                        ostr.Write(buf, 0, size);
                    }
                    buf = ostr.ToArray();
                }
                finally {
                    if (istr != null)
                    {
                        try {
                            istr.Close();
                        }
                        catch {
                            // empty on purpose
                        }
                    }
                }
                try {
                    rf = new RandomAccessFileOrArray(buf);
                    Process(rf);
                }
                finally {
                    if (rf != null)
                    {
                        try {
                            rf.Close();
                        }
                        catch {
                            // empty on purpose
                        }
                    }
                }
            }
            else if (afmFile.ToLower(CultureInfo.InvariantCulture).EndsWith(".afm"))
            {
                try {
                    if (ttfAfm == null)
                    {
                        rf = new RandomAccessFileOrArray(afmFile, forceRead);
                    }
                    else
                    {
                        rf = new RandomAccessFileOrArray(ttfAfm);
                    }
                    Process(rf);
                }
                finally {
                    if (rf != null)
                    {
                        try {
                            rf.Close();
                        }
                        catch {
                            // empty on purpose
                        }
                    }
                }
            }
            else if (afmFile.ToLower(CultureInfo.InvariantCulture).EndsWith(".pfm"))
            {
                try {
                    MemoryStream ba = new MemoryStream();
                    if (ttfAfm == null)
                    {
                        rf = new RandomAccessFileOrArray(afmFile, forceRead);
                    }
                    else
                    {
                        rf = new RandomAccessFileOrArray(ttfAfm);
                    }
                    Pfm2afm.Convert(rf, ba);
                    rf.Close();
                    rf = new RandomAccessFileOrArray(ba.ToArray());
                    Process(rf);
                }
                finally {
                    if (rf != null)
                    {
                        try {
                            rf.Close();
                        }
                        catch  {
                            // empty on purpose
                        }
                    }
                }
            }
            else
            {
                throw new DocumentException(afmFile + " is not an AFM or PFM font file.");
            }
            EncodingScheme = EncodingScheme.Trim();
            if (EncodingScheme.Equals("AdobeStandardEncoding") || EncodingScheme.Equals("StandardEncoding"))
            {
                fontSpecific = false;
            }
            if (!encoding.StartsWith("#"))
            {
                PdfEncodings.ConvertToBytes(" ", enc); // check if the encoding exists
            }
            CreateEncoding();
        }
Пример #11
0
 protected void WriteFontString(string s)
 {
     byte[] b = PdfEncodings.ConvertToBytes(s, BaseFont.WINANSI);
     Array.Copy(b, 0, outFont, fontPtr, b.Length);
     fontPtr += b.Length;
 }
Пример #12
0
        /**
         * Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> with a certain <VAR>content</VAR>.
         *
         * @param        type            type of the new <CODE>PdfObject</CODE>
         * @param        content            content of the new <CODE>PdfObject</CODE> as a <CODE>String</CODE>.
         */

        protected PdfObject(int type, string content)
        {
            this.type = type;
            bytes     = PdfEncodings.ConvertToBytes(content, null);
        }