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); }
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); }
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); }