Beispiel #1
0
        /**
         * When an MCID is encountered, the parser will check the list
         * structure items and turn an annotation into an XObject if
         * necessary.
         * @param mcid	the MCID that was encountered in the content stream
         * @throws IOException
         */
        virtual protected void DealWithMcid(PdfNumber mcid)
        {
            if (mcid == null)
            {
                return;
            }
            StructureItem item = items[0];

            if (LOGGER.IsLogging(Level.INFO))
            {
                LOGGER.Info(String.Format("Encountered MCID {0} in content, comparing with {1}", mcid, item));
            }
            switch (item.CheckMCID(pageref.Number, mcid.IntValue))
            {
            case 0:
                StructureObject obj = (StructureObject)item;
                ConvertToXObject(obj);
                LOGGER.Info("Removed structure item from stack.");
                items.RemoveAt(0);
                DealWithMcid(mcid);
                return;

            case 1:
                LOGGER.Info("Removed structure item from stack.");
                items.RemoveAt(0);
                return;

            default:
                LOGGER.Warn("MCID not found! There's probably an error in your form!");
                // hack to deal with MCIDs that are added in the wrong order
                int check;
                for (int i = 1; i < items.Count; i++)
                {
                    item  = items[i];
                    check = item.CheckMCID(pageref.Number, mcid.IntValue);
                    switch (check)
                    {
                    case 1:
                        LOGGER.Info("Removed structure item from stack.");
                        items.RemoveAt(i);
                        return;

                    case 0:
                        break;
                    }
                }
                throw new DocumentException(MessageLocalization.GetComposedMessage("can.t.read.document.structure"));
            }
        }
        /**
         * Processes the kids object of a StructElem dictionary.
         * This kids object can be a number (MCID), another StructElem dictionary,
         * an MCR dictionary, an OBJR dictionary, or an array of the above.
         * @param structElem	the StructElem dictionary
         * @param ref			the reference to the StructElem dictionary
         * @param object		the kids object
         */

        protected virtual void ProcessStructElemKids(PdfDictionary structElem, PdfIndirectReference refa, PdfObject objecta)
        {
            if (LOGGER.IsLogging(Level.INFO))
            {
                LOGGER.Info(String.Format("addStructureItem({0}, {1}, {2})", structElem, refa, objecta));
            }
            if (objecta == null)
            {
                return;
            }
            StructureItem item;

            switch (objecta.Type)
            {
            case PdfObject.NUMBER:
                item = new StructureMCID(structElem.GetAsIndirectObject(PdfName.PG), (PdfNumber)objecta);
                Add(item);
                LOGGER.Info("Added " + item);
                break;

            case PdfObject.ARRAY:
                PdfArray array = (PdfArray)objecta;
                for (int i = 0; i < array.Size; i++)
                {
                    ProcessStructElemKids(structElem, array.GetAsIndirectObject(i), array.GetDirectObject(i));
                }
                break;

            case PdfObject.DICTIONARY:
                PdfDictionary dict = (PdfDictionary)objecta;
                if (dict.CheckType(PdfName.MCR))
                {
                    item = new StructureMCID(dict);
                    Add(item);
                    LOGGER.Info("Added " + item);
                }
                else if (dict.CheckType(PdfName.OBJR))
                {
                    item = new StructureObject(structElem, refa, dict);
                    Add(item);
                    LOGGER.Info("Added " + item);
                }
                else
                {
                    ProcessStructElems(dict, refa);
                }
                break;
            }
        }
Beispiel #3
0
        /**
         * Converts an annotation structure item to a Form XObject annotation.
         * @param item the structure item
         * @throws IOException
         */
        protected void ConvertToXObject(StructureObject item)
        {
            PdfDictionary structElem = item.GetStructElem();

            if (structElem == null)
            {
                return;
            }
            PdfDictionary dict = item.GetObjAsDict();

            if (dict == null || !dict.CheckType(PdfName.ANNOT))
            {
                return;
            }
            PdfDictionary ap = dict.GetAsDict(PdfName.AP);

            if (ap == null)
            {
                return;
            }
            PdfNumber structParent = dict.GetAsNumber(PdfName.STRUCTPARENT);

            if (structParent == null)
            {
                return;
            }
            PdfStream stream = ap.GetAsStream(PdfName.N);

            if (stream == null)
            {
                return;
            }
            stream.Put(PdfName.STRUCTPARENT, structParent);
            PdfIndirectReference xobjr = ap.GetAsIndirectObject(PdfName.N);

            if (xobjr == null)
            {
                return;
            }
            // remove the annotation from the page
            for (int i = 0; i < annots.Length; i++)
            {
                PdfIndirectReference annotref = annots.GetAsIndirectObject(i);
                if (item.GetObjRef().Number == annotref.Number)
                {
                    annots.Remove(i);
                    break;
                }
            }
            // replace the existing attributes by a PrintField attribute
            PdfDictionary attribute = new PdfDictionary();

            attribute.Put(PdfName.O, PdfName.PRINTFIELD);
            PdfString description = dict.GetAsString(PdfName.TU);

            if (description == null)
            {
                description = dict.GetAsString(PdfName.T);
            }
            if (PdfName.BTN.Equals(dict.Get(PdfName.FT)))
            {
                PdfNumber fflags = dict.GetAsNumber(PdfName.FF);
                if (fflags != null)
                {
                    int ff = fflags.IntValue;
                    if ((ff & PdfFormField.FF_PUSHBUTTON) != 0)
                    {
                        attribute.Put(PdfName.ROLE, PdfName.PB);
                    }
                    // I don't think the condition below will ever be true
                    if ((ff & PdfFormField.FF_RADIO) != 0)
                    {
                        attribute.Put(PdfName.ROLE, PdfName.rb);
                    }
                    else
                    {
                        attribute.Put(PdfName.ROLE, PdfName.CB);
                    }
                }
            }
            else
            {
                attribute.Put(PdfName.ROLE, PdfName.TV);
            }
            attribute.Put(PdfName.DESC, description);
            // Updating the values of the StructElem dictionary
            PdfString t = structElem.GetAsString(PdfName.T);

            if (t == null || t.ToString().Trim().Length == 0)
            {
                structElem.Put(PdfName.T, dict.GetAsString(PdfName.T));
            }
            structElem.Put(PdfName.A, attribute);
            structElem.Put(PdfName.S, PdfName.P);
            structElem.Put(PdfName.PG, pageref);
            // Defining a new MCID
            int mcid = items.ProcessMCID(structParents, item.GetRef());

            LOGGER.Info("Using MCID " + mcid);
            structElem.Put(PdfName.K, new PdfNumber(mcid));
            // removing the annotation from the parent tree
            items.RemoveFromParentTree(structParent);
            // Adding the XObject to the page
            PdfName xobj = new PdfName("XObj" + structParent.IntValue);

            LOGGER.Info("Creating XObject with name " + xobj);
            xobjects.Put(xobj, xobjr);
            PdfArray array = dict.GetAsArray(PdfName.RECT);
            // Getting the position of the annotation
            Rectangle rect = new Rectangle(
                array.GetAsNumber(0).FloatValue, array.GetAsNumber(1).FloatValue,
                array.GetAsNumber(2).FloatValue, array.GetAsNumber(3).FloatValue);

            rect.Normalize();
            // A Do operator is forbidden inside a text block
            if (inText && !btWrite)
            {
                LOGGER.Debug("Introducing extra ET");
                byte[] bytes = Encoding.ASCII.GetBytes("ET\n");
                baos.Write(bytes, 0, bytes.Length);
                etExtra = true;
            }
            // Writing the marked-content sequence with the Do operator
            // Note that the position assumes that the CTM wasn't changed in the graphics state
            // TODO: do the math if the CTM did change!
            ByteBuffer buf = new ByteBuffer();

            buf.Append("/P <</MCID ");
            buf.Append(mcid);
            buf.Append(">> BDC\n");
            buf.Append("q 1 0 0 1 ");
            buf.Append(rect.Left.ToString(CultureInfo.InvariantCulture));
            buf.Append(" ");
            buf.Append(rect.Bottom.ToString(CultureInfo.InvariantCulture));
            buf.Append(" cm ");
            buf.Append(xobj.GetBytes());
            buf.Append(" Do Q\n");
            buf.Append("EMC\n");
            buf.Flush();
            buf.WriteTo(baos);
            // if we were inside a text block, we've introduced an ET, so we'll need to write a BT
            if (inText)
            {
                btWrite = true;
            }
        }
Beispiel #4
0
     /**
      * Converts an annotation structure item to a Form XObject annotation.
      * @param item the structure item
      * @throws IOException
      */
     virtual protected void ConvertToXObject(StructureObject item) {
         PdfDictionary structElem = item.GetStructElem();
         if (structElem == null)
             return;
         PdfDictionary dict =  item.GetObjAsDict();
         if (dict == null || !dict.CheckType(PdfName.ANNOT))
             return;
         PdfDictionary ap = dict.GetAsDict(PdfName.AP);
         if (ap == null)
             return;
         PdfNumber structParent = dict.GetAsNumber(PdfName.STRUCTPARENT);
         if (structParent == null)
             return;
         PdfStream stream = ap.GetAsStream(PdfName.N);
         if (stream == null)
             return;
         stream.Put(PdfName.STRUCTPARENT, structParent);
         PdfIndirectReference xobjr = ap.GetAsIndirectObject(PdfName.N);
         if (xobjr == null)
             return;
         // remove the annotation from the page
 	    for (int i = 0; i < annots.Length; i++) {
 		    PdfIndirectReference annotref = annots.GetAsIndirectObject(i);
 		    if (item.GetObjRef().Number == annotref.Number) {
 			    annots.Remove(i);
 			    break;
 		    }
 	    }
 	    // replace the existing attributes by a PrintField attribute
         PdfDictionary attribute = new PdfDictionary();
         attribute.Put(PdfName.O, PdfName.PRINTFIELD);
         PdfString description = dict.GetAsString(PdfName.TU);
         if (description == null)
             description = dict.GetAsString(PdfName.T);
         if (PdfName.BTN.Equals(dict.Get(PdfName.FT))) {
             PdfNumber fflags = dict.GetAsNumber(PdfName.FF);
             if (fflags != null) {
                 int ff = fflags.IntValue;
                 if ((ff & PdfFormField.FF_PUSHBUTTON) != 0)
                     attribute.Put(PdfName.ROLE, PdfName.PB);
                 // I don't think the condition below will ever be true
                 if ((ff & PdfFormField.FF_RADIO) != 0)
                     attribute.Put(PdfName.ROLE, PdfName.rb);
                 else
                     attribute.Put(PdfName.ROLE, PdfName.CB);
             }
         }
         else {
             attribute.Put(PdfName.ROLE, PdfName.TV);
         }
         attribute.Put(PdfName.DESC, description);
         // Updating the values of the StructElem dictionary
         PdfString t = structElem.GetAsString(PdfName.T);
         if (t == null || t.ToString().Trim().Length == 0)
             structElem.Put(PdfName.T, dict.GetAsString(PdfName.T));
         structElem.Put(PdfName.A, attribute);
         structElem.Put(PdfName.S, PdfName.P);
         structElem.Put(PdfName.PG, pageref);
       	// Defining a new MCID
        	int mcid = items.ProcessMCID(structParents, item.GetRef());
         LOGGER.Info("Using MCID " + mcid);
         structElem.Put(PdfName.K, new PdfNumber(mcid));
         // removing the annotation from the parent tree
         items.RemoveFromParentTree(structParent);
         // Adding the XObject to the page
         PdfName xobj = new PdfName("XObj" + structParent.IntValue);
         LOGGER.Info("Creating XObject with name " + xobj);
         xobjects.Put(xobj, xobjr);
         PdfArray array = dict.GetAsArray(PdfName.RECT);
         // Getting the position of the annotation
         Rectangle rect = new Rectangle(
             array.GetAsNumber(0).FloatValue, array.GetAsNumber(1).FloatValue,
             array.GetAsNumber(2).FloatValue, array.GetAsNumber(3).FloatValue);
         rect.Normalize();
         // A Do operator is forbidden inside a text block
         if (inText && !btWrite) {
             LOGGER.Debug("Introducing extra ET");
             byte[] bytes = Encoding.ASCII.GetBytes("ET\n");
             baos.Write(bytes, 0, bytes.Length);
             etExtra = true;
         }
 	    // Writing the marked-content sequence with the Do operator
 	    // Note that the position assumes that the CTM wasn't changed in the graphics state
 	    // TODO: do the math if the CTM did change!
         ByteBuffer buf = new ByteBuffer();
         buf.Append("/P <</MCID ");
         buf.Append(mcid);
         buf.Append(">> BDC\n");
         buf.Append("q 1 0 0 1 ");
         buf.Append(rect.Left.ToString(CultureInfo.InvariantCulture));
         buf.Append(" ");
         buf.Append(rect.Bottom.ToString(CultureInfo.InvariantCulture));
         buf.Append(" cm ");
         buf.Append(xobj.GetBytes());
         buf.Append(" Do Q\n");
         buf.Append("EMC\n");
         buf.Flush();
         buf.WriteTo(baos);
 	    // if we were inside a text block, we've introduced an ET, so we'll need to write a BT
         if (inText)
             btWrite = true;
     }