i'm working with a pdf export function, and my problem is about change the font i'm using to a little piece of my document.
do i need to create a new object to write in bold? or can i change the font object already created? How can i do that?
i'm working with a pdf export function, and my problem is about change the font i'm using to a little piece of my document.
do i need to create a new object to write in bold? or can i change the font object already created? How can i do that?
You might want to look here to get an idea of the scope of the problem. Changing the text on a page programmatically is definitely doable, but it's not as straightforward as you might think. Here's an example. Let's say that your page has the text "five blue turtles" and you want 'blue' to be in a bold font. If you're lucky, the code in the PDF will look like this:
BT /F0 12 72 300 Td (five) 82 300 Td (blue) Td 92 300 (turtles) Td ET
which is begin text, set my font to F0, 12 point, move to (72, 300) draw "five"... end text
In this case, you'd have to insert the font change and set it back
BT /F0 12 72 300 Td (five) /F1 12 82 300 Td (blue) Td /F0 12 92 300 (turtles) Td ET
The trick is to know that when you find the word, you will know what the font is at that point.
It gets worse, though. What if your text is placed on the page like this:
BT /F0 12 72 300 Td (five blue turtles) Td ET
You need to find where that word "blue" would be placed on the page using the current metrics and font rendering characteristics and split the text up and turn it into what you see above. This is no mean feat, but hopefully the library you work with would give you tools for doing the metrics.
The next issue you'd have to deal with is font encodings - again, hopefully the library you're working with knows how to handle that because an 'a' in a string might not necessarily map to the glyph for an 'a', depending on the algorithm used for font subsetting (I'm looking at you, GhostScript).
Then you might have to deal with the fact that the font that you want to use isn't really available for you (either not in the file or it's a subset and not a complete font).
And none of this even touches on the part where PDF is a file-location dependent file format and you have to cope with that too.
EDIT Upon reading your comment to the other answer, it looks like you need to read the documentation for the library that you're using more thoroughly. You need to tell us what library your using and show us the code with which you're creating these objects.
For example, if you were using my PDF library (disclaimers: I work for Atalasoft, no it's not free, I wrote the library, I used to work on Acrobat, I'm not a lawyer), you'd be doing something like this:
private double MeasureLine(PdfTextLine line, PdfGeneratedDocument doc)
{
PdfFontResource font = doc.Resources.Fonts.Get(line.FontName);
if (font == null) throw new ArgumentException("line");
return font.Metrics.MeasureText(line.Text, line.FontSize).X;
}
PdfGeneratedDocument doc = new PdfGeneratedDocument();
PdfGeneratedPage page = doc.AddPage(PdfDefaultPages.Letter);
string plainFontResName = doc.Resources.Fonts.Add("Arial");
string boldFontResName = doc.Resources.Fonts.Add("Arial");
PdfPoint where = new PdfPoint(72, 300);
PdfTextLine fiveline = new PdfTextLine("five ", where);
fiveline.FontName = plainFontResName; fiveline.FontSize = 12.0;
page.DrawingList.Add(fiveline);
where = new PdfPoint(where.X + MeasureText(fiveline), where.Y);
PdfTextLine blueline = new PdfTextLine("blue ", where);
blueline.FontName = boldFontResName; blueline.FontSize = 12.0;
page.DrawingList.Add(blueline);
where = new PdfPoint(where.X + MeasureText(blueline), where.Y);
PdfTextLine turtlesline = new PdfTextLine("turtles", where);
turtlesline.FontName = plainFontResName; turtlesline.FontSize = 12.0;
page.DrawingList.Add(turtlesline);
doc.Save("somepdf.pdf");
And in reality, you'd probably want to use a higher-level shape object like PdfStyledTextBox or create your own that figures does the font style changes for you.