internal override void PrepareForSave() { if (Rectangle.X1 + Rectangle.X2 + Rectangle.Y1 + Rectangle.Y2 == 0) { return; } if (this.AppearanceHandler == null) { return; } PdfRectangle rect = Elements.GetRectangle(PdfAnnotation.Keys.Rect); XForm form = new XForm(this._document, rect.Size); XGraphics gfx = XGraphics.FromForm(form); this.AppearanceHandler.DrawAppearance(gfx, rect.ToXRect()); form.DrawingFinished(); // Get existing or create new appearance dictionary PdfDictionary ap = Elements[PdfAnnotation.Keys.AP] as PdfDictionary; if (ap == null) { ap = new PdfDictionary(this._document); Elements[PdfAnnotation.Keys.AP] = ap; } // Set XRef to normal state ap.Elements["/N"] = form.PdfForm.Reference; }
/// <summary> /// Creates the normal appearance form X object for the annotation that represents /// this acro form text field. /// </summary> void RenderAppearance() { PdfRectangle rect = Elements.GetRectangle(PdfAnnotation.Keys.Rect); XForm form = new XForm(this.document, rect.Size); XGraphics gfx = XGraphics.FromForm(form); if (backColor != XColor.Empty) { gfx.DrawRectangle(new XSolidBrush(BackColor), rect.ToXRect() - rect.Location); } string text = Text; if (text.Length > 0) { gfx.DrawString(Text, Font, new XSolidBrush(ForeColor), rect.ToXRect() - rect.Location + new XPoint(2, 0), XStringFormats.TopLeft); } form.DrawingFinished(); // Get existing or create new appearance dictionary PdfDictionary ap = Elements[PdfAnnotation.Keys.AP] as PdfDictionary; if (ap == null) { ap = new PdfDictionary(this.document); Elements[PdfAnnotation.Keys.AP] = ap; } // Set XRef to normal state ap.Elements["/N"] = form.PdfForm.Reference; }
void RenderAppearance() { for (var i = 0; i < Annotations.Elements.Count; i++) { var widget = Annotations.Elements[i]; if (widget == null) { continue; } var rect = widget.Rectangle; var xRect = new XRect(0, 0, rect.Width, rect.Height); var form = new XForm(_document, xRect); EnsureFonts(form); using (var gfx = XGraphics.FromForm(form)) { if (widget.BackColor != XColor.Empty) { gfx.DrawRectangle(new XSolidBrush(widget.BackColor), xRect); } // Draw Border if (!widget.BorderColor.IsEmpty) { gfx.DrawRectangle(new XPen(widget.BorderColor), xRect); } var index = SelectedIndex; if (index > 0) { var text = ValueInOptArray(index, false); if (!String.IsNullOrEmpty(text)) { var format = TextAlign == TextAlignment.Left ? XStringFormats.CenterLeft : TextAlign == TextAlignment.Center ? XStringFormats.Center : XStringFormats.CenterRight; gfx.DrawString(text, Font, new XSolidBrush(ForeColor), xRect, format); } } form.DrawingFinished(); var ap = new PdfDictionary(this._document); widget.Elements[PdfAnnotation.Keys.AP] = ap; widget.Elements.SetName(PdfAnnotation.Keys.AS, "/N"); // set appearance state // Set XRef to normal state ap.Elements["/N"] = form.PdfForm.Reference; var xobj = form.PdfForm; var s = xobj.Stream.ToString(); s = "/Tx BMC\n" + s + "\nEMC"; xobj.Stream.Value = new RawEncoding().GetBytes(s); } } }
public override void RenderPage(XGraphics gfx) { #if true_ // Create a new PDF document //PdfDocument document = new PdfDocument(); // Create a font XFont font = new XFont("Verdana", 16); // Create a new page //PdfPage page = document.AddPage(); //XGraphics gfx = XGraphics.FromPdfPage(page); //gfx.DrawString("XPdfForm Sample", font, XBrushes.DarkGray, 15, 25, XStringFormat.Default); // Step 1: Create an XForm and draw some graphics on it // Create an empty XForm object with the specified width and height // A form is bound to its target document when it is created. The reason is that the form can // share fonts and other objects with its target document. XForm form = new XForm(gfx, XUnit.FromMillimeter(70), XUnit.FromMillimeter(55)); // Create an XGraphics object for drawing the contents of the form. XGraphics formGfx = XGraphics.FromForm(form); // Draw a large transparent rectangle to visualize the area the form occupies XColor back = XColors.Orange; back.A = 0.2; XSolidBrush brush = new XSolidBrush(back); formGfx.DrawRectangle(brush, -10000, -10000, 20000, 20000); // On a form you can draw... //// ... text //formGfx.DrawString("Text, Graphics, Images, and Forms", new XFont("Verdana", 10, XFontStyle.Regular), XBrushes.Navy, 3, 0, XStringFormat.TopLeft); //XPen pen = XPens.LightBlue.Clone(); //pen.Width = 2.5; // ... graphics like Bézier curves //formGfx.DrawBeziers(pen, XPoint.ParsePoints("30,120 80,20, 100,140 175,33.3")); //// ... raster images like GIF files //XGraphicsState state = formGfx.Save(); //formGfx.RotateAtTransform(17, new XPoint(30, 30)); //formGfx.DrawImage(XImage.FromFile("../../../../XGraphicsLab/images/Test.gif"), 20, 20); //formGfx.Restore(state); //// ... and forms like XPdfForm objects //state = formGfx.Save(); //formGfx.RotateAtTransform(-8, new XPoint(165, 115)); //formGfx.DrawImage(XPdfForm.FromFile("../../../../PDFs/SomeLayout.pdf"), new XRect(140, 80, 50, 50 * Math.Sqrt(2))); //formGfx.Restore(state); // When you finished drawing on the form, dispose the XGraphic object. formGfx.Dispose(); // Step 2: Draw the XPdfForm on your PDF page like an image // Draw the form on the page of the document in its original size gfx.DrawImage(form, 20, 50); #if true_ // Draw it stretched gfx.DrawImage(form, 300, 100, 250, 40); // Draw and rotate it int d = 25; for (int idx = 0; idx < 360; idx += d) { gfx.DrawImage(form, 300, 480, 200, 200); gfx.RotateAtTransform(d, new XPoint(300, 480)); } #endif //// Save the document... //string filename = "XForms.pdf"; //document.Save(filename); //// ...and start a viewer. //Process.Start(filename); #else //base.RenderPage(gfx); int cx = 300; int cy = 240; XForm form; //if (gfx.PdfPage == null) form = new XForm(gfx, cx, cy); //else // form = new XForm(gfx.PdfPage.Owner, cx, cy); double dx = gfx.PageSize.Width; double dy = gfx.PageSize.Height; XGraphics formgfx = XGraphics.FromForm(form); XSolidBrush brush = new XSolidBrush(XColor.FromArgb(128, 0, 255, 255)); formgfx.DrawRectangle(brush, -1000, -1000, 2000, 2000); formgfx.DrawLine(XPens.Red, 0, 0, cx, cy); formgfx.DrawLine(XPens.Red, cx, 0, 0, cy); XFont font = new XFont("Times", 16, XFontStyle.BoldItalic); formgfx.DrawString("Text", font, XBrushes.DarkOrange, 0, 0, XStringFormats.TopLeft); formgfx.DrawString("Text", font, XBrushes.DarkOrange, new XRect(0, 0, cx, cy), XStringFormats.Center); // Required to finish drawing the form. Both cases are correct. #if true formgfx.Dispose(); #else form.DrawingFinished(); #endif gfx.DrawImage(form, 50, 50); #if true_ gfx.TranslateTransform(dx / 2, dy / 2); gfx.RotateTransform(-25); gfx.TranslateTransform(-dx / 2, -dy / 2); gfx.DrawImage(form, (dx - form.Width) / 2, (dy - form.Height) / 2, form.Width, form.Height); #endif #endif }
/// <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 }
/// <summary> /// Creates the normal appearance form X object for the annotation that represents /// this acro form text field. /// </summary> void RenderAppearance() { #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.PdfSharpCore.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); if (_backColor != XColor.Empty) { gfx.DrawRectangle(new XSolidBrush(BackColor), rect.ToXRect() - rect.Location); } string text = Text; if (text.Length > 0) { gfx.DrawString(Text, Font, new XSolidBrush(ForeColor), rect.ToXRect() - rect.Location + new XPoint(2, 0), XStringFormats.TopLeft); } 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"] = form.PdfForm.Reference; PdfFormXObject xobj = form.PdfForm; if (xobj.Stream == null) { xobj.CreateStream(Encoding.ASCII.GetBytes(text)); } 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"; xobj.Stream.Value = new RawEncoding().GetBytes(s); #endif }
void RenderAppearance() { var format = TextAlign == TextAlignment.Left ? XStringFormats.CenterLeft : TextAlign == TextAlignment.Center ? XStringFormats.Center : XStringFormats.CenterRight; for (var idx = 0; idx < Annotations.Elements.Count; idx++) { var widget = Annotations.Elements[idx]; if (widget == null) { continue; } var rect = widget.Rectangle; var xRect = new XRect(0, 0, rect.Width, rect.Height); var form = new XForm(_document, xRect); EnsureFonts(form); using (var gfx = XGraphics.FromForm(form)) { if (widget.BackColor != XColor.Empty) { gfx.DrawRectangle(new XSolidBrush(widget.BackColor), xRect); } // Draw Border if (!widget.BorderColor.IsEmpty) { gfx.DrawRectangle(new XPen(widget.BorderColor), xRect); } var lineHeight = Font.Size * 1.2; var y = 0.0; var startIndex = Math.Min(TopIndex, Options.Count - (int)(rect.Height / lineHeight)); for (var i = startIndex; i < Options.Count; i++) { var text = Options[i]; // offset and shrink a bit to not draw on top of the outer border var lineRect = new XRect(1, y + 1, rect.Width - 2, lineHeight - 1); var selected = false; if (text.Length > 0) { if (SelectedIndices.Contains(i)) { gfx.DrawRectangle(new XSolidBrush(HighlightColor), lineRect); selected = true; } lineRect.Inflate(-2, 0); gfx.DrawString(text, Font, new XSolidBrush(selected ? HighlightTextColor : ForeColor), lineRect, format); y += lineHeight; } } form.DrawingFinished(); var ap = new PdfDictionary(this._document); widget.Elements[PdfAnnotation.Keys.AP] = ap; // Set XRef to normal state ap.Elements["/N"] = form.PdfForm.Reference; widget.Elements.SetName(PdfAnnotation.Keys.AS, "/N"); // set appearance state // Set XRef to normal state ap.Elements["/N"] = form.PdfForm.Reference; var xobj = form.PdfForm; var s = xobj.Stream.ToString(); s = "/Tx BMC\n" + s + "\nEMC"; xobj.Stream.Value = new RawEncoding().GetBytes(s); } } }