コード例 #1
0
 /**
  * This method feeds in the source document
  * @param reader The PDF reader containing the source document
  * @throws DocumentException
  */
 public void CopyDocumentFields(PdfReader reader)
 {
     if (!reader.IsOpenedWithFullPermissions)
     {
         throw new BadPasswordException("PdfReader not opened with owner password");
     }
     if (readers2intrefs.ContainsKey(reader))
     {
         reader = new PdfReader(reader);
     }
     else
     {
         if (reader.Tampered)
         {
             throw new DocumentException("The document was reused.");
         }
         reader.ConsolidateNamedDestinations();
         reader.Tampered = true;
     }
     reader.ShuffleSubsetNames();
     readers2intrefs[reader] = new IntHashtable();
     fields.Add(reader.AcroFields);
     UpdateCalculationOrder(reader);
 }
コード例 #2
0
        internal void AddDocument(PdfReader reader)
        {
            if (!reader.IsOpenedWithFullPermissions)
            {
                throw new BadPasswordException("PdfReader not opened with owner password");
            }
            OpenDoc();
            if (readers2intrefs.ContainsKey(reader))
            {
                reader = new PdfReader(reader);
            }
            else
            {
                if (reader.Tampered)
                {
                    throw new DocumentException("The document was reused.");
                }
                reader.ConsolidateNamedDestinations();
                reader.Tampered = true;
            }
            reader.ShuffleSubsetNames();
            readers2intrefs[reader] = new IntHashtable();
            readers.Add(reader);
            int          len  = reader.NumberOfPages;
            IntHashtable refs = new IntHashtable();

            for (int p = 1; p <= len; ++p)
            {
                refs[reader.GetPageOrigRef(p).Number] = 1;
                reader.ReleasePage(p);
            }
            pages2intrefs[reader] = refs;
            visited[reader]       = new IntHashtable();
            fields.Add(reader.AcroFields);
            UpdateCalculationOrder(reader);
        }
コード例 #3
0
        private void DoType1TT()
        {
            PdfObject enc = PdfReader.GetPdfObject(font.Get(PdfName.ENCODING));

            if (enc == null)
            {
                FillEncoding(null);
            }
            else
            {
                if (enc.IsName())
                {
                    FillEncoding((PdfName)enc);
                }
                else
                {
                    PdfDictionary encDic = (PdfDictionary)enc;
                    enc = PdfReader.GetPdfObject(encDic.Get(PdfName.BASEENCODING));
                    if (enc == null)
                    {
                        FillEncoding(null);
                    }
                    else
                    {
                        FillEncoding((PdfName)enc);
                    }
                    PdfArray diffs = encDic.GetAsArray(PdfName.DIFFERENCES);
                    if (diffs != null)
                    {
                        diffmap = new IntHashtable();
                        int currentNumber = 0;
                        for (int k = 0; k < diffs.Size; ++k)
                        {
                            PdfObject obj = diffs[k];
                            if (obj.IsNumber())
                            {
                                currentNumber = ((PdfNumber)obj).IntValue;
                            }
                            else
                            {
                                int[] c = GlyphList.NameToUnicode(PdfName.DecodeName(((PdfName)obj).ToString()));
                                if (c != null && c.Length > 0)
                                {
                                    uni2byte[c[0]] = currentNumber;
                                    diffmap[c[0]]  = currentNumber;
                                }
                                ++currentNumber;
                            }
                        }
                    }
                }
            }
            PdfArray  newWidths = font.GetAsArray(PdfName.WIDTHS);
            PdfNumber first     = font.GetAsNumber(PdfName.FIRSTCHAR);
            PdfNumber last      = font.GetAsNumber(PdfName.LASTCHAR);

            if (BuiltinFonts14.ContainsKey(fontName))
            {
                BaseFont bf;
                bf = BaseFont.CreateFont(fontName, WINANSI, false);
                int[] e = uni2byte.ToOrderedKeys();
                for (int k = 0; k < e.Length; ++k)
                {
                    int n = uni2byte[e[k]];
                    widths[n] = bf.GetRawWidth(n, GlyphList.UnicodeToName(e[k]));
                }
                if (diffmap != null)   //widths for differences must override existing ones
                {
                    e = diffmap.ToOrderedKeys();
                    for (int k = 0; k < e.Length; ++k)
                    {
                        int n = diffmap[e[k]];
                        widths[n] = bf.GetRawWidth(n, GlyphList.UnicodeToName(e[k]));
                    }
                    diffmap = null;
                }
                Ascender    = bf.GetFontDescriptor(ASCENT, 1000);
                CapHeight   = bf.GetFontDescriptor(CAPHEIGHT, 1000);
                Descender   = bf.GetFontDescriptor(DESCENT, 1000);
                ItalicAngle = bf.GetFontDescriptor(ITALICANGLE, 1000);
                llx         = bf.GetFontDescriptor(BBOXLLX, 1000);
                lly         = bf.GetFontDescriptor(BBOXLLY, 1000);
                urx         = bf.GetFontDescriptor(BBOXURX, 1000);
                ury         = bf.GetFontDescriptor(BBOXURY, 1000);
            }
            if (first != null && last != null && newWidths != null)
            {
                int f = first.IntValue;
                for (int k = 0; k < newWidths.Size; ++k)
                {
                    widths[f + k] = newWidths.GetAsNumber(k).IntValue;
                }
            }
            FillFontDesc(font.GetAsDict(PdfName.FONTDESCRIPTOR));
        }
コード例 #4
0
        private void FillMetrics(byte[] touni, IntHashtable widths, int dw)
        {
            PdfContentParser ps   = new PdfContentParser(new PRTokeniser(touni));
            PdfObject        ob   = null;
            PdfObject        last = null;

            while ((ob = ps.ReadPRObject()) != null)
            {
                if (ob.Type == PdfContentParser.COMMAND_TYPE)
                {
                    if (ob.ToString().Equals("beginbfchar"))
                    {
                        int n = ((PdfNumber)last).IntValue;
                        for (int k = 0; k < n; ++k)
                        {
                            String cid = DecodeString((PdfString)ps.ReadPRObject());
                            String uni = DecodeString((PdfString)ps.ReadPRObject());
                            if (uni.Length == 1)
                            {
                                int cidc = (int)cid[0];
                                int unic = (int)uni[uni.Length - 1];
                                int w    = dw;
                                if (widths.ContainsKey(cidc))
                                {
                                    w = widths[cidc];
                                }
                                metrics[unic] = new int[] { cidc, w };
                            }
                        }
                    }
                    else if (ob.ToString().Equals("beginbfrange"))
                    {
                        int n = ((PdfNumber)last).IntValue;
                        for (int k = 0; k < n; ++k)
                        {
                            String    cid1  = DecodeString((PdfString)ps.ReadPRObject());
                            String    cid2  = DecodeString((PdfString)ps.ReadPRObject());
                            int       cid1c = (int)cid1[0];
                            int       cid2c = (int)cid2[0];
                            PdfObject ob2   = ps.ReadPRObject();
                            if (ob2.IsString())
                            {
                                String uni = DecodeString((PdfString)ob2);
                                if (uni.Length == 1)
                                {
                                    int unic = (int)uni[uni.Length - 1];
                                    for (; cid1c <= cid2c; cid1c++, unic++)
                                    {
                                        int w = dw;
                                        if (widths.ContainsKey(cid1c))
                                        {
                                            w = widths[cid1c];
                                        }
                                        metrics[unic] = new int[] { cid1c, w };
                                    }
                                }
                            }
                            else
                            {
                                PdfArray a = (PdfArray)ob2;
                                for (int j = 0; j < a.Size; ++j, ++cid1c)
                                {
                                    String uni = DecodeString(a.GetAsString(j));
                                    if (uni.Length == 1)
                                    {
                                        int unic = (int)uni[uni.Length - 1];
                                        int w    = dw;
                                        if (widths.ContainsKey(cid1c))
                                        {
                                            w = widths[cid1c];
                                        }
                                        metrics[unic] = new int[] { cid1c, w };
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    last = ob;
                }
            }
        }
コード例 #5
0
        protected internal bool IsVisited(PdfReader reader, int number, int generation)
        {
            IntHashtable refs = (IntHashtable)readers2intrefs[reader];

            return(refs.ContainsKey(number));
        }
コード例 #6
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);
        }
コード例 #7
0
        internal static string ConvertToVCIDMetrics(int[] keys, IntHashtable v, IntHashtable h)
        {
            if (keys.Length == 0)
            {
                return(null);
            }
            int lastCid    = 0;
            int lastValue  = 0;
            int lastHValue = 0;
            int start;

            for (start = 0; start < keys.Length; ++start)
            {
                lastCid   = keys[start];
                lastValue = v[lastCid];
                if (lastValue != 0)
                {
                    ++start;
                    break;
                }
                else
                {
                    lastHValue = h[lastCid];
                }
            }
            if (lastValue == 0)
            {
                return(null);
            }
            if (lastHValue == 0)
            {
                lastHValue = 1000;
            }
            StringBuilder buf = new StringBuilder();

            buf.Append('[');
            buf.Append(lastCid);
            int state = FIRST;

            for (int k = start; k < keys.Length; ++k)
            {
                int cid   = keys[k];
                int value = v[cid];
                if (value == 0)
                {
                    continue;
                }
                int hValue = h[lastCid];
                if (hValue == 0)
                {
                    hValue = 1000;
                }
                switch (state)
                {
                case FIRST: {
                    if (cid == lastCid + 1 && value == lastValue && hValue == lastHValue)
                    {
                        state = SERIAL;
                    }
                    else
                    {
                        buf.Append(' ').Append(lastCid).Append(' ').Append(-lastValue).Append(' ').Append(lastHValue / 2).Append(' ').Append(V1Y).Append(' ').Append(cid);
                    }
                    break;
                }

                case SERIAL: {
                    if (cid != lastCid + 1 || value != lastValue || hValue != lastHValue)
                    {
                        buf.Append(' ').Append(lastCid).Append(' ').Append(-lastValue).Append(' ').Append(lastHValue / 2).Append(' ').Append(V1Y).Append(' ').Append(cid);
                        state = FIRST;
                    }
                    break;
                }
                }
                lastValue  = value;
                lastCid    = cid;
                lastHValue = hValue;
            }
            buf.Append(' ').Append(lastCid).Append(' ').Append(-lastValue).Append(' ').Append(lastHValue / 2).Append(' ').Append(V1Y).Append(" ]");
            return(buf.ToString());
        }
コード例 #8
0
        /** Creates a CJK font.
         * @param fontName the name of the font
         * @param enc the encoding of the font
         * @param emb always <CODE>false</CODE>. CJK font and not embedded
         * @throws DocumentException on error
         * @throws IOException on error
         */
        internal CJKFont(string fontName, string enc, bool emb)
        {
            LoadProperties();
            this.FontType = FONT_TYPE_CJK;
            string nameBase = GetBaseName(fontName);

            if (!IsCJKFont(nameBase, enc))
            {
                throw new DocumentException("Font '" + fontName + "' with '" + enc + "' encoding is not a CJK font.");
            }
            if (nameBase.Length < fontName.Length)
            {
                style    = fontName.Substring(nameBase.Length);
                fontName = nameBase;
            }
            this.fontName = fontName;
            encoding      = CJK_ENCODING;
            vertical      = enc.EndsWith("V");
            CMap          = enc;
            if (enc.StartsWith("Identity-"))
            {
                cidDirect = true;
                string s = cjkFonts[fontName];
                s = s.Substring(0, s.IndexOf('_'));
                char[] c = (char[])allCMaps[s];
                if (c == null)
                {
                    c = ReadCMap(s);
                    if (c == null)
                    {
                        throw new DocumentException("The cmap " + s + " does not exist as a resource.");
                    }
                    c[CID_NEWLINE] = '\n';
                    allCMaps.Add(s, c);
                }
                translationMap = c;
            }
            else
            {
                char[] c = (char[])allCMaps[enc];
                if (c == null)
                {
                    string s = cjkEncodings[enc];
                    if (s == null)
                    {
                        throw new DocumentException("The resource cjkencodings.properties does not contain the encoding " + enc);
                    }
                    StringTokenizer tk = new StringTokenizer(s);
                    string          nt = tk.NextToken();
                    c = (char[])allCMaps[nt];
                    if (c == null)
                    {
                        c = ReadCMap(nt);
                        allCMaps.Add(nt, c);
                    }
                    if (tk.HasMoreTokens())
                    {
                        string nt2 = tk.NextToken();
                        char[] m2  = ReadCMap(nt2);
                        for (int k = 0; k < 0x10000; ++k)
                        {
                            if (m2[k] == 0)
                            {
                                m2[k] = c[k];
                            }
                        }
                        allCMaps.Add(enc, m2);
                        c = m2;
                    }
                }
                translationMap = c;
            }
            fontDesc = (Hashtable)allFonts[fontName];
            if (fontDesc == null)
            {
                fontDesc = ReadFontProperties(fontName);
                allFonts.Add(fontName, fontDesc);
            }
            hMetrics = (IntHashtable)fontDesc["W"];
            vMetrics = (IntHashtable)fontDesc["W2"];
        }
コード例 #9
0
        /** Converts a <CODE>String</CODE> to a </CODE>byte</CODE> array according
         * to the font's encoding.
         * @return an array of <CODE>byte</CODE> representing the conversion according to the font's encoding
         * @param encoding the encoding
         * @param char1 the <CODE>char</CODE> to be converted
         */
        public static byte[] ConvertToBytes(char char1, String encoding)
        {
            if (encoding == null || encoding.Length == 0)
            {
                return new byte[] { (byte)char1 }
            }
            ;
            IExtraEncoding extra = (IExtraEncoding)extraEncodings[encoding.ToLower(System.Globalization.CultureInfo.InvariantCulture)];

            if (extra != null)
            {
                byte[] b = extra.CharToByte(char1, encoding);

                if (b != null)
                {
                    return(b);
                }
            }
            IntHashtable hash = null;

            if (encoding.Equals(BaseFont.WINANSI))
            {
                hash = winansi;
            }
            else if (encoding.Equals(PdfObject.TEXT_PDFDOCENCODING))
            {
                hash = pdfEncoding;
            }
            if (hash != null)
            {
                int c = 0;
                if (char1 < 128 || (char1 > 160 && char1 <= 255))
                {
                    c = char1;
                }
                else
                {
                    c = hash[char1];
                }
                if (c != 0)
                {
                    return new byte[] { (byte)c }
                }
                ;
                else
                {
                    return(new byte[0]);
                }
            }
            Encoding encw = IanaEncodings.GetEncodingEncoding(encoding);

            byte[] preamble = encw.GetPreamble();
            char[] text     = new char[] { char1 };
            if (preamble.Length == 0)
            {
                return(encw.GetBytes(text));
            }
            byte[] encoded = encw.GetBytes(text);
            byte[] total   = new byte[encoded.Length + preamble.Length];
            Array.Copy(preamble, 0, total, 0, preamble.Length);
            Array.Copy(encoded, 0, total, preamble.Length, encoded.Length);
            return(total);
        }
コード例 #10
0
        /**
         * Converts a <CODE>string</CODE> to a </CODE>byte</CODE> array according
         * to the font's encoding.
         * @param text the <CODE>string</CODE> to be converted
         * @return an array of <CODE>byte</CODE> representing the conversion according to the font's encoding
         */
        public static byte[] ConvertToBytes(string text, string encoding)
        {
            if (text == null)
            {
                return(new byte[0]);
            }
            if (encoding == null || encoding.Length == 0)
            {
                int    len = text.Length;
                byte[] b   = new byte[len];
                for (int k = 0; k < len; ++k)
                {
                    b[k] = (byte)text[k];
                }
                return(b);
            }
            IExtraEncoding extra = (IExtraEncoding)extraEncodings[encoding.ToLower(System.Globalization.CultureInfo.InvariantCulture)];

            if (extra != null)
            {
                byte[] b = extra.CharToByte(text, encoding);
                if (b != null)
                {
                    return(b);
                }
            }
            IntHashtable hash = null;

            if (encoding.Equals(BaseFont.CP1252))
            {
                hash = winansi;
            }
            else if (encoding.Equals(PdfObject.TEXT_PDFDOCENCODING))
            {
                hash = pdfEncoding;
            }
            if (hash != null)
            {
                char[] cc  = text.ToCharArray();
                int    len = cc.Length;
                int    ptr = 0;
                byte[] b   = new byte[len];
                int    c   = 0;
                for (int k = 0; k < len; ++k)
                {
                    char char1 = cc[k];
                    if (char1 < 128 || (char1 > 160 && char1 <= 255))
                    {
                        c = char1;
                    }
                    else
                    {
                        c = hash[char1];
                    }
                    if (c != 0)
                    {
                        b[ptr++] = (byte)c;
                    }
                }
                if (ptr == len)
                {
                    return(b);
                }
                byte[] b2 = new byte[ptr];
                Array.Copy(b, 0, b2, 0, ptr);
                return(b2);
            }
            Encoding encw = IanaEncodings.GetEncodingEncoding(encoding);

            byte[] preamble = encw.GetPreamble();
            if (preamble.Length == 0)
            {
                return(encw.GetBytes(text));
            }
            byte[] encoded = encw.GetBytes(text);
            byte[] total   = new byte[encoded.Length + preamble.Length];
            Array.Copy(preamble, 0, total, 0, preamble.Length);
            Array.Copy(encoded, 0, total, preamble.Length, encoded.Length);
            return(total);
        }