-1

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?

Sled
  • 18,541
  • 27
  • 119
  • 168
Cleiton Ribeiro
  • 359
  • 2
  • 5
  • 16
  • But in that topic, the question was another... now, i wanna know how can i change something in a object already created. – Cleiton Ribeiro Jan 23 '14 at 14:17
  • 1
    This is a reword of the same question you asked before. Generating a PDF file first and then trying to modify it is using a way too big hammer. You need to provide more detail regarding how you are using whatever library you are using to generate the PDF in the first place so that you can generate it properly. Please spend *much more* time on wording your question correctly and providing background information - someone might actually be able to help you then. – David van Driessche Jan 23 '14 at 17:47

1 Answers1

2

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.

Community
  • 1
  • 1
plinth
  • 48,267
  • 11
  • 78
  • 120
  • +1; and stressing **If you're lucky...** – mkl Jan 23 '14 at 14:44
  • @CleitonRibeiro: What exactly made you think this code changes the font color? I.e., if the sample text changes to "five **big** turtles", do you believe that would change the font *size*? (OTOH, plinth *does* select the same font for both plain and 'bold' text.) – Jongware Jan 23 '14 at 15:51
  • doesn't work for me... i've try: Print #1, "BT /F1 12 10 746 Td(titulo)Td ET" but nothing happens. – Cleiton Ribeiro Jan 23 '14 at 15:52
  • :D sorry, i was thinking that numbers represent the RGB codes and i associated this to the "Blue" on the text. Now i understand, but this code doesn't work for me. – Cleiton Ribeiro Jan 23 '14 at 16:07