Recursive AI

Add an inline icon inside a PDF paragraph in C#

OnePDF is a free, open-source PDF library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Use InlineCanvas() to draw an icon that moves with the words when a paragraph wraps.

Terminal
dotnet add package RecursiveAI.OnePdf.Layout

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OnePdf.Layout;
 
LayoutDocument.Compose(doc => doc.Page(p =>
{
    p.Size(216, 144);
    p.Margin(24);
    p.Content().Text(t =>
    {
        t.Span("Delivery status: ");
        t.InlineCanvas(9, 9, s =>
        {
            s.Builder.SetFillRgb(0, 0.5, 0);
            s.Builder.Ellipse(0, 0, 9, 9);
            s.Builder.Fill();
        }).Artifact().AlignMiddle();
        t.Span(" Shipped. Tracking is ready.");
    });
})).Save("inline.pdf");
Console.WriteLine("Wrote inline.pdf");

Unzip the example, then dotnet run.

Text
Wrote inline.pdf

Open inline.pdf: a small green dot sits between “Delivery status:” and “Shipped.” The rest of the sentence wraps onto the next line. The code draws the dot, so there’s no image file to supply. If you make the icon wider than the line, wrapping won’t squeeze it: the icon overflows on a line of its own.

Variations

Keep the icon with the surrounding words. Change the end of the InlineCanvas call to:

C#
}).Artifact().AlignMiddle().KeepWithNeighbours();

Give a meaningful icon a description. If the dot itself conveys a status that the words don’t say, use this instead of Artifact():

C#
}).AltText("Shipped").AlignMiddle();

Remove the duplicate “Shipped.” from the next span if you use the icon in place of that word.

Use a PNG or JPEG icon file. Replace the InlineCanvas call with this snippet. It uses status.png, a green check mark included in the download; you can substitute your own PNG or JPEG.

C#
byte[] icon = File.ReadAllBytes("status.png");
t.InlineImage(icon, 9, 9)
    .Artifact().AlignMiddle();
Text
Wrote inline.pdf

The check mark appears in place of the dot, between “Delivery status:” and “Shipped.”

The short answer

Call InlineCanvas between two Span calls in Text(t => ...) when you generate the paragraph. The two size arguments are in PDF points, and the canvas draws in its own small box. You set coordinates inside that box, not on the page. For an existing PNG or JPEG icon, use InlineImage in the same place. Neither call inserts an icon into a paragraph in an already saved PDF.

How do I put an icon and text on the same line?

Put both in one paragraph. A separate image block may sit above or below the text, and an image drawn at an absolute position won’t move when a longer label makes the paragraph wrap. Here the dot is a single, indivisible item in the text. A line can break before or after it unless you call KeepWithNeighbours(). That’s useful if the icon acts as a marker for the next word. Check the result in your narrowest column: keeping more content together leaves the layout fewer places to break the line.

Use AlignMiddle() to center the drawing against the text, not against the whole paragraph. With the default baseline alignment, the bottom edge of the canvas sits on the text baseline. A tall icon can make its line taller than neighboring lines, even if the surrounding font size is unchanged. Start with an icon close to the height of the letters, then adjust its size for the font you use.

How do I add alt text to an icon in a PDF?

Use AltText when the picture adds information. In a tagged document, OnePDF records the icon as a figure inside the paragraph and uses the description in text extraction. When the text already says “Shipped,” the green dot only repeats the status. Marking it with Artifact() keeps it out of the tag tree and text extraction; the dot still draws. Marking up an individual icon doesn’t by itself make the whole document accessible.

All onepdf recipes