static void Main() { // Create new document PdfDocument document = new PdfDocument(); // Set font encoding to unicode //XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always); XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode); XFont font = new XFont("Times New Roman", 12, XFontStyle.Regular, options); // Draw text in different languages for (int idx = 0; idx < texts.Length; idx++) { PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); XTextFormatter tf = new XTextFormatter(gfx); tf.Alignment = XParagraphAlignment.Left; tf.DrawString(texts[idx], font, XBrushes.Black, new XRect(100, 100, page.Width - 200, 600), XStringFormats.TopLeft()); } const string filename = "Unicode_tempfile.pdf"; // Save the document... document.Save(filename); // ...and start a viewer. Process.Start(filename); }
static void Main() { const string filename = "TextLayout_tempfile.pdf"; const string text = "Facin exeraessisit la consenim iureet dignibh eu facilluptat vercil dunt autpat. " + "Ecte magna faccum dolor sequisc iliquat, quat, quipiss equipit accummy niate magna " + "facil iure eraesequis am velit, quat atis dolore dolent luptat nulla adio odipissectet " + "lan venis do essequatio conulla facillandrem zzriusci bla ad minim inis nim velit eugait " + "aut aut lor at ilit ut nulla ate te eugait alit augiamet ad magnim iurem il eu feuissi.\n" + "Guer sequis duis eu feugait luptat lum adiamet, si tate dolore mod eu facidunt adignisl in " + "henim dolorem nulla faccum vel inis dolutpatum iusto od min ex euis adio exer sed del " + "dolor ing enit veniamcon vullutat praestrud molenis ciduisim doloborem ipit nulla consequisi.\n" + "Nos adit pratetu eriurem delestie del ut lumsandreet nis exerilisit wis nos alit venit praestrud " + "dolor sum volore facidui blaor erillaortis ad ea augue corem dunt nis iustinciduis euisi.\n" + "Ut ulputate volore min ut nulpute dolobor sequism olorperilit autatie modit wisl illuptat dolore " + "min ut in ute doloboreet ip ex et am dunt at."; PdfDocument document = new PdfDocument(); PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); XFont font = new XFont("Times New Roman", 10, XFontStyle.Bold); XTextFormatter tf = new XTextFormatter(gfx); XRect rect = new XRect(40, 100, 250, 232); gfx.DrawRectangle(XBrushes.SeaShell, rect); //tf.Alignment = ParagraphAlignment.Left; tf.DrawString(text, font, XBrushes.Black, rect, XStringFormats.TopLeft()); rect = new XRect(310, 100, 250, 232); gfx.DrawRectangle(XBrushes.SeaShell, rect); tf.Alignment = XParagraphAlignment.Right; tf.DrawString(text, font, XBrushes.Black, rect, XStringFormats.TopLeft()); rect = new XRect(40, 400, 250, 232); gfx.DrawRectangle(XBrushes.SeaShell, rect); tf.Alignment = XParagraphAlignment.Center; tf.DrawString(text, font, XBrushes.Black, rect, XStringFormats.TopLeft()); rect = new XRect(310, 400, 250, 232); gfx.DrawRectangle(XBrushes.SeaShell, rect); tf.Alignment = XParagraphAlignment.Justify; tf.DrawString(text, font, XBrushes.Black, rect, XStringFormats.TopLeft()); // Save the document... document.Save(filename); // ...and start a viewer. Process.Start(filename); }
/// <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.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); 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; 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 }
/// <summary> /// Draws the text. /// </summary> /// <param name="text">The text to be drawn.</param> /// <param name="font">The font.</param> /// <param name="brush">The text brush.</param> /// <param name="layoutRectangle">The layout rectangle.</param> public void DrawString(string text, XFont font, XBrush brush, XRect layoutRectangle) { DrawString(text, font, brush, layoutRectangle, XStringFormats.TopLeft()); }
static void Main() { // 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, XPageDirection.Downwards); gfx.DrawString("XPdfForm Sample", font, XBrushes.DarkGray, 15, 25, XStringFormats.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(document, 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, XStringFormats.TopLeft()); XPen pen = XPens.LightBlue.Clone(); pen.Width = 2.5; // ... graphics like Bezier 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("d:\\WImageTest\\test1.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("d:\\WImageTest\\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 const 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... const string filename = "XForms_tempfile.pdf"; document.Save(filename); // ...and start a viewer. Process.Start(filename); }