Creating PDFs in C# with Outlined or Invisible Text

by Andy C Thu, August 30 2012, 1:51 PM (US Eastern Time)

DynamicPDF Generator has lots of predefined page elements which allow you to easily create different types of content, but you may occasionally have a need to do something that is not available in the API by default. Custom page elements is an easy way to extend the API to achieve these results.

We get questions from time to time on how to add outlined text to a PDF. By outlined text I mean text that is not completely filled in with a color but rather is just an outline of the font and nothing filled in the middle of the text.  This is a great situation for using a Custom page element and the C# code below shows how to extend the TextArea page element to add this functionality:

using System;
using ceTe.DynamicPDF;
using ceTe.DynamicPDF.PageElements;

public class OutlinedTextArea : TextArea
{
    // Member variable to store Border Width
    private float borderWidth = 1.0f;

    // Constructors. Additional ones can be added to model TextArea
    public OutlinedTextArea(string text, float x, float y, float width, float height)
        : base(text, x, y, width, height) { }
    public OutlinedTextArea(string text, float x, float y, float width, float height,
        Font font, float fontSize)
        : base(text, x, y, width, height, font, fontSize) { }

    // Border Width Property added
    public float BorderWidth
    {
        get { return this.borderWidth; }
        set { this.borderWidth = value; }
    }

    // Override Draw Method
    public override void Draw(ceTe.DynamicPDF.IO.PageWriter writer)
    {
        // Enter Graphics Mode
        writer.SetGraphicsMode();
        // Set Stroke Color and Border Width
        writer.SetStrokeColor(base.TextColor);
        writer.SetLineWidth(this.borderWidth);
        // Enter Text Mode
        writer.SetTextMode();
        // Set Text Rendering Mode
        writer.SetTextRenderingMode(TextRenderingMode.Stroke);
        // Call Draw on base class
        base.Draw(writer);
        // Enter Text Mode (in case underline is set to true)
        writer.SetTextMode();
        // Set Text Rendering Mode Back to Fill
        writer.SetTextRenderingMode(TextRenderingMode.Fill);
    }
}

Here is the C# code to use the newly created page element:

// Create Document and add a Page
Document document = new Document();
Page page = new Page();
document.Pages.Add(page);

// Create the Custom Page Element and add it to the page 
OutlinedTextArea outlinedTextArea = new OutlinedTextArea("Outlined Text", 0f, 0f,
    200f, 30f, Font.HelveticaBold, 24f);
outlinedTextArea.TextColor = RgbColor.Red;
page.Elements.Add(outlinedTextArea);

// Save the PDF
document.Draw(@"MyPDF.pdf");

A similar technique can be used to create a PDF with invisible text. Invisible text is selectable on the PDF and will be extracted if text extraction is used. This is commonly used in OCR applications to make it possible to select and copy text from an image. The C# code to create an invisible text custom page element appears below:

using System;
using ceTe.DynamicPDF;
using ceTe.DynamicPDF.PageElements;

public class InvisibleTextArea : TextArea
{
    // Constructors. Additional ones can be added to model TextArea
    public InvisibleTextArea(string text, float x, float y, float width, float height)
        : base(text, x, y, width, height) { }
    public InvisibleTextArea(string text, float x, float y, float width, float height,
        Font font, float fontSize)
        : base(text, x, y, width, height, font, fontSize) { }

    // Override Draw Method
    public override void Draw(ceTe.DynamicPDF.IO.PageWriter writer)
    {
        // Enter Text Mode
        writer.SetTextMode();
        // Set Text Rendering Mode
        writer.SetTextRenderingMode(TextRenderingMode.Invisible);
        // Call Draw on base class
        base.Draw(writer);
        // Enter Text Mode (in case underline is set to true)
        writer.SetTextMode();
        // Set Text Rendering Mode Back to Fill
        writer.SetTextRenderingMode(TextRenderingMode.Fill);
    }
}

As seen from the code above, it is quite straight forward to extend existing page elements to achieve the desired output format if that is not available already. Custom page elements are very powerful and even allow you to write your own page elements from scratch.

Have you written any custom page elements that you would like to share? If you have any questions or comments, I would love to hear from you.

Tags: , , , , , , , , , , ,

Comments (2) -

Anil
1/2/2014 2:02:10 AM #

How can i get dlls of ceTe.DynamicPDF and  ceTe.DynamicPDF.PageElements, please do this favour as early as possible

Anil S
1/6/2014 2:25:01 PM #

Hello Anil,

The two namespaces you are looking for are available in the DynamicPDF Core Suite for .NET product: http://www.dynamicpdf.com/PDF-Suite-.NET.aspx. Here is the download link: www.dynamicpdf.com/.../Download.csp. After you download and install the product, please refer to the following link: www.dynamicpdf.com/.../...ng%20the%20Assembly.html to determine which dll to reference in your project.

Add comment

biuquote
  • Comment
  • Preview
Loading