示例#1
0
        /// <summary>
        /// Creates the normal appearance form X object for the annotation that represents
        /// this acro form text field.
        /// </summary>
        void RenderAppearance()
        {
            if (string.IsNullOrEmpty(Text))
            {
                Elements.Remove(PdfAnnotation.Keys.AP);
                return;
            }
#if true_
            PdfFormXObject xobj = new PdfFormXObject(Owner);
            Owner.Internals.AddObject(xobj);
            xobj.Elements["/BBox"]     = new PdfLiteral("[0 0 122.653 12.707]");
            xobj.Elements["/FormType"] = new PdfLiteral("1");
            xobj.Elements["/Matrix"]   = new PdfLiteral("[1 0 0 1 0 0]");
            PdfDictionary res = new PdfDictionary(Owner);
            xobj.Elements["/Resources"] = res;
            res.Elements["/Font"]       = new PdfLiteral("<< /Helv 28 0 R >> /ProcSet [/PDF /Text]");
            xobj.Elements["/Subtype"]   = new PdfLiteral("/Form");
            xobj.Elements["/Type"]      = new PdfLiteral("/XObject");

            string s =
                "/Tx BMC " + '\n' +
                "q" + '\n' +
                "1 1 120.653 10.707 re" + '\n' +
                "W" + '\n' +
                "n" + '\n' +
                "BT" + '\n' +
                "/Helv 7.93 Tf" + '\n' +
                "0 g" + '\n' +
                "2 3.412 Td" + '\n' +
                "(Hello ) Tj" + '\n' +
                "20.256 0 Td" + '\n' +
                "(XXX) Tj" + '\n' +
                "ET" + '\n' +
                "Q" + '\n' +
                "";//"EMC";
            int    length = s.Length;
            byte[] stream = new byte[length];
            for (int idx = 0; idx < length; idx++)
            {
                stream[idx] = (byte)s[idx];
            }
            xobj.CreateStream(stream);

            // Get existing or create new appearance dictionary
            PdfDictionary ap = Elements[PdfAnnotation.Keys.AP] as PdfDictionary;
            if (ap == null)
            {
                ap = new PdfDictionary(_document);
                Elements[PdfAnnotation.Keys.AP] = ap;
            }

            // Set XRef to normal state
            ap.Elements["/N"] = xobj.Reference;



            //// HACK
            //string m =
            //"<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>" + '\n' +
            //"<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"Adobe XMP Core 4.0-c321 44.398116, Tue Aug 04 2009 14:24:39\"> " + '\n' +
            //"   <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"> " + '\n' +
            //"      <rdf:Description rdf:about=\"\" " + '\n' +
            //"            xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\"> " + '\n' +
            //"         <pdf:Producer>PDFsharp 1.40.2150-g (www.PdfSharp.com) (Original: Powered By Crystal)</pdf:Producer> " + '\n' +
            //"      </rdf:Description> " + '\n' +
            //"      <rdf:Description rdf:about=\"\" " + '\n' +
            //"            xmlns:xap=\"http://ns.adobe.com/xap/1.0/\"> " + '\n' +
            //"         <xap:ModifyDate>2011-07-11T23:15:09+02:00</xap:ModifyDate> " + '\n' +
            //"         <xap:CreateDate>2011-05-19T16:26:51+03:00</xap:CreateDate> " + '\n' +
            //"         <xap:MetadataDate>2011-07-11T23:15:09+02:00</xap:MetadataDate> " + '\n' +
            //"         <xap:CreatorTool>Crystal Reports</xap:CreatorTool> " + '\n' +
            //"      </rdf:Description> " + '\n' +
            //"      <rdf:Description rdf:about=\"\" " + '\n' +
            //"            xmlns:dc=\"http://purl.org/dc/elements/1.1/\"> " + '\n' +
            //"         <dc:format>application/pdf</dc:format> " + '\n' +
            //"      </rdf:Description> " + '\n' +
            //"      <rdf:Description rdf:about=\"\" " + '\n' +
            //"            xmlns:xapMM=\"http://ns.adobe.com/xap/1.0/mm/\"> " + '\n' +
            //"         <xapMM:DocumentID>uuid:68249d89-baed-4384-9a2d-fbf8ace75c45</xapMM:DocumentID> " + '\n' +
            //"         <xapMM:InstanceID>uuid:3d5f2f46-c140-416f-baf2-7f9c970cef1d</xapMM:InstanceID> " + '\n' +
            //"      </rdf:Description> " + '\n' +
            //"   </rdf:RDF> " + '\n' +
            //"</x:xmpmeta> " + '\n' +
            //"                                                                          " + '\n' +
            //"                                                                          " + '\n' +
            //"                                                                          " + '\n' +
            //"                                                                          " + '\n' +
            //"                                                                          " + '\n' +
            //"                                                                          " + '\n' +
            //"                                                                          " + '\n' +
            //"                                                                          " + '\n' +
            //"                                                                          " + '\n' +
            //"                                                                          " + '\n' +
            //"<?xpacket end=\"w\"?>";

            //PdfDictionary mdict = (PdfDictionary)_document.Internals.GetObject(new PdfObjectID(32));

            //length = m.Length;
            //stream = new byte[length];
            //for (int idx = 0; idx < length; idx++)
            //  stream[idx] = (byte)m[idx];

            //mdict.Stream.Value = stream;
#else
            PdfRectangle rect  = Elements.GetRectangle(PdfAnnotation.Keys.Rect);
            XForm        form  = new XForm(_document, rect.Size);
            XGraphics    gfx   = XGraphics.FromForm(form);
            XRect        xrect = (rect.ToXRect() - rect.Location);

            if (BackColor != XColor.Empty)
            {
                gfx.DrawRectangle(new XSolidBrush(BackColor), rect.ToXRect() - rect.Location);
            }
            // Draw Border
            if (!BorderColor.IsEmpty)
            {
                gfx.DrawRectangle(new XPen(BorderColor), rect.ToXRect() - rect.Location);
            }

            string text = Text;

            if (text.Length > 0)
            {
                xrect.Y      = xrect.Y + TopMargin;
                xrect.X      = xrect.X + LeftMargin;
                xrect.Width  = xrect.Width + RightMargin;
                xrect.Height = xrect.Height + BottomMargin;

                if ((Flags & PdfAcroFieldFlags.Comb) != 0 && MaxLength > 0)
                {
                    var combWidth = xrect.Width / MaxLength;
                    var format    = XStringFormats.TopLeft;
                    format.Comb      = true;
                    format.CombWidth = combWidth;
                    gfx.Save();
                    gfx.IntersectClip(xrect);
                    if (this.MultiLine)
                    {
                        XTextFormatter formatter = new XTextFormatter(gfx);
                        formatter.Text = text;

                        formatter.DrawString(Text, Font, new XSolidBrush(ForeColor), xrect, Alignment);
                    }
                    else
                    {
                        gfx.DrawString(text, Font, new XSolidBrush(ForeColor), xrect + new XPoint(0, 1.5), format);
                    }
                    gfx.Restore();
                }
                else
                {
                    XTextFormatter formatter = new XTextFormatter(gfx);
                    formatter.Text = text;

                    formatter.DrawString(text, Font, new XSolidBrush(ForeColor), rect.ToXRect() - rect.Location, Alignment);
                }
            }

            form.DrawingFinished();
            form.PdfForm.Elements.Add("/FormType", new PdfLiteral("1"));

            // Get existing or create new appearance dictionary.
            PdfDictionary ap = Elements[PdfAnnotation.Keys.AP] as PdfDictionary;
            if (ap == null)
            {
                ap = new PdfDictionary(_document);
                Elements[PdfAnnotation.Keys.AP] = ap;
            }

            // Set XRef to normal state
            ap.Elements["/N"] = PdfObject.DeepCopyClosure(Owner, form.PdfForm);

            var normalStateDict = ap.Elements.GetDictionary("/N");
            var resourceDict    = new PdfDictionary(Owner);
            resourceDict.Elements[PdfResources.Keys.ProcSet] = new PdfArray(Owner, new PdfName("/PDF"), new PdfName("/Text"));

            var defaultFormResources = Owner.AcroForm.Elements.GetDictionary(PdfAcroForm.Keys.DR);
            if (defaultFormResources != null && defaultFormResources.Elements.ContainsKey(PdfResources.Keys.Font))
            {
                var           fontResourceItem = XForm.GetFontResourceItem(Font.FamilyName, defaultFormResources);
                PdfDictionary fontDict         = new PdfDictionary(Owner);
                resourceDict.Elements[PdfResources.Keys.Font] = fontDict;
                fontDict.Elements[fontResourceItem.Key]       = fontResourceItem.Value;
            }

            normalStateDict.Elements.SetObject(PdfPage.Keys.Resources, resourceDict);

            PdfFormXObject xobj = form.PdfForm;
            if (xobj.Stream == null)
            {
                xobj.CreateStream(new byte[] { });
            }

            string s = xobj.Stream.ToString();
            // Thank you Adobe: Without putting the content in 'EMC brackets'
            // the text is not rendered by PDF Reader 9 or higher.
            s = "/Tx BMC\n" + s + "\nEMC";
            ap.Elements.GetDictionary("/N").Stream.Value = new RawEncoding().GetBytes(s);
#endif
        }