Пример #1
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();
        }
Пример #2
0
        private static ArrayList BookmarkDepth(PdfReader reader, PdfDictionary outline, IntHashtable pages)
        {
            ArrayList list = new ArrayList();

            while (outline != null)
            {
                Hashtable map   = new Hashtable();
                PdfString title = (PdfString)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.TITLE));
                map["Title"] = title.ToUnicodeString();
                PdfArray color = (PdfArray)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.C));
                if (color != null && color.Size == 3)
                {
                    ByteBuffer outp = new ByteBuffer();
                    outp.Append(color.GetAsNumber(0).FloatValue).Append(' ');
                    outp.Append(color.GetAsNumber(1).FloatValue).Append(' ');
                    outp.Append(color.GetAsNumber(2).FloatValue);
                    map["Color"] = PdfEncodings.ConvertToString(outp.ToByteArray(), null);
                }
                PdfNumber style = (PdfNumber)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.F));
                if (style != null)
                {
                    int    f = style.IntValue;
                    String s = "";
                    if ((f & 1) != 0)
                    {
                        s += "italic ";
                    }
                    if ((f & 2) != 0)
                    {
                        s += "bold ";
                    }
                    s = s.Trim();
                    if (s.Length != 0)
                    {
                        map["Style"] = s;
                    }
                }
                PdfNumber count = (PdfNumber)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.COUNT));
                if (count != null && count.IntValue < 0)
                {
                    map["Open"] = "false";
                }
                try {
                    PdfObject dest = PdfReader.GetPdfObjectRelease(outline.Get(PdfName.DEST));
                    if (dest != null)
                    {
                        MapGotoBookmark(map, dest, pages); //changed by ujihara 2004-06-13
                    }
                    else
                    {
                        PdfDictionary action = (PdfDictionary)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.A));
                        if (action != null)
                        {
                            if (PdfName.GOTO.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S))))
                            {
                                dest = PdfReader.GetPdfObjectRelease(action.Get(PdfName.D));
                                if (dest != null)
                                {
                                    MapGotoBookmark(map, dest, pages);
                                }
                            }
                            else if (PdfName.URI.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S))))
                            {
                                map["Action"] = "URI";
                                map["URI"]    = ((PdfString)PdfReader.GetPdfObjectRelease(action.Get(PdfName.URI))).ToUnicodeString();
                            }
                            else if (PdfName.GOTOR.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S))))
                            {
                                dest = PdfReader.GetPdfObjectRelease(action.Get(PdfName.D));
                                if (dest != null)
                                {
                                    if (dest.IsString())
                                    {
                                        map["Named"] = dest.ToString();
                                    }
                                    else if (dest.IsName())
                                    {
                                        map["NamedN"] = PdfName.DecodeName(dest.ToString());
                                    }
                                    else if (dest.IsArray())
                                    {
                                        PdfArray      arr = (PdfArray)dest;
                                        StringBuilder s   = new StringBuilder();
                                        s.Append(arr[0].ToString());
                                        s.Append(' ').Append(arr[1].ToString());
                                        for (int k = 2; k < arr.Size; ++k)
                                        {
                                            s.Append(' ').Append(arr[k].ToString());
                                        }
                                        map["Page"] = s.ToString();
                                    }
                                }
                                map["Action"] = "GoToR";
                                PdfObject file = PdfReader.GetPdfObjectRelease(action.Get(PdfName.F));
                                if (file != null)
                                {
                                    if (file.IsString())
                                    {
                                        map["File"] = ((PdfString)file).ToUnicodeString();
                                    }
                                    else if (file.IsDictionary())
                                    {
                                        file = PdfReader.GetPdfObject(((PdfDictionary)file).Get(PdfName.F));
                                        if (file.IsString())
                                        {
                                            map["File"] = ((PdfString)file).ToUnicodeString();
                                        }
                                    }
                                }
                                PdfObject newWindow = PdfReader.GetPdfObjectRelease(action.Get(PdfName.NEWWINDOW));
                                if (newWindow != null)
                                {
                                    map["NewWindow"] = newWindow.ToString();
                                }
                            }
                            else if (PdfName.LAUNCH.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S))))
                            {
                                map["Action"] = "Launch";
                                PdfObject file = PdfReader.GetPdfObjectRelease(action.Get(PdfName.F));
                                if (file == null)
                                {
                                    file = PdfReader.GetPdfObjectRelease(action.Get(PdfName.WIN));
                                }
                                if (file != null)
                                {
                                    if (file.IsString())
                                    {
                                        map["File"] = ((PdfString)file).ToUnicodeString();
                                    }
                                    else if (file.IsDictionary())
                                    {
                                        file = PdfReader.GetPdfObjectRelease(((PdfDictionary)file).Get(PdfName.F));
                                        if (file.IsString())
                                        {
                                            map["File"] = ((PdfString)file).ToUnicodeString();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch  {
                    //empty on purpose
                }
                PdfDictionary first = (PdfDictionary)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.FIRST));
                if (first != null)
                {
                    map["Kids"] = BookmarkDepth(reader, first, pages);
                }
                list.Add(map);
                outline = (PdfDictionary)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.NEXT));
            }
            return(list);
        }
Пример #3
0
        /**
         * Constructs a <CODE>PdfString</CODE>-object.
         *
         * @param        bytes    an array of <CODE>byte</CODE>
         */

        public PdfString(byte[] bytes) : base(STRING)
        {
            value    = PdfEncodings.ConvertToString(bytes, null);
            encoding = NOTHING;
        }
Пример #4
0
 /**
  * Adds JavaScript to the global document
  * @param js the JavaScript
  */
 public void AddJavaScript(String js)
 {
     fc.AddJavaScript(js, !PdfEncodings.IsPdfDocEncoding(js));
 }
Пример #5
0
 protected void WriteFontString(string s)
 {
     byte[] b = PdfEncodings.ConvertToBytes(s, BaseFont.WINANSI);
     Array.Copy(b, 0, outFont, fontPtr, b.Length);
     fontPtr += b.Length;
 }
Пример #6
0
        /**
         * Outputs a <CODE>double</CODE> into a format suitable for the PDF.
         * @param d a double
         * @param buf a ByteBuffer
         * @return the <CODE>String</CODE> representation of the <CODE>double</CODE> if
         * <CODE>buf</CODE> is <CODE>null</CODE>. If <CODE>buf</CODE> is <B>not</B> <CODE>null</CODE>,
         * then the double is appended directly to the buffer and this methods returns <CODE>null</CODE>.
         */
        public static string FormatDouble(double d, ByteBuffer buf)
        {
            if (HIGH_PRECISION)
            {
                String sform = d.ToString("0.######", CultureInfo.InvariantCulture);
                if (buf == null)
                {
                    return(sform);
                }
                else
                {
                    buf.Append(sform);
                    return(null);
                }
            }
            bool negative = false;

            if (Math.Abs(d) < 0.000015)
            {
                if (buf != null)
                {
                    buf.Append(ZERO);
                    return(null);
                }
                else
                {
                    return("0");
                }
            }
            if (d < 0)
            {
                negative = true;
                d        = -d;
            }
            if (d < 1.0)
            {
                d += 0.000005;
                if (d >= 1)
                {
                    if (negative)
                    {
                        if (buf != null)
                        {
                            buf.Append((byte)'-');
                            buf.Append((byte)'1');
                            return(null);
                        }
                        else
                        {
                            return("-1");
                        }
                    }
                    else
                    {
                        if (buf != null)
                        {
                            buf.Append((byte)'1');
                            return(null);
                        }
                        else
                        {
                            return("1");
                        }
                    }
                }
                if (buf != null)
                {
                    int v = (int)(d * 100000);

                    if (negative)
                    {
                        buf.Append((byte)'-');
                    }
                    buf.Append((byte)'0');
                    buf.Append((byte)'.');

                    buf.Append((byte)(v / 10000 + ZERO));
                    if (v % 10000 != 0)
                    {
                        buf.Append((byte)((v / 1000) % 10 + ZERO));
                        if (v % 1000 != 0)
                        {
                            buf.Append((byte)((v / 100) % 10 + ZERO));
                            if (v % 100 != 0)
                            {
                                buf.Append((byte)((v / 10) % 10 + ZERO));
                                if (v % 10 != 0)
                                {
                                    buf.Append((byte)((v) % 10 + ZERO));
                                }
                            }
                        }
                    }
                    return(null);
                }
                else
                {
                    int x = 100000;
                    int v = (int)(d * x);

                    StringBuilder res = new StringBuilder();
                    if (negative)
                    {
                        res.Append('-');
                    }
                    res.Append("0.");

                    while (v < x / 10)
                    {
                        res.Append('0');
                        x /= 10;
                    }
                    res.Append(v);
                    int cut = res.Length - 1;
                    while (res[cut] == '0')
                    {
                        --cut;
                    }
                    res.Length = cut + 1;
                    return(res.ToString());
                }
            }
            else if (d <= 32767)
            {
                d += 0.005;
                int v = (int)(d * 100);

                if (v < byteCacheSize && byteCache[v] != null)
                {
                    if (buf != null)
                    {
                        if (negative)
                        {
                            buf.Append((byte)'-');
                        }
                        buf.Append(byteCache[v]);
                        return(null);
                    }
                    else
                    {
                        string tmp = PdfEncodings.ConvertToString(byteCache[v], null);
                        if (negative)
                        {
                            tmp = "-" + tmp;
                        }
                        return(tmp);
                    }
                }
                if (buf != null)
                {
                    if (v < byteCacheSize)
                    {
                        //create the cachebyte[]
                        byte[] cache;
                        int    size = 0;
                        if (v >= 1000000)
                        {
                            //the original number is >=10000, we need 5 more bytes
                            size += 5;
                        }
                        else if (v >= 100000)
                        {
                            //the original number is >=1000, we need 4 more bytes
                            size += 4;
                        }
                        else if (v >= 10000)
                        {
                            //the original number is >=100, we need 3 more bytes
                            size += 3;
                        }
                        else if (v >= 1000)
                        {
                            //the original number is >=10, we need 2 more bytes
                            size += 2;
                        }
                        else if (v >= 100)
                        {
                            //the original number is >=1, we need 1 more bytes
                            size += 1;
                        }

                        //now we must check if we have a decimal number
                        if (v % 100 != 0)
                        {
                            //yes, do not forget the "."
                            size += 2;
                        }
                        if (v % 10 != 0)
                        {
                            size++;
                        }
                        cache = new byte[size];
                        int add = 0;
                        if (v >= 1000000)
                        {
                            cache[add++] = bytes[(v / 1000000)];
                        }
                        if (v >= 100000)
                        {
                            cache[add++] = bytes[(v / 100000) % 10];
                        }
                        if (v >= 10000)
                        {
                            cache[add++] = bytes[(v / 10000) % 10];
                        }
                        if (v >= 1000)
                        {
                            cache[add++] = bytes[(v / 1000) % 10];
                        }
                        if (v >= 100)
                        {
                            cache[add++] = bytes[(v / 100) % 10];
                        }

                        if (v % 100 != 0)
                        {
                            cache[add++] = (byte)'.';
                            cache[add++] = bytes[(v / 10) % 10];
                            if (v % 10 != 0)
                            {
                                cache[add++] = bytes[v % 10];
                            }
                        }
                        byteCache[v] = cache;
                    }

                    if (negative)
                    {
                        buf.Append((byte)'-');
                    }
                    if (v >= 1000000)
                    {
                        buf.Append(bytes[(v / 1000000)]);
                    }
                    if (v >= 100000)
                    {
                        buf.Append(bytes[(v / 100000) % 10]);
                    }
                    if (v >= 10000)
                    {
                        buf.Append(bytes[(v / 10000) % 10]);
                    }
                    if (v >= 1000)
                    {
                        buf.Append(bytes[(v / 1000) % 10]);
                    }
                    if (v >= 100)
                    {
                        buf.Append(bytes[(v / 100) % 10]);
                    }

                    if (v % 100 != 0)
                    {
                        buf.Append((byte)'.');
                        buf.Append(bytes[(v / 10) % 10]);
                        if (v % 10 != 0)
                        {
                            buf.Append(bytes[v % 10]);
                        }
                    }
                    return(null);
                }
                else
                {
                    StringBuilder res = new StringBuilder();
                    if (negative)
                    {
                        res.Append('-');
                    }
                    if (v >= 1000000)
                    {
                        res.Append(chars[(v / 1000000)]);
                    }
                    if (v >= 100000)
                    {
                        res.Append(chars[(v / 100000) % 10]);
                    }
                    if (v >= 10000)
                    {
                        res.Append(chars[(v / 10000) % 10]);
                    }
                    if (v >= 1000)
                    {
                        res.Append(chars[(v / 1000) % 10]);
                    }
                    if (v >= 100)
                    {
                        res.Append(chars[(v / 100) % 10]);
                    }

                    if (v % 100 != 0)
                    {
                        res.Append('.');
                        res.Append(chars[(v / 10) % 10]);
                        if (v % 10 != 0)
                        {
                            res.Append(chars[v % 10]);
                        }
                    }
                    return(res.ToString());
                }
            }
            else
            {
                StringBuilder res = new StringBuilder();
                if (negative)
                {
                    res.Append('-');
                }
                d += 0.5;
                long v = (long)d;
                return(res.Append(v).ToString());
            }
        }
Пример #7
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);
        }