Letters - UglyToad/PdfPig GitHub Wiki
Each glyph (letter) drawing operation in a PDF document is represented by the Letter
class in PdfPig.
To get the letters from the first page of a document you can use this code:
using UglyToad.PdfPig;
using UglyToad.PdfPig.Content;
using (var document = PdfDocument.Open("my-document.pdf"))
{
var page = document.GetPage(1);
IReadOnlyList<Letter> letters = page.Letters;
}
Each letter has 3 properties related to its size and position in the PDF document. The meaning of each of these can be slightly confusing.
For reference we will be using this image of a lower-case letter 'g' from a PDF.
Description for people unable to view the image. The letter has a descender, there is a red circle drawn on the left hand side of the image at about 33% of the vertical height from the bottom of the image, this represents the location (origin). The visible character which is shown in black is surrounded by a teal bordered rectangle, this is the glyph rectangle. A blue line runs horizontally from the red circle indicating the location, this line indicates the baseline and intersects the letter g at about 33% of its height, level with the center of the location circle.
The location is a point in 2D space (PdfPoint
where the Y axis goes upwards from zero) where the PDF was instructed to begin drawing the glyph. The visible region of each glyph is usually slightly horizontally to the right of the location depending on how the glyph was defined by the font's designer.
As shown in the image above the actual visible outline of the glyph does not have to intersect with the location and may extend below, above, right-of and in some cases to-the-left-of the location. The location is the most reliable measure of a glyph's location since it is the position the PDF uses and does not rely on interpreting the font file.
Accessed via letter.Location
.
This is the smallest rectangle (PdfRectangle
) which completely contains the visible glyph outline. As above in the letter 'g' the bottom of this rectangle may be below the location (the baseline is the blue horizontal line) and the may extend above it. The width of this rectangle is the actual width occupied by the glyph on-screen.
Accessed via letter.GlyphRectangle
.
This is the width (decimal
) the PDF is instructed to move after it has drawn this glyph, it can be greater, equal or less than the width of the Glyph Rectangle. Once the PDF has moved this width the resulting location is where it will draw the next character (place the next character's location) if another Show Text command is received. In the image above the width would probably be the distance from the location to the right hand side of the glyph's rectangle.
Accessed via letter.Width
.
Two more images are show below:
For this capital K the glyph rectangle sits on the blue baseline and does not extend below the baseline. The location is slightly to the left of the glyph rectangle.
For this lowercase o the glyph rectangle extends marginally below the baseline due to the curvature of the character. The location is yet again to the left of the left edge of the glyph rectangle.