Example #1
0
        /**
         * Parses the ToUnicode entry, if present, and constructs a CMap for it
         * @since 2.1.7
         */
        private void ProcessToUnicode()
        {
            PdfObject toUni = PdfReader.GetPdfObjectRelease(fontDic.Get(PdfName.TOUNICODE));

            if (toUni is PRStream)
            {
                try {
                    byte[] touni           = PdfReader.GetStreamBytes((PRStream)toUni);
                    CidLocationFromByte lb = new CidLocationFromByte(touni);
                    toUnicodeCmap = new CMapToUnicode();
                    CMapParserEx.ParseCid("", toUnicodeCmap, lb);
                    uni2cid = toUnicodeCmap.CreateReverseMapping();
                } catch {
                    toUnicodeCmap = null;
                    uni2cid       = null;
                    // technically, we should log this or provide some sort of feedback... but sometimes the cmap will be junk, but it's still possible to get text, so we don't want to throw an exception
                    //throw new IllegalStateException("Unable to process ToUnicode map - " + e.GetMessage(), e);
                }
            }
            else if (isType0)
            {
                // fake a ToUnicode for CJK Identity-H fonts
                try {
                    PdfName encodingName = fontDic.GetAsName(PdfName.ENCODING);
                    if (encodingName == null)
                    {
                        return;
                    }
                    String enc = PdfName.DecodeName(encodingName.ToString());
                    if (!enc.Equals("Identity-H"))
                    {
                        return;
                    }
                    PdfArray      df      = (PdfArray)PdfReader.GetPdfObjectRelease(fontDic.Get(PdfName.DESCENDANTFONTS));
                    PdfDictionary cidft   = (PdfDictionary)PdfReader.GetPdfObjectRelease(df[0]);
                    PdfDictionary cidinfo = cidft.GetAsDict(PdfName.CIDSYSTEMINFO);
                    if (cidinfo == null)
                    {
                        return;
                    }
                    PdfString ordering = cidinfo.GetAsString(PdfName.ORDERING);
                    if (ordering == null)
                    {
                        return;
                    }
                    CMapToUnicode touni = IdentityToUnicode.GetMapFromOrdering(ordering.ToUnicodeString());
                    if (touni == null)
                    {
                        return;
                    }
                    toUnicodeCmap = touni;
                    uni2cid       = toUnicodeCmap.CreateReverseMapping();
                } catch (IOException e) {
                    toUnicodeCmap = null;
                    uni2cid       = null;
                }
            }
        }
Example #2
0
        private void Init()
        {
            encoding     = "";
            fontSpecific = false;
            fontType     = FONT_TYPE_DOCUMENT;
            PdfName baseFont = font.GetAsName(PdfName.BASEFONT);

            fontName = baseFont != null?PdfName.DecodeName(baseFont.ToString()) : "Unspecified Font Name";

            PdfName subType = font.GetAsName(PdfName.SUBTYPE);

            if (PdfName.TYPE1.Equals(subType) || PdfName.TRUETYPE.Equals(subType))
            {
                DoType1TT();
            }
            else
            {
                PdfName encodingName = font.GetAsName(PdfName.ENCODING);
                if (encodingName != null)
                {
                    String enc       = PdfName.DecodeName(encodingName.ToString());
                    String ffontname = CJKFont.GetCompatibleFont(enc);
                    if (ffontname != null)
                    {
                        cjkMirror   = BaseFont.CreateFont(ffontname, enc, false);
                        cjkEncoding = enc;
                        uniMap      = ((CJKFont)cjkMirror).UniMap;
                    }
                    if (PdfName.TYPE0.Equals(subType))
                    {
                        isType0 = true;
                        if (!enc.Equals("Identity-H") && cjkMirror != null)
                        {
                            PdfArray      df    = (PdfArray)PdfReader.GetPdfObjectRelease(font.Get(PdfName.DESCENDANTFONTS));
                            PdfDictionary cidft = (PdfDictionary)PdfReader.GetPdfObjectRelease(df[0]);
                            PdfNumber     dwo   = (PdfNumber)PdfReader.GetPdfObjectRelease(cidft.Get(PdfName.DW));
                            if (dwo != null)
                            {
                                defaultWidth = dwo.IntValue;
                            }
                            hMetrics = ReadWidths((PdfArray)PdfReader.GetPdfObjectRelease(cidft.Get(PdfName.W)));

                            PdfDictionary fontDesc = (PdfDictionary)PdfReader.GetPdfObjectRelease(cidft.Get(PdfName.FONTDESCRIPTOR));
                            FillFontDesc(fontDesc);
                        }
                        else
                        {
                            ProcessType0(font);
                        }
                    }
                }
            }
        }
Example #3
0
        /** Creates a new instance of DocumentFont */
        internal DocumentFont(PRIndirectReference refFont)
        {
            encoding     = "";
            fontSpecific = false;
            this.refFont = refFont;
            fontType     = FONT_TYPE_DOCUMENT;
            font         = (PdfDictionary)PdfReader.GetPdfObject(refFont);
            PdfName baseFont = font.GetAsName(PdfName.BASEFONT);

            fontName = baseFont != null?PdfName.DecodeName(baseFont.ToString()) : "Unspecified Font Name";

            PdfName subType = font.GetAsName(PdfName.SUBTYPE);

            if (PdfName.TYPE1.Equals(subType) || PdfName.TRUETYPE.Equals(subType))
            {
                DoType1TT();
            }
            else
            {
                for (int k = 0; k < cjkNames.Length; ++k)
                {
                    if (fontName.StartsWith(cjkNames[k]))
                    {
                        fontName  = cjkNames[k];
                        cjkMirror = BaseFont.CreateFont(fontName, cjkEncs[k], false);
                        return;
                    }
                }
                PdfName encodingName = font.GetAsName(PdfName.ENCODING);
                if (encodingName != null)
                {
                    String enc = PdfName.DecodeName(encodingName.ToString());
                    for (int k = 0; k < cjkEncs2.Length; ++k)
                    {
                        if (enc.StartsWith(cjkEncs2[k]))
                        {
                            if (k > 3)
                            {
                                k -= 4;
                            }
                            cjkMirror = BaseFont.CreateFont(cjkNames2[k], cjkEncs2[k], false);
                            return;
                        }
                    }
                    if (PdfName.TYPE0.Equals(subType) && enc.Equals("Identity-H"))
                    {
                        ProcessType0(font);
                        isType0 = true;
                    }
                }
            }
        }
Example #4
0
        /**
         * Adds a template to this template.
         *
         * @param template the template
         * @param a an element of the transformation matrix
         * @param b an element of the transformation matrix
         * @param c an element of the transformation matrix
         * @param d an element of the transformation matrix
         * @param e an element of the transformation matrix
         * @param f an element of the transformation matrix
         */

        public override void addTemplate(PdfTemplate template, float a, float b, float c, float d, float e, float f)
        {
            checkNoPattern(template);
            PdfName name = writer.addDirectTemplateSimple(template);

            content.Append("q ");
            content.Append(a).Append(' ');
            content.Append(b).Append(' ');
            content.Append(c).Append(' ');
            content.Append(d).Append(' ');
            content.Append(e).Append(' ');
            content.Append(f).Append(" cm ");
            content.Append(name.ToString()).Append(" Do Q").Append_i(separator);
            xObjectDictionary.put(name, template.IndirectReference);
        }
Example #5
0
        private void Init()
        {
            encoding     = "";
            fontSpecific = false;
            fontType     = FONT_TYPE_DOCUMENT;
            PdfName baseFont = font.GetAsName(PdfName.BASEFONT);

            fontName = baseFont != null?PdfName.DecodeName(baseFont.ToString()) : "Unspecified Font Name";

            PdfName subType = font.GetAsName(PdfName.SUBTYPE);

            if (PdfName.TYPE1.Equals(subType) || PdfName.TRUETYPE.Equals(subType))
            {
                DoType1TT();
            }
            else
            {
                PdfName encodingName = font.GetAsName(PdfName.ENCODING);
                if (encodingName != null)
                {
                    String enc       = PdfName.DecodeName(encodingName.ToString());
                    String ffontname = CJKFont.GetCompatibleFont(enc);
                    if (ffontname != null)
                    {
                        cjkMirror   = BaseFont.CreateFont(ffontname, enc, false);
                        cjkEncoding = enc;
                        uniMap      = ((CJKFont)cjkMirror).UniMap;
                    }
                    if (PdfName.TYPE0.Equals(subType) && enc.Equals("Identity-H"))
                    {
                        ProcessType0(font);
                        isType0 = true;
                    }
                }
            }
        }
Example #6
0
 /** Sets the open and close page additional action.
 * @param actionType the action type. It can be <CODE>PdfWriter.PAGE_OPEN</CODE>
 * or <CODE>PdfWriter.PAGE_CLOSE</CODE>
 * @param action the action to perform
 * @throws PdfException if the action type is invalid
 */    
 public virtual void SetPageAction(PdfName actionType, PdfAction action) {
     if (!actionType.Equals(PAGE_OPEN) && !actionType.Equals(PAGE_CLOSE))
         throw new PdfException(MessageLocalization.GetComposedMessage("invalid.page.additional.action.type.1", actionType.ToString()));
     pdf.SetPageAction(actionType, action);
 }
Example #7
0
 /** Additional-actions defining the actions to be taken in
 * response to various trigger events affecting the document
 * as a whole. The actions types allowed are: <CODE>DOCUMENT_CLOSE</CODE>,
 * <CODE>WILL_SAVE</CODE>, <CODE>DID_SAVE</CODE>, <CODE>WILL_PRINT</CODE>
 * and <CODE>DID_PRINT</CODE>.
 *
 * @param actionType the action type
 * @param action the action to execute in response to the trigger
 * @throws PdfException on invalid action type
 */
 public virtual void SetAdditionalAction(PdfName actionType, PdfAction action) {
     if (!(actionType.Equals(DOCUMENT_CLOSE) ||
     actionType.Equals(WILL_SAVE) ||
     actionType.Equals(DID_SAVE) ||
     actionType.Equals(WILL_PRINT) ||
     actionType.Equals(DID_PRINT))) {
         throw new PdfException(MessageLocalization.GetComposedMessage("invalid.additional.action.type.1", actionType.ToString()));
     }
     pdf.AddAdditionalAction(actionType, action);
 }
Example #8
0
 /** Additional-actions defining the actions to be taken in
 * response to various trigger events affecting the document
 * as a whole. The actions types allowed are: <CODE>DOCUMENT_CLOSE</CODE>,
 * <CODE>WILL_SAVE</CODE>, <CODE>DID_SAVE</CODE>, <CODE>WILL_PRINT</CODE>
 * and <CODE>DID_PRINT</CODE>.
 *
 * @param actionType the action type
 * @param action the action to execute in response to the trigger
 * @throws PdfException on invalid action type
 */
 public override void SetAdditionalAction(PdfName actionType, PdfAction action) {
     if (!(actionType.Equals(DOCUMENT_CLOSE) ||
     actionType.Equals(WILL_SAVE) ||
     actionType.Equals(DID_SAVE) ||
     actionType.Equals(WILL_PRINT) ||
     actionType.Equals(DID_PRINT))) {
         throw new PdfException(MessageLocalization.GetComposedMessage("invalid.additional.action.type.1", actionType.ToString()));
     }
     PdfDictionary aa = reader.Catalog.GetAsDict(PdfName.AA);
     if (aa == null) {
         if (action == null)
             return;
         aa = new PdfDictionary();
         reader.Catalog.Put(PdfName.AA, aa);
     }
     MarkUsed(aa);
     if (action == null)
         aa.Remove(actionType);
     else
         aa.Put(actionType, action);
 }
Example #9
0
 /**
 * Sets the open and close page additional action.
 * @param actionType the action type. It can be <CODE>PdfWriter.PAGE_OPEN</CODE>
 * or <CODE>PdfWriter.PAGE_CLOSE</CODE>
 * @param action the action to perform
 * @param page the page where the action will be applied. The first page is 1
 * @throws PdfException if the action type is invalid
 */    
 internal void SetPageAction(PdfName actionType, PdfAction action, int page) {
     if (!actionType.Equals(PAGE_OPEN) && !actionType.Equals(PAGE_CLOSE))
         throw new PdfException(MessageLocalization.GetComposedMessage("invalid.page.additional.action.type.1", actionType.ToString()));
     PdfDictionary pg = reader.GetPageN(page);
     PdfDictionary aa = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.AA), pg);
     if (aa == null) {
         aa = new PdfDictionary();
         pg.Put(PdfName.AA, aa);
         MarkUsed(pg);
     }
     aa.Put(actionType, action);
     MarkUsed(aa);
 }
Example #10
0
 /** Additional-actions defining the actions to be taken in
 * response to various trigger events affecting the document
 * as a whole. The actions types allowed are: <CODE>DOCUMENT_CLOSE</CODE>,
 * <CODE>WILL_SAVE</CODE>, <CODE>DID_SAVE</CODE>, <CODE>WILL_PRINT</CODE>
 * and <CODE>DID_PRINT</CODE>.
 *
 * @param actionType the action type
 * @param action the action to execute in response to the trigger
 * @throws PdfException on invalid action type
 */
 public override void SetAdditionalAction(PdfName actionType, PdfAction action)
 {
     if (!(actionType.Equals(DOCUMENT_CLOSE) ||
     actionType.Equals(WILL_SAVE) ||
     actionType.Equals(DID_SAVE) ||
     actionType.Equals(WILL_PRINT) ||
     actionType.Equals(DID_PRINT))) {
         throw new PdfException("Invalid additional action type: " + actionType.ToString());
     }
     PdfDictionary aa = (PdfDictionary)PdfReader.GetPdfObject(reader.Catalog.Get(PdfName.AA));
     if (aa == null) {
         if (action == null)
             return;
         aa = new PdfDictionary();
         reader.Catalog.Put(PdfName.AA, aa);
     }
     MarkUsed(aa);
     if (action == null)
         aa.Remove(actionType);
     else
         aa.Put(actionType, action);
 }
Example #11
0
 /**
 * Sets the open and close page additional action.
 * @param actionType the action type. It can be <CODE>PdfWriter.PAGE_OPEN</CODE>
 * or <CODE>PdfWriter.PAGE_CLOSE</CODE>
 * @param action the action to perform
 * @param page the page where the action will be applied. The first page is 1
 * @throws PdfException if the action type is invalid
 */
 internal void SetPageAction(PdfName actionType, PdfAction action, int page)
 {
     if (!actionType.Equals(PAGE_OPEN) && !actionType.Equals(PAGE_CLOSE))
         throw new PdfException("Invalid page additional action type: " + actionType.ToString());
     PdfDictionary pg = reader.GetPageN(page);
     PdfDictionary aa = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.AA), pg);
     if (aa == null) {
         aa = new PdfDictionary();
         pg.Put(PdfName.AA, aa);
         MarkUsed(pg);
     }
     aa.Put(actionType, action);
     MarkUsed(aa);
 }
Example #12
0
        private void Init()
        {
            encoding     = "";
            fontSpecific = false;
            fontType     = FONT_TYPE_DOCUMENT;
            PdfName baseFont = font.GetAsName(PdfName.BASEFONT);

            fontName = baseFont != null?PdfName.DecodeName(baseFont.ToString()) : "Unspecified Font Name";

            PdfName subType = font.GetAsName(PdfName.SUBTYPE);

            if (PdfName.TYPE1.Equals(subType) || PdfName.TRUETYPE.Equals(subType))
            {
                DoType1TT();
            }
            else if (PdfName.TYPE3.Equals(subType))
            {
                // In case of a Type3 font, we just show the characters as is.
                // Note that this doesn't always make sense:
                // Type 3 fonts are user defined fonts where arbitrary characters are mapped to custom glyphs
                // For instance: the character a could be mapped to an image of a dog, the character b to an image of a cat
                // When parsing a document that shows a cat and a dog, you shouldn't expect seeing a cat and a dog. Instead you'll get b and a.
                FillEncoding(null);
            }
            else
            {
                PdfName encodingName = font.GetAsName(PdfName.ENCODING);
                if (encodingName != null)
                {
                    String enc       = PdfName.DecodeName(encodingName.ToString());
                    String ffontname = CJKFont.GetCompatibleFont(enc);
                    if (ffontname != null)
                    {
                        cjkMirror   = BaseFont.CreateFont(ffontname, enc, false);
                        cjkEncoding = enc;
                        uniMap      = ((CJKFont)cjkMirror).UniMap;
                    }
                    if (PdfName.TYPE0.Equals(subType))
                    {
                        isType0 = true;
                        if (!enc.Equals("Identity-H") && cjkMirror != null)
                        {
                            PdfArray      df    = (PdfArray)PdfReader.GetPdfObjectRelease(font.Get(PdfName.DESCENDANTFONTS));
                            PdfDictionary cidft = (PdfDictionary)PdfReader.GetPdfObjectRelease(df[0]);
                            PdfNumber     dwo   = (PdfNumber)PdfReader.GetPdfObjectRelease(cidft.Get(PdfName.DW));
                            if (dwo != null)
                            {
                                defaultWidth = dwo.IntValue;
                            }
                            hMetrics = ReadWidths((PdfArray)PdfReader.GetPdfObjectRelease(cidft.Get(PdfName.W)));

                            PdfDictionary fontDesc = (PdfDictionary)PdfReader.GetPdfObjectRelease(cidft.Get(PdfName.FONTDESCRIPTOR));
                            FillFontDesc(fontDesc);
                        }
                        else
                        {
                            ProcessType0(font);
                        }
                    }
                }
            }
        }
Example #13
0
        private void Init(PdfDictionary parent, PdfName structureType)
        {
            if (!top.Writer.GetStandardStructElems().Contains(structureType))
            {
                PdfDictionary roleMap = top.GetAsDict(PdfName.ROLEMAP);
                if (roleMap == null || !roleMap.Contains(structureType))
                {
                    throw new DocumentException(MessageLocalization.GetComposedMessage("unknown.structure.element.role.1", structureType.ToString()));
                }
                else
                {
                    this.structureType = roleMap.GetAsName(structureType);
                }
            }
            else
            {
                this.structureType = structureType;
            }
            PdfObject kido = parent.Get(PdfName.K);
            PdfArray  kids = null;

            if (kido == null)
            {
                kids = new PdfArray();
                parent.Put(PdfName.K, kids);
            }
            else if (kido is PdfArray)
            {
                kids = (PdfArray)kido;
            }
            else
            {
                kids = new PdfArray();
                kids.Add(kido);
                parent.Put(PdfName.K, kids);
            }
            if (kids.Size > 0)
            {
                if (kids.GetAsNumber(0) != null)
                {
                    kids.Remove(0);
                }
                if (kids.Size > 0)
                {
                    PdfDictionary mcr = kids.GetAsDict(0);
                    if (mcr != null && PdfName.MCR.Equals(mcr.GetAsName(PdfName.TYPE)))
                    {
                        kids.Remove(0);
                    }
                }
            }
            kids.Add(this);
            Put(PdfName.S, structureType);
            reference = top.Writer.PdfIndirectReference;
        }
Example #14
0
 protected String XmlName(PdfName name)
 {
     String oldName = name.ToString();
     String xmlName = oldName.Remove(oldName.IndexOf("/"), 1);
     xmlName = (xmlName.ToLower()[0])
                + xmlName.Substring(1);
     return xmlName;
 }
Example #15
0
 private void Init(PdfDictionary parent, PdfName structureType)
 {
     if(!top.Writer.GetStandardStructElems().Contains(structureType)) {
         PdfDictionary roleMap = top.GetAsDict(PdfName.ROLEMAP);
         if(roleMap == null || !roleMap.Contains(structureType))
             throw new DocumentException(MessageLocalization.GetComposedMessage("unknown.structure.element.role.1", structureType.ToString()));
         else
             this.structureType = roleMap.GetAsName(structureType);
     }
     else {
         this.structureType = structureType;
     }
     PdfObject kido = parent.Get(PdfName.K);
     PdfArray kids = null;
     if (kido == null)
     {
         kids = new PdfArray();
         parent.Put(PdfName.K, kids);
     }
     else if (kido is PdfArray)
     {
         kids = (PdfArray) kido;
     }
     else
     {
         kids = new PdfArray();
         kids.Add(kido);
         parent.Put(PdfName.K, kids);
     }
     if (kids.Size > 0)
     {
         if (kids.GetAsNumber(0) != null)
             kids.Remove(0);
         if (kids.Size > 0)
         {
             PdfDictionary mcr = kids.GetAsDict(0);
             if (mcr != null && PdfName.MCR.Equals(mcr.GetAsName(PdfName.TYPE)))
                 kids.Remove(0);
         }
     }
     kids.Add(this);
     Put(PdfName.S, structureType);
     reference = top.Writer.PdfIndirectReference;
 }
Example #16
0
 /** Sets the open and close page additional action.
 * @param actionType the action type. It can be <CODE>PdfWriter.PAGE_OPEN</CODE>
 * or <CODE>PdfWriter.PAGE_CLOSE</CODE>
 * @param action the action to perform
 * @throws PdfException if the action type is invalid
 */
 public virtual void SetPageAction(PdfName actionType, PdfAction action)
 {
     if (!actionType.Equals(PAGE_OPEN) && !actionType.Equals(PAGE_CLOSE))
         throw new PdfException("Invalid page additional action type: " + actionType.ToString());
     pdf.SetPageAction(actionType, action);
 }
Example #17
0
 /** Additional-actions defining the actions to be taken in
 * response to various trigger events affecting the document
 * as a whole. The actions types allowed are: <CODE>DOCUMENT_CLOSE</CODE>,
 * <CODE>WILL_SAVE</CODE>, <CODE>DID_SAVE</CODE>, <CODE>WILL_PRINT</CODE>
 * and <CODE>DID_PRINT</CODE>.
 *
 * @param actionType the action type
 * @param action the action to execute in response to the trigger
 * @throws PdfException on invalid action type
 */
 public virtual void SetAdditionalAction(PdfName actionType, PdfAction action)
 {
     if (!(actionType.Equals(DOCUMENT_CLOSE) ||
     actionType.Equals(WILL_SAVE) ||
     actionType.Equals(DID_SAVE) ||
     actionType.Equals(WILL_PRINT) ||
     actionType.Equals(DID_PRINT))) {
         throw new PdfException("Invalid additional action type: " + actionType.ToString());
     }
     pdf.AddAdditionalAction(actionType, action);
 }
        private int GetXObjNum(PdfName xobjName) {
            String decodedPdfName = PdfName.DecodeName(xobjName.ToString());

            if (decodedPdfName.LastIndexOf(XOBJ_NAME_PREFIX) == -1) {
                return 0;
            }

            String numStr = decodedPdfName.Substring( decodedPdfName.LastIndexOf(XOBJ_NAME_PREFIX) + XOBJ_NAME_PREFIX.Length );
            return Int32.Parse(numStr);
        }