The following example demonstrates how to add paths on a page of a PDF document. It covers both how to draw rectangles & how to draw lines, as well as how to add a fill color, a stroke color & set the stroke's width.
| Add paths on a page (C#) |
Copy Code |
|---|---|
public static void AddPaths() { Console.WriteLine( "=== ADD PATHS ===" ); var outputFileName = "AddPaths.pdf"; var outputPath = PathsSample.PathsSampleOutputDirectory + outputFileName; // Create a pdf document. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Get the first page of the document. var page = pdfoutput.Pages.First(); // Add title. var titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d ); page.AddParagraph( "Add Paths", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Add Text. page.AddText( "This document contains rectangle and line paths.", new Point( 50, 55 ) ); // Add a blue stroked rectangle of 100x20, with a width of 5. var pen = Pen.Default.SetStrokeColor( Color.Blue ).SetStrokeWidth( 5 ); page.Graphics.DrawRectangle( pen, new Rectangle( 200, 90, 100, 20 ) ); // Add a deepPink filled rectangle of 60x60. var brush = Brushes.DeepPink; page.Graphics.DrawRectangle( brush, new Rectangle( 100, 150, 60, 60 ) ); // Add a blue stroke + deepPink filled rectangle of 40x100. page.Graphics.DrawRectangle( pen, brush, new Rectangle( 300, 150, 40, 100 ) ); // Add 2 Lines. var linePen = Pen.Default.SetStrokeColor( Color.Orange ).SetStrokeWidth( 3 ); page.Graphics.DrawLine( linePen, new Point( 110, 300 ), new Point( 225, 360 ) ); var linePen2 = Pen.Default.SetStrokeColor( Color.Green ).SetStrokeWidth( 6 ); page.Graphics.DrawLine( linePen2, new Point( 110, 320 ), new Point( 330, 320 ) ); // Save the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } | |