Enhancing PDFs with Stroking in C# .NET

Stroking is an important operation when working with PDF documents. It allows you to highlight the contours of objects such as text and graphic elements, which makes the document more expressive and readable. In this article, we will look at how to implement a stroke in PDF using C# and the SautinSoft.Rdf .NET library.

  1. Add SautinSoft.PDF from NuGet.
  2. Create a new PDF document and add a page.
  3. Add text and set font formatting..
  4. Create a path and set the start and end points and stroke parameters (width and color).
  5. Save the document.

Output result:

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;
using SautinSoft.Pdf.Content;

class Program
{
    /// <summary>
    /// Stroking
    /// </summary>
    /// <remarks>
    /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/pdf-content-formatting-stroking.php
    /// </remarks>
    static void Main()
    {
        // Before starting this example, please get a free 100-day trial key:
        // https://sautinsoft.com/start-for-free/

        // Apply the key here:
        // PdfDocument.SetLicense("...");

        using (var document = new PdfDocument())
        {
            var page = document.Pages.Add();

            // PdfFormattedText currently doesn't support stroking, so we will stroke its drawn output.
            using (var formattedText = new PdfFormattedText())
            {
                formattedText.Font = new PdfFont("Helvetica", 200);

                formattedText.Append("Hello!");

                page.Content.DrawText(formattedText, new PdfPoint(50, 600));
            }

            // Draw lines with different line joins.
            var path = page.Content.Elements.AddPath();
            path.BeginSubpath(50, 350).LineTo(100, 550).LineTo(150, 350);
            path.Format.Stroke.LineJoin = PdfLineJoin.Miter;

            path = page.Content.Elements.AddPath();
            path.BeginSubpath(200, 350).LineTo(250, 550).LineTo(300, 350);
            path.Format.Stroke.LineJoin = PdfLineJoin.Round;

            path = page.Content.Elements.AddPath();
            path.BeginSubpath(350, 350).LineTo(400, 550).LineTo(450, 350);
            path.Format.Stroke.LineJoin = PdfLineJoin.Bevel;

            // Create a dash pattern with 20 point dashes and 10 point gaps.
            var dashPattern = new PdfLineDashPattern(0, 20, 10);

            // Draw lines with different line caps.
            // Notice how the line cap is applied to each dash.
            path = page.Content.Elements.AddPath();
            path.BeginSubpath(50, 100).LineTo(100, 300).LineTo(150, 100);
            var format = path.Format;
            format.Stroke.LineCap = PdfLineCap.Butt;
            format.Stroke.DashPattern = dashPattern;

            path = page.Content.Elements.AddPath();
            path.BeginSubpath(200, 100).LineTo(250, 300).LineTo(300, 100);
            format = path.Format;
            format.Stroke.LineCap = PdfLineCap.Round;
            format.Stroke.DashPattern = dashPattern;

            path = page.Content.Elements.AddPath();
            path.BeginSubpath(350, 100).LineTo(400, 300).LineTo(450, 100);
            format = path.Format;
            format.Stroke.LineCap = PdfLineCap.Square;
            format.Stroke.DashPattern = dashPattern;

            // Do not fill any content and stroke all content with a 10 point width red outline that is 50% opaque.
            format = page.Content.Format;
            format.Fill.IsApplied = false;
            format.Stroke.IsApplied = true;
            format.Stroke.Width = 10;
            format.Stroke.Color = PdfColor.FromRgb(1, 0, 0);
            format.Stroke.Opacity = 0.5;

            // Add a line to visualize the differences between line joins.
            var line = page.Content.Elements.AddPath().BeginSubpath(25, 550).LineTo(475, 550);
            format = line.Format;
            format.Stroke.IsApplied = true;
            // A line width of 0 denotes the thinnest line that can be rendered at device resolution: 1 device pixel wide.
            format.Stroke.Width = 0;

            // Add a line to visualize the differences between line caps.
            line = page.Content.Elements.AddPath().BeginSubpath(25, 100).LineTo(475, 100);
            format = line.Format;
            format.Stroke.IsApplied = true;
            format.Stroke.Width = 0;

            document.Save("Stroking.pdf");
        }

        System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Stroking.pdf") { UseShellExecute = true });
    }
}

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.