GetAsNumber() публичный Метод

public GetAsNumber ( PdfName key ) : PdfNumber
key PdfName
Результат PdfNumber
Пример #1
0
        private void FillFontDesc(PdfDictionary fontDesc)
        {
            if (fontDesc == null)
            {
                return;
            }
            PdfNumber v = fontDesc.GetAsNumber(PdfName.ASCENT);

            if (v != null)
            {
                Ascender = v.FloatValue;
            }
            v = fontDesc.GetAsNumber(PdfName.CAPHEIGHT);
            if (v != null)
            {
                CapHeight = v.FloatValue;
            }
            v = fontDesc.GetAsNumber(PdfName.DESCENT);
            if (v != null)
            {
                Descender = v.FloatValue;
            }
            v = fontDesc.GetAsNumber(PdfName.ITALICANGLE);
            if (v != null)
            {
                ItalicAngle = v.FloatValue;
            }
            PdfArray bbox = fontDesc.GetAsArray(PdfName.FONTBBOX);

            if (bbox != null)
            {
                llx = bbox.GetAsNumber(0).FloatValue;
                lly = bbox.GetAsNumber(1).FloatValue;
                urx = bbox.GetAsNumber(2).FloatValue;
                ury = bbox.GetAsNumber(3).FloatValue;
                if (llx > urx)
                {
                    float t = llx;
                    llx = urx;
                    urx = t;
                }
                if (lly > ury)
                {
                    float t = lly;
                    lly = ury;
                    ury = t;
                }
            }
        }
        private void fillFontDesc(PdfDictionary fontDesc)
        {
            if (fontDesc == null)
            {
                return;
            }
            PdfNumber v = fontDesc.GetAsNumber(PdfName.Ascent);

            if (v != null)
            {
                _ascender = v.FloatValue;
            }
            v = fontDesc.GetAsNumber(PdfName.Capheight);
            if (v != null)
            {
                _capHeight = v.FloatValue;
            }
            v = fontDesc.GetAsNumber(PdfName.Descent);
            if (v != null)
            {
                _descender = v.FloatValue;
            }
            v = fontDesc.GetAsNumber(PdfName.Italicangle);
            if (v != null)
            {
                _italicAngle = v.FloatValue;
            }
            PdfArray bbox = fontDesc.GetAsArray(PdfName.Fontbbox);

            if (bbox != null)
            {
                _llx = bbox.GetAsNumber(0).FloatValue;
                _lly = bbox.GetAsNumber(1).FloatValue;
                _urx = bbox.GetAsNumber(2).FloatValue;
                _ury = bbox.GetAsNumber(3).FloatValue;
                if (_llx > _urx)
                {
                    float t = _llx;
                    _llx = _urx;
                    _urx = t;
                }
                if (_lly > _ury)
                {
                    float t = _lly;
                    _lly = _ury;
                    _ury = t;
                }
            }
        }
Пример #3
0
        private void FillWidths()
        {
            PdfArray  newWidths = font.GetAsArray(PdfName.WIDTHS);
            PdfNumber first     = font.GetAsNumber(PdfName.FIRSTCHAR);
            PdfNumber last      = font.GetAsNumber(PdfName.LASTCHAR);

            if (first != null && last != null && newWidths != null)
            {
                int f     = first.IntValue;
                int nSize = f + newWidths.Size;
                if (widths.Length < nSize)
                {
                    int[] tmp = new int[nSize];
                    System.Array.Copy(widths, 0, tmp, 0, f);
                    widths = tmp;
                }
                for (int k = 0; k < newWidths.Size; ++k)
                {
                    widths[f + k] = newWidths.GetAsNumber(k).IntValue;
                }
            }
        }
Пример #4
0
        bool IsSymbolic()
        {
            PdfDictionary fontDescriptor = font.GetAsDict(PdfName.FONTDESCRIPTOR);

            if (fontDescriptor == null)
            {
                return(false);
            }
            PdfNumber flags = fontDescriptor.GetAsNumber(PdfName.FLAGS);

            if (flags == null)
            {
                return(false);
            }
            return((flags.IntValue & 0x04) != 0);
        }
Пример #5
0
 private void FillFontDesc(PdfDictionary fontDesc) {
     if (fontDesc == null)
         return;
     PdfNumber v = fontDesc.GetAsNumber(PdfName.ASCENT);
     if (v != null)
         Ascender = v.FloatValue;
     v = fontDesc.GetAsNumber(PdfName.CAPHEIGHT);
     if (v != null)
         CapHeight = v.FloatValue;
     v = fontDesc.GetAsNumber(PdfName.DESCENT);
     if (v != null)
         Descender = v.FloatValue;
     v = fontDesc.GetAsNumber(PdfName.ITALICANGLE);
     if (v != null)
         ItalicAngle = v.FloatValue;
     PdfArray bbox = fontDesc.GetAsArray(PdfName.FONTBBOX);
     if (bbox != null) {
         llx = bbox.GetAsNumber(0).FloatValue;
         lly = bbox.GetAsNumber(1).FloatValue;
         urx = bbox.GetAsNumber(2).FloatValue;
         ury = bbox.GetAsNumber(3).FloatValue;
         if (llx > urx) {
             float t = llx;
             llx = urx;
             urx = t;
         }
         if (lly > ury) {
             float t = lly;
             lly = ury;
             ury = t;
         }
     }
 }
 public void DecodeGenericDictionary(PdfDictionary merged, BaseField tx)
 {
     int flags = 0;
     // the text size and color
     PdfString da = merged.GetAsString(PdfName.DA);
     if (da != null) {
         Object[] dab = SplitDAelements(da.ToUnicodeString());
         if (dab[DA_SIZE] != null)
             tx.FontSize = (float)dab[DA_SIZE];
         if (dab[DA_COLOR] != null)
             tx.TextColor = (Color)dab[DA_COLOR];
         if (dab[DA_FONT] != null) {
             PdfDictionary font = merged.GetAsDict(PdfName.DR);
             if (font != null) {
                 font = font.GetAsDict(PdfName.FONT);
                 if (font != null) {
                     PdfObject po = font.Get(new PdfName((String)dab[DA_FONT]));
                     if (po != null && po.Type == PdfObject.INDIRECT) {
                         PRIndirectReference por = (PRIndirectReference)po;
                         BaseFont bp = new DocumentFont((PRIndirectReference)po);
                         tx.Font = bp;
                         int porkey = por.Number;
                         BaseFont porf = (BaseFont)extensionFonts[porkey];
                         if (porf == null) {
                             if (!extensionFonts.ContainsKey(porkey)) {
                                 PdfDictionary fo = (PdfDictionary)PdfReader.GetPdfObject(po);
                                 PdfDictionary fd = fo.GetAsDict(PdfName.FONTDESCRIPTOR);
                                 if (fd != null) {
                                     PRStream prs = (PRStream)PdfReader.GetPdfObject(fd.Get(PdfName.FONTFILE2));
                                     if (prs == null)
                                         prs = (PRStream)PdfReader.GetPdfObject(fd.Get(PdfName.FONTFILE3));
                                     if (prs == null) {
                                         extensionFonts[porkey] = null;
                                     }
                                     else {
                                         try {
                                             porf = BaseFont.CreateFont("font.ttf", BaseFont.IDENTITY_H, true, false, PdfReader.GetStreamBytes(prs), null);
                                         }
                                         catch {
                                         }
                                         extensionFonts[porkey] = porf;
                                     }
                                 }
                             }
                         }
                         if (tx is TextField)
                             ((TextField)tx).ExtensionFont = porf;
                     }
                     else {
                         BaseFont bf = (BaseFont)localFonts[dab[DA_FONT]];
                         if (bf == null) {
                             String[] fn = (String[])stdFieldFontNames[dab[DA_FONT]];
                             if (fn != null) {
                                 try {
                                     String enc = "winansi";
                                     if (fn.Length > 1)
                                         enc = fn[1];
                                     bf = BaseFont.CreateFont(fn[0], enc, false);
                                     tx.Font = bf;
                                 }
                                 catch {
                                     // empty
                                 }
                             }
                         }
                         else
                             tx.Font = bf;
                     }
                 }
             }
         }
     }
     //rotation, border and backgound color
     PdfDictionary mk = merged.GetAsDict(PdfName.MK);
     if (mk != null) {
         PdfArray ar = mk.GetAsArray(PdfName.BC);
         Color border = GetMKColor(ar);
         tx.BorderColor = border;
         if (border != null)
             tx.BorderWidth = 1;
         ar = mk.GetAsArray(PdfName.BG);
         tx.BackgroundColor = GetMKColor(ar);
         PdfNumber rotation = mk.GetAsNumber(PdfName.R);
         if (rotation != null)
             tx.Rotation = rotation.IntValue;
     }
     //flags
     PdfNumber nfl = merged.GetAsNumber(PdfName.F);
     flags = 0;
     tx.Visibility = BaseField.VISIBLE_BUT_DOES_NOT_PRINT;
     if (nfl != null) {
         flags = nfl.IntValue;
         if ((flags & PdfFormField.FLAGS_PRINT) != 0 && (flags & PdfFormField.FLAGS_HIDDEN) != 0)
             tx.Visibility = BaseField.HIDDEN;
         else if ((flags & PdfFormField.FLAGS_PRINT) != 0 && (flags & PdfFormField.FLAGS_NOVIEW) != 0)
             tx.Visibility = BaseField.HIDDEN_BUT_PRINTABLE;
         else if ((flags & PdfFormField.FLAGS_PRINT) != 0)
             tx.Visibility = BaseField.VISIBLE;
     }
     //multiline
     nfl = merged.GetAsNumber(PdfName.FF);
     flags = 0;
     if (nfl != null)
         flags = nfl.IntValue;
     tx.Options = flags;
     if ((flags & PdfFormField.FF_COMB) != 0) {
         PdfNumber maxLen = merged.GetAsNumber(PdfName.MAXLEN);
         int len = 0;
         if (maxLen != null)
             len = maxLen.IntValue;
         tx.MaxCharacterLength = len;
     }
     //alignment
     nfl = merged.GetAsNumber(PdfName.Q);
     if (nfl != null) {
         if (nfl.IntValue == PdfFormField.Q_CENTER)
             tx.Alignment = Element.ALIGN_CENTER;
         else if (nfl.IntValue == PdfFormField.Q_RIGHT)
             tx.Alignment = Element.ALIGN_RIGHT;
     }
     //border styles
     PdfDictionary bs = merged.GetAsDict(PdfName.BS);
     if (bs != null) {
         PdfNumber w = bs.GetAsNumber(PdfName.W);
         if (w != null)
             tx.BorderWidth = w.FloatValue;
         PdfName s = bs.GetAsName(PdfName.S);
         if (PdfName.D.Equals(s))
             tx.BorderStyle = PdfBorderDictionary.STYLE_DASHED;
         else if (PdfName.B.Equals(s))
             tx.BorderStyle = PdfBorderDictionary.STYLE_BEVELED;
         else if (PdfName.I.Equals(s))
             tx.BorderStyle = PdfBorderDictionary.STYLE_INSET;
         else if (PdfName.U.Equals(s))
             tx.BorderStyle = PdfBorderDictionary.STYLE_UNDERLINE;
     }
     else {
         PdfArray bd = merged.GetAsArray(PdfName.BORDER);
         if (bd != null) {
             if (bd.Size >= 3)
                 tx.BorderWidth = bd.GetAsNumber(2).FloatValue;
             if (bd.Size >= 4)
                 tx.BorderStyle = PdfBorderDictionary.STYLE_DASHED;
         }
     }
 }
 internal PdfAppearance GetAppearance(PdfDictionary merged, String text, String fieldName)
 {
     topFirst = 0;
     TextField tx = null;
     if (fieldCache == null || !fieldCache.ContainsKey(fieldName)) {
         tx = new TextField(writer, null, null);
         tx.SetExtraMargin(extraMarginLeft, extraMarginTop);
         tx.BorderWidth = 0;
         tx.SubstitutionFonts = substitutionFonts;
         DecodeGenericDictionary(merged, tx);
         //rect
         PdfArray rect = merged.GetAsArray(PdfName.RECT);
         Rectangle box = PdfReader.GetNormalizedRectangle(rect);
         if (tx.Rotation == 90 || tx.Rotation == 270)
             box = box.Rotate();
         tx.Box = box;
         if (fieldCache != null)
             fieldCache[fieldName] = tx;
     }
     else {
         tx = (TextField)fieldCache[fieldName];
         tx.Writer = writer;
     }
     PdfName fieldType = merged.GetAsName(PdfName.FT);
     if (PdfName.TX.Equals(fieldType)) {
         tx.Text = text;
         return tx.GetAppearance();
     }
     if (!PdfName.CH.Equals(fieldType))
         throw new DocumentException("An appearance was requested without a variable text field.");
     PdfArray opt = merged.GetAsArray(PdfName.OPT);
     int flags = 0;
     PdfNumber nfl = merged.GetAsNumber(PdfName.FF);
     if (nfl != null)
         flags = nfl.IntValue;
     if ((flags & PdfFormField.FF_COMBO) != 0 && opt == null) {
         tx.Text = text;
         return tx.GetAppearance();
     }
     if (opt != null) {
         String[] choices = new String[opt.Size];
         String[] choicesExp = new String[opt.Size];
         for (int k = 0; k < opt.Size; ++k) {
             PdfObject obj = opt[k];
             if (obj.IsString()) {
                     choices[k] = choicesExp[k] = ((PdfString)obj).ToUnicodeString();
             }
             else {
                 PdfArray a = (PdfArray) obj;
                 choicesExp[k] = a.GetAsString(0).ToUnicodeString();
                 choices[k] = a.GetAsString(1).ToUnicodeString();
             }
         }
         if ((flags & PdfFormField.FF_COMBO) != 0) {
             for (int k = 0; k < choices.Length; ++k) {
                 if (text.Equals(choicesExp[k])) {
                     text = choices[k];
                     break;
                 }
             }
             tx.Text = text;
             return tx.GetAppearance();
         }
         int idx = 0;
         for (int k = 0; k < choicesExp.Length; ++k) {
             if (text.Equals(choicesExp[k])) {
                 idx = k;
                 break;
             }
         }
         tx.Choices = choices;
         tx.ChoiceExports = choicesExp;
         tx.ChoiceSelection = idx;
     }
     PdfAppearance app = tx.GetListAppearance();
     topFirst = tx.TopFirst;
     return app;
 }
Пример #8
0
 internal int GetPageRotation(PdfDictionary page) {
     PdfNumber rotate = page.GetAsNumber(PdfName.ROTATE);
     if (rotate == null)
         return 0;
     else {
         int n = rotate.IntValue;
         n %= 360;
         return n < 0 ? n + 360 : n;
     }
 }
Пример #9
0
 private void FillFontDesc(PdfDictionary fontDesc) {
     if (fontDesc == null)
         return;
     PdfNumber v = fontDesc.GetAsNumber(PdfName.ASCENT);
     if (v != null)
         Ascender = v.FloatValue;
     v = fontDesc.GetAsNumber(PdfName.CAPHEIGHT);
     if (v != null)
         CapHeight = v.FloatValue;
     v = fontDesc.GetAsNumber(PdfName.DESCENT);
     if (v != null)
         Descender = v.FloatValue;
     v = fontDesc.GetAsNumber(PdfName.ITALICANGLE);
     if (v != null)
         ItalicAngle = v.FloatValue;
     v = fontDesc.GetAsNumber(PdfName.FONTWEIGHT);
     if (v != null) {
         fontWeight = v.FloatValue;
     }
     PdfArray bbox = fontDesc.GetAsArray(PdfName.FONTBBOX);
     if (bbox != null) {
         llx = bbox.GetAsNumber(0).FloatValue;
         lly = bbox.GetAsNumber(1).FloatValue;
         urx = bbox.GetAsNumber(2).FloatValue;
         ury = bbox.GetAsNumber(3).FloatValue;
         if (llx > urx) {
             float t = llx;
             llx = urx;
             urx = t;
         }
         if (lly > ury) {
             float t = lly;
             lly = ury;
             ury = t;
         }
     }
     float maxAscent = Math.Max(ury, Ascender);
     float minDescent = Math.Min(lly, Descender);
     Ascender = maxAscent * 1000 / (maxAscent - minDescent);
     Descender = minDescent * 1000 / (maxAscent - minDescent);
 }
Пример #10
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));
        }
        /**
         * Parses the samples of the image from the underlying content parser, ignoring all filters.
         * The parser must be positioned immediately after the ID operator that ends the inline image's dictionary.
         * The parser will be left positioned immediately following the EI operator.
         * This is primarily useful if no filters have been applied.
         * @param imageDictionary the dictionary of the inline image
         * @param ps the content parser
         * @return the samples of the image
         * @throws IOException if anything bad happens during parsing
         */
        private static byte[] ParseUnfilteredSamples(PdfDictionary imageDictionary, PdfDictionary colorSpaceDic, PdfContentParser ps)
        {
            // special case:  when no filter is specified, we just read the number of bits
            // per component, multiplied by the width and height.
            if (imageDictionary.Contains(PdfName.FILTER))
                throw new ArgumentException("Dictionary contains filters");

            PdfNumber h = imageDictionary.GetAsNumber(PdfName.HEIGHT);

            int bytesToRead = ComputeBytesPerRow(imageDictionary, colorSpaceDic) * h.IntValue;
            byte[] bytes = new byte[bytesToRead];
            PRTokeniser tokeniser = ps.GetTokeniser();

            int shouldBeWhiteSpace = tokeniser.Read(); // skip next character (which better be a whitespace character - I suppose we could check for this)
            // from the PDF spec:  Unless the image uses ASCIIHexDecode or ASCII85Decode as one of its filters, the ID operator shall be followed by a single white-space character, and the next character shall be interpreted as the first byte of image data.
            // unfortunately, we've seen some PDFs where there is no space following the ID, so we have to capture this case and handle it
            int startIndex = 0;
            if (!PRTokeniser.IsWhitespace(shouldBeWhiteSpace) || shouldBeWhiteSpace == 0){ // tokeniser treats 0 as whitespace, but for our purposes, we shouldn't)
                bytes[0] = (byte)shouldBeWhiteSpace;
                startIndex++;
            }
            for (int i = startIndex; i < bytesToRead; i++){
                int ch = tokeniser.Read();
                if (ch == -1)
                    throw new InlineImageParseException("End of content stream reached before end of image data");

                bytes[i] = (byte)ch;
            }
            PdfObject ei = ps.ReadPRObject();
            if (!ei.ToString().Equals("EI"))
                throw new InlineImageParseException("EI not found after end of image data");

            return bytes;
        }
        /**
         * Computes the number of unfiltered bytes that each row of the image will contain.
         * If the number of bytes results in a partial terminating byte, this number is rounded up
         * per the PDF specification
         * @param imageDictionary the dictionary of the inline image
         * @return the number of bytes per row of the image
         */
        private static int ComputeBytesPerRow(PdfDictionary imageDictionary, PdfDictionary colorSpaceDic)
        {
            PdfNumber wObj = imageDictionary.GetAsNumber(PdfName.WIDTH);
            PdfNumber bpcObj = imageDictionary.GetAsNumber(PdfName.BITSPERCOMPONENT);
            int cpp = GetComponentsPerPixel(imageDictionary.GetAsName(PdfName.COLORSPACE), colorSpaceDic);

            int w = wObj.IntValue;
            int bpc = bpcObj != null ? bpcObj.IntValue : 1;

            int bytesPerRow = (w * bpc * cpp + 7) / 8;

            return bytesPerRow;
        }
Пример #13
0
            public byte[] Decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDictionary streamDictionary)
            {
                PdfNumber wn = (PdfNumber)PdfReader.GetPdfObjectRelease(streamDictionary.Get(PdfName.WIDTH));
                PdfNumber hn = (PdfNumber)PdfReader.GetPdfObjectRelease(streamDictionary.Get(PdfName.HEIGHT));

                if (wn == null || hn == null)
                {
                    throw new UnsupportedPdfException(MessageLocalization.GetComposedMessage("filter.ccittfaxdecode.is.only.supported.for.images"));
                }
                int width  = wn.IntValue;
                int height = hn.IntValue;

                PdfDictionary param     = decodeParams is PdfDictionary ? (PdfDictionary)decodeParams : null;
                int           k         = 0;
                bool          blackIs1  = false;
                bool          byteAlign = false;

                if (param != null)
                {
                    PdfNumber kn = param.GetAsNumber(PdfName.K);
                    if (kn != null)
                    {
                        k = kn.IntValue;
                    }
                    PdfBoolean bo = param.GetAsBoolean(PdfName.BLACKIS1);
                    if (bo != null)
                    {
                        blackIs1 = bo.BooleanValue;
                    }
                    bo = param.GetAsBoolean(PdfName.ENCODEDBYTEALIGN);
                    if (bo != null)
                    {
                        byteAlign = bo.BooleanValue;
                    }
                }
                byte[] outBuf = new byte[(width + 7) / 8 * height];
                TIFFFaxDecompressor decoder = new TIFFFaxDecompressor();

                if (k == 0 || k > 0)
                {
                    int tiffT4Options = k > 0 ? TIFFConstants.GROUP3OPT_2DENCODING : 0;
                    tiffT4Options |= byteAlign ? TIFFConstants.GROUP3OPT_FILLBITS : 0;
                    decoder.SetOptions(1, TIFFConstants.COMPRESSION_CCITTFAX3, tiffT4Options, 0);
                    decoder.DecodeRaw(outBuf, b, width, height);
                    if (decoder.fails > 0)
                    {
                        byte[] outBuf2  = new byte[(width + 7) / 8 * height];
                        int    oldFails = decoder.fails;
                        decoder.SetOptions(1, TIFFConstants.COMPRESSION_CCITTRLE, tiffT4Options, 0);
                        decoder.DecodeRaw(outBuf2, b, width, height);
                        if (decoder.fails < oldFails)
                        {
                            outBuf = outBuf2;
                        }
                    }
                }
                else
                {
                    TIFFFaxDecoder deca = new TIFFFaxDecoder(1, width, height);
                    deca.DecodeT6(outBuf, b, 0, height, 0);
                }
                if (!blackIs1)
                {
                    int len = outBuf.Length;
                    for (int t = 0; t < len; ++t)
                    {
                        outBuf[t] ^= 0xff;
                    }
                }
                b = outBuf;
                return(b);
            }
Пример #14
0
 internal static int? GetFlags(PdfDictionary field) {
     PdfName type = field.GetAsName(PdfName.FT);
     if (!PdfName.BTN.Equals(type))
         return null;
     PdfNumber flags = field.GetAsNumber(PdfName.FF);
     if (flags == null)
         return null;
     return flags.IntValue;
 }
Пример #15
0
 /**
  * Parses the samples of the image from the underlying content parser, ignoring all filters.
  * The parser must be positioned immediately after the ID operator that ends the inline image's dictionary.
  * The parser will be left positioned immediately following the EI operator.
  * This is primarily useful if no filters have been applied. 
  * @param imageDictionary the dictionary of the inline image
  * @param ps the content parser
  * @return the samples of the image
  * @throws IOException if anything bad happens during parsing
  */
 private static byte[] ParseUnfilteredSamples(PdfDictionary imageDictionary, PdfContentParser ps) {
     // special case:  when no filter is specified, we just read the number of bits
     // per component, multiplied by the width and height.
     if (imageDictionary.Contains(PdfName.FILTER))
         throw new ArgumentException("Dictionary contains filters");
     
     PdfNumber h = imageDictionary.GetAsNumber(PdfName.HEIGHT);
     
     int bytesToRead = ComputeBytesPerRow(imageDictionary) * h.IntValue;
     byte[] bytes = new byte[bytesToRead];
     PRTokeniser tokeniser = ps.GetTokeniser();
     
     tokeniser.Read(); // skip next character (which better be a whitespace character - I suppose we could check for this)
     for (int i = 0; i < bytesToRead; i++){
         int ch = tokeniser.Read();
         if (ch == -1)
             throw new InlineImageParseException("End of content stream reached before end of image data");
         
         bytes[i] = (byte)ch;
     }
     PdfObject ei = ps.ReadPRObject();
     if (!ei.ToString().Equals("EI"))
         throw new InlineImageParseException("EI not found after end of image data");
     
     return bytes;
 }
Пример #16
0
        private void DoType1TT()
        {
            CMapToUnicode toUnicode = null;
            PdfObject     enc       = PdfReader.GetPdfObject(font.Get(PdfName.ENCODING));

            if (enc == null)
            {
                PdfName baseFont = font.GetAsName(PdfName.BASEFONT);
                if (BuiltinFonts14.ContainsKey(fontName) &&
                    (PdfName.SYMBOL.Equals(baseFont) || PdfName.ZAPFDINGBATS.Equals(baseFont)))
                {
                    FillEncoding(baseFont);
                }
                else
                {
                    FillEncoding(null);
                }
                toUnicode = ProcessToUnicode();
                if (toUnicode != null)
                {
                    IDictionary <int, int> rm = toUnicode.CreateReverseMapping();
                    foreach (KeyValuePair <int, int> kv in rm)
                    {
                        uni2byte[kv.Key]   = kv.Value;
                        byte2uni[kv.Value] = kv.Key;
                    }
                }
            }
            else
            {
                if (enc.IsName())
                {
                    FillEncoding((PdfName)enc);
                }
                else if (enc.IsDictionary())
                {
                    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;
                                    byte2uni[currentNumber] = c[0];
                                    diffmap[c[0]]           = currentNumber;
                                }
                                else
                                {
                                    if (toUnicode == null)
                                    {
                                        toUnicode = ProcessToUnicode();
                                        if (toUnicode == null)
                                        {
                                            toUnicode = new CMapToUnicode();
                                        }
                                    }
                                    string unicode = toUnicode.Lookup(new byte[] { (byte)currentNumber }, 0, 1);
                                    if ((unicode != null) && (unicode.Length == 1))
                                    {
                                        this.uni2byte[unicode[0]]    = currentNumber;
                                        this.byte2uni[currentNumber] = unicode[0];
                                        this.diffmap[unicode[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 = 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);
                fontWeight  = bf.GetFontDescriptor(FONT_WEIGHT, 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;
                int nSize = f + newWidths.Size;
                if (widths.Length < nSize)
                {
                    int[] tmp = new int[nSize];
                    System.Array.Copy(widths, 0, tmp, 0, f);
                    widths = tmp;
                }
                for (int k = 0; k < newWidths.Size; ++k)
                {
                    widths[f + k] = newWidths.GetAsNumber(k).IntValue;
                }
            }
            FillFontDesc(font.GetAsDict(PdfName.FONTDESCRIPTOR));
        }
        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 = 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));
        }