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; // Creates a PdfDocument. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Gets the first Page of the document. var page = pdfoutput.Pages.First(); // Adds a title. var titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d ); page.AddParagraph( "Add Paths", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Adds text. page.AddText( "This document contains rectangle and line paths.", new Point( 50, 55 ) ); // Adds a blue stroked Rectangle of 100x20, with a width of 5. var pen = Pen.Default.SetColor( Color.Blue ).SetThickness( 5 ); page.Graphics.DrawRectangle( pen, new Rectangle( 200, 90, 100, 20 ) ); // Adds a deepPink filled Rectangle of 60x60. var brush = Brushes.DeepPink; page.Graphics.DrawRectangle( brush, new Rectangle( 100, 150, 60, 60 ) ); // Adds a blue stroke + deepPink filled Rectangle of 40x100. page.Graphics.DrawRectangle( pen, brush, new Rectangle( 300, 150, 40, 100 ) ); // Adds 2 lines. var linePen = Pen.Default.SetColor( Color.Orange ).SetThickness( 3 ); page.Graphics.DrawLine( linePen, new Point( 110, 300 ), new Point( 225, 360 ) ); var linePen2 = Pen.Default.SetColor( Color.Green ).SetThickness( 6 ); page.Graphics.DrawLine( linePen2, new Point( 110, 320 ), new Point( 330, 320 ) ); // Saves the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } | |