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); }
/// <summary> /// Draws a sample box. /// </summary> public void BeginBox(XGraphics gfx, int number, string title) { const int dEllipse = 15; XRect rect = new XRect(0, 20, 300, 200); if (number % 2 == 0) { rect.X = 300 - 5; } rect.Y = 40 + ((number - 1) / 2) * (200 - 5); rect.Inflate(-10, -10); XRect rect2 = rect; rect2.Offset(this.borderWidth, this.borderWidth); gfx.DrawRoundedRectangle(new XSolidBrush(this.shadowColor), rect2, new XSize(dEllipse + 8, dEllipse + 8)); XLinearGradientBrush brush = new XLinearGradientBrush(rect, this.backColor, this.backColor2, XLinearGradientMode.Vertical); gfx.DrawRoundedRectangle(this.borderPen, brush, rect, new XSize(dEllipse, dEllipse)); rect.Inflate(-5, -5); XFont font = new XFont("Verdana", 12, XFontStyle.Regular); gfx.DrawString(title, font, XBrushes.Navy, rect, XStringFormats.TopCenter()); rect.Inflate(-10, -5); rect.Y += 20; rect.Height -= 20; //gfx.DrawRectangle(XPens.Red, rect); this.state = gfx.Save(); gfx.TranslateTransform(rect.X, rect.Y); }
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> /// Draws the page title and footer. /// </summary> public void DrawTitle(PdfPage page, XGraphics gfx, string title) { XRect rect = new XRect(new XPoint(), gfx.PageSize); rect.Inflate(-10, -15); XFont font = new XFont("Verdana", 14, XFontStyle.Bold); gfx.DrawString(title, font, XBrushes.MidnightBlue, rect, XStringFormats.TopCenter()); rect.Offset(0, 5); font = new XFont("Verdana", 8, XFontStyle.Italic); XStringFormat format = new XStringFormat(); format.Alignment = XStringAlignment.Near; format.LineAlignment = XLineAlignment.Far; gfx.DrawString("Created with " + PdfSharp.ProductVersionInfo.Producer, font, XBrushes.DarkOrchid, rect, format); font = new XFont("Verdana", 8); format.Alignment = XStringAlignment.Center; gfx.DrawString(Program.s_document.PageCount.ToString(), font, XBrushes.DarkOrchid, rect, format); Program.s_document.Outlines.Add(title, page, true); }
static void Main() { // Create a new PDF document PdfDocument document = new PdfDocument(); // Create a font XFont font = new XFont("Verdana", 16); // Create first page PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); gfx.DrawString("Page 1", font, XBrushes.Black, 20, 50, XStringFormats.Default()); // Create the root bookmark. You can set the style and the color. PdfOutline outline = document.Outlines.Add("Root", page, true, PdfOutlineStyle.Bold, XColors.Red); // Create some more pages for (int idx = 2; idx <= 5; idx++) { page = document.AddPage(); gfx = XGraphics.FromPdfPage(page); string text = "Page " + idx; gfx.DrawString(text, font, XBrushes.Black, 20, 50, XStringFormats.Default()); // Create a sub bookmark outline.Outlines.Add(text, page, true); } // Save the document... const string filename = "Bookmarks_tempfile.pdf"; 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 }
static void Main() { const string watermark = "PDFsharp"; const int emSize = 150; // Get a fresh copy of the sample PDF file const string filename = "Portable Document Format.pdf"; File.Copy(Path.Combine("d:/WImageTest/PDFs/", filename), Path.Combine(Directory.GetCurrentDirectory(), filename), true); // Create the font for drawing the watermark XFont font = new XFont("Times New Roman", emSize, XFontStyle.BoldItalic); // Open an existing document for editing and loop through its pages PdfDocument document = PdfReader.Open(filename); // Set version to PDF 1.4 (Acrobat 5) because we use transparency. if (document.Version < 14) { document.Version = 14; } for (int idx = 0; idx < document.Pages.Count; idx++) { //if (idx == 1) break; PdfPage page = document.Pages[idx]; switch (idx % 3) { case 0: { // Variation 1: Draw watermark as text string // Get an XGraphics object for drawing beneath the existing content XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend); #if true_ // Fill background with linear gradient color XRect rect = page.MediaBox.ToXRect(); XLinearGradientBrush gbrush = new XLinearGradientBrush(rect, XColors.LightSalmon, XColors.WhiteSmoke, XLinearGradientMode.Vertical); gfx.DrawRectangle(gbrush, rect); #endif // Get the size (in point) of the text XSize size = gfx.MeasureString(watermark, font); // Define a rotation transformation at the center of the page gfx.TranslateTransform(page.Width / 2, page.Height / 2); gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI); gfx.TranslateTransform(-page.Width / 2, -page.Height / 2); // Create a string format XStringFormat format = new XStringFormat(); format.Alignment = XStringAlignment.Near; format.LineAlignment = XLineAlignment.Near; // Create a dimmed red brush XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0)); // Draw the string gfx.DrawString(watermark, font, brush, new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2), format); } break; case 1: { // Variation 2: Draw watermark as outlined graphical path // Get an XGraphics object for drawing beneath the existing content XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend); // Get the size (in point) of the text XSize size = gfx.MeasureString(watermark, font); // Define a rotation transformation at the center of the page gfx.TranslateTransform(page.Width / 2, page.Height / 2); gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI); gfx.TranslateTransform(-page.Width / 2, -page.Height / 2); // Create a graphical path XGraphicsPath path = new XGraphicsPath(); // Add the text to the path path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150, new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2), XStringFormats.Default()); // Create a dimmed red pen XPen pen = new XPen(XColor.FromArgb(128, 255, 0, 0), 2); // Stroke the outline of the path gfx.DrawPath(pen, path); } break; case 2: { // Variation 3: Draw watermark as transparent graphical path above text // Get an XGraphics object for drawing above the existing content XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append); // Get the size (in point) of the text XSize size = gfx.MeasureString(watermark, font); // Define a rotation transformation at the center of the page gfx.TranslateTransform(page.Width / 2, page.Height / 2); gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI); gfx.TranslateTransform(-page.Width / 2, -page.Height / 2); // Create a graphical path XGraphicsPath path = new XGraphicsPath(); // Add the text to the path path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150, new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2), XStringFormats.Default()); // Create a dimmed red pen and brush XPen pen = new XPen(XColor.FromArgb(50, 75, 0, 130), 3); XBrush brush = new XSolidBrush(XColor.FromArgb(50, 106, 90, 205)); // Stroke the outline of the path gfx.DrawPath(pen, brush, path); } break; } } // Save the document... document.Save(filename); // ...and start a viewer Process.Start(filename); }
/// <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", 14); // Create a page PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); // Create a PDF text annotation PdfTextAnnotation textAnnot = new PdfTextAnnotation(); textAnnot.Title = "This is the title"; textAnnot.Subject = "This is the subject"; textAnnot.Contents = "This is the contents of the annotation.\rThis is the 2nd line."; textAnnot.Icon = PdfTextAnnotationIcon.Note; gfx.DrawString("The first text annotation", font, XBrushes.Black, 30, 50, XStringFormats.Default()); // Convert rectangle form world space to page space. This is necessary because the annotation is // placed relative to the bottom left corner of the page with units measured in point. XRect rect = gfx.Transformer.WorldToDefaultPage(new XRect(new XPoint(30, 60), new XSize(30, 30))); textAnnot.Rectangle = new PdfRectangle(rect); // Add the annotation to the page page.Annotations.Add(textAnnot); // Create another PDF text annotation which is open and transparent textAnnot = new PdfTextAnnotation(); textAnnot.Title = "Annotation 2 (title)"; textAnnot.Subject = "Annotation 2 (subject)"; textAnnot.Contents = "This is the contents of the 2nd annotation."; textAnnot.Icon = PdfTextAnnotationIcon.Help; textAnnot.Color = XColors.LimeGreen; textAnnot.Opacity = 0.5; textAnnot.Open = true; gfx.DrawString("The second text annotation (opened)", font, XBrushes.Black, 30, 140, XStringFormats.Default()); rect = gfx.Transformer.WorldToDefaultPage(new XRect(new XPoint(30, 150), new XSize(30, 30))); textAnnot.Rectangle = new PdfRectangle(rect); // Add the 2nd annotation to the page page.Annotations.Add(textAnnot); // Create a so called rubber stamp annotion. I'm not sure if it is useful, but at least // it looks impressive... PdfRubberStampAnnotation rsAnnot = new PdfRubberStampAnnotation(); rsAnnot.Icon = PdfRubberStampAnnotationIcon.TopSecret; rsAnnot.Flags = PdfAnnotationFlags.ReadOnly; rect = gfx.Transformer.WorldToDefaultPage(new XRect(new XPoint(100, 400), new XSize(350, 150))); rsAnnot.Rectangle = new PdfRectangle(rect); // Add the rubber stamp annotation to the page page.Annotations.Add(rsAnnot); // PDF supports some more pretty types of annotations like PdfLineAnnotation, PdfSquareAnnotation, // PdfCircleAnnotation, PdfMarkupAnnotation (with the subtypes PdfHighlightAnnotation, PdfUnderlineAnnotation, // PdfStrikeOutAnnotation, and PdfSquigglyAnnotation), PdfSoundAnnotation, or PdfMovieAnnotation. // If you need one of them, feel encouraged to implement it. It is quite easy. // Save the document... const string filename = "Annotations_tempfile.pdf"; document.Save(filename); // ...and start a viewer. Process.Start(filename); }
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); }
internal void RenderText(BarCodeRenderInfo info) { if (info.Font == null) { info.Font = new XFont("Courier New", Size.Height / 6); } XPoint center = info.Position + CalcDistance(Anchor, AnchorType.TopLeft, Size); switch (TextLocation) { case TextLocation.Above: center = new XPoint(center.X, center.Y - info.Gfx.MeasureString(Text, info.Font).Height); info.Gfx.DrawString(Text, info.Font, info.Brush, new XRect(center, Size), XStringFormats.TopCenter()); break; case TextLocation.AboveEmbedded: info.Gfx.DrawString(Text, info.Font, info.Brush, new XRect(center, Size), XStringFormats.TopCenter()); break; case TextLocation.Below: center = new XPoint(center.X, info.Gfx.MeasureString(Text, info.Font).Height + center.Y); info.Gfx.DrawString(Text, info.Font, info.Brush, new XRect(center, Size), XStringFormats.BottomCenter()); break; case TextLocation.BelowEmbedded: info.Gfx.DrawString(Text, info.Font, info.Brush, new XRect(center, Size), XStringFormats.BottomCenter()); break; } }