How to convert text to outlines?

164 views
Skip to first unread message

catinahat

unread,
Sep 21, 2009, 2:58:56 PM9/21/09
to PDFTron PDFNet SDK
We sometimes have the request to produce PDFs with all text converted
to vector outlines. How would one go about achieving this?

Thanks!!

trn2

unread,
Sep 21, 2009, 5:47:52 PM9/21/09
to PDFTron PDFNet SDK
Q: We sometimes have the request to produce PDFs with all text
converted
to vector outlines. How would one go about achieving this?

-----------
A: You can obtain glyph outlines for every charcode used within a
document using Font.GetGlyphPath() method.

For example, assuming that an 'e_text_begin' element is encountered,
you can obtain glyph outlines for each charcode in the text block as
follows:

// Please refer to ElementEdit and ElementReaderAdv -
http://www.pdftron.com/pdfnet/samplecode.html#ElementEdit
// as a context for the following pseudo-code:

void ProcessText(ElementReader page_reader, ElementWriter writer)
{
Element element;
while ((element = page_reader.Next()) != null) {
switch (element.GetType()) {
case Element::e_text_end:
return;
case Element::e_text:
CharIterator itr = element.GetCharIterator();
GState gs = element.GetGState();
double font_size = gs.GetFontSize();
pdftron.PDF.Font font = gs.GetFont();
double horiz_spacing = gs.GetHorizontalScale() / 100.0;
Matrix2D text_mtx = element.GetTextMatrix();
Matrix2D pos = new Matrix2D(1, 0, 0, 1, 0, 0);
Matrix2D font_mtx = new Matrix2D(font_size * horiz_spacing, 0,
0, font_size, 0, 0);

double units_per_em = font.GetUnitsPerEm();
font_mtx *= Matrix2D(1.0/units_per_em, 0, 0, -1.0/
units_per_em, 0, 0);

for (; itr.HasNext(); itr.Next() {
pos.m_h = itr.Current().x;
pos.m_v = itr.Current().y;
Matrix2D path_mtx(text_mtx * pos * font_mtx);
font.GetGlyphPath(itr.Current().char_code, path_oprs,
path_data, true, (ref) path_mtx);

// Now, write glyph outline instead of a text run...
... construct a path element using ElementBuilder :
b.PathBegin(); b.MoveTo(); b.LineTo();
... path_element = b.PathEnd();
... See ElementBuilder (http://www.pdftron.com/pdfnet/
samplecode.html#ElementBuilder)
writer.WriteElement(path_element);
}
}
}
}

The meaning of data stored in 'path_oprs' and 'path_data' is the same
as for the path element (returned by Element.GetPathTypes()/
GetPathPoints(); for a concrete example of how to extract the outlines
out of these arrays, please take a look at ElementReaderAdv (http://
www.pdftron.com/net/index.html#ElementReaderAdv) sample project.
Please note that Font.GetGlyphPath() will work regardless of whether
the font embedded, is subsetted or is missing.

catinahat

unread,
Sep 22, 2009, 9:56:06 AM9/22/09
to PDFTron PDFNet SDK
Thanks for the quick, spot-on reply!!



On Sep 21, 5:47 pm, trn2 <supp...@pdftron.com> wrote:
> Q: We sometimes have the request to produce PDFs with all text
> converted
> to vector outlines. How would one go about achieving this?
>
> -----------
> A: You can obtain glyph outlines for every charcode used within a
> document using Font.GetGlyphPath() method.
>
> For example, assuming that an 'e_text_begin' element is encountered,
> you can obtain glyph outlines for each charcode in the text block as
> follows:
>
> // Please refer to ElementEdit and ElementReaderAdv -http://www.pdftron.com/pdfnet/samplecode.html#ElementEdit
> out of these arrays, please take a look at ElementReaderAdv (http://www.pdftron.com/net/index.html#ElementReaderAdv) sample project.
Reply all
Reply to author
Forward
0 new messages