PDF语法BT/ET文本操作技术咨询:换行、加粗与多语言实现
Hey there! Let's tackle your PDF text rendering questions one by one—these are all common hurdles when working directly with PDF content streams, so you’re not alone in figuring this out.
1. How to insert line breaks (your \n\r isn’t working)
PDF doesn’t recognize plain newline characters like \n or \r in (...)Tj strings as line breaks—text placement is entirely position-based. You have two reliable ways to add line breaks:
Use the
Tdoperator to manually shift the text position:
This lets you control exactly how far down the next line starts. For example:BT /F1 24 Tf % Set base font and size 100 700 Td % Position text at (100, 700) (First line of text)Tj 0 -30 Td % Move down 30 units (PDF y-axis points upward) (Second line of text)Tj ETUse
TL+T*for automatic line spacing:
First set the line leading (distance between baselines) withTL, then useT*to jump to the next line:BT /F1 24 Tf 30 TL % Set line leading to 30 units 100 700 Td (First line)Tj T* % Jump to next line using the set leading (Second line)Tj ET
2. How to switch to bold mid-string
To change font weight (or any font property) in the middle of a string, you need to:
- Have the bold version of your font defined in the PDF’s page resources (under
/Resources/Font). - Switch fonts mid-text stream before rendering the bold portion, then switch back if needed.
Example code snippet:
BT /F1 24 Tf % Regular font (already defined in resources) (This is regular text, then )Tj /F1-Bold 24 Tf % Switch to bold variant (must exist in resources) (this part is bold)Tj /F1 24 Tf % Switch back to regular font (, and this is regular again.)Tj ET
Just make sure /F1-Bold is properly registered in the page’s font dictionary—otherwise the PDF reader won’t recognize it.
3. Rendering non-English languages (Hebrew, Arabic, Chinese)
Rendering complex scripts or non-Latin languages requires handling three key pieces: font embedding, text direction (for RTL languages), and character mapping.
For Chinese & CJK Languages
- Embed a CJK-compatible font: Use fonts like SimSun, Microsoft YaHei, or a custom CJK font, and ensure it’s embedded in the PDF (so readers don’t substitute it with an incompatible one).
- Match character encoding: Use the correct character mapping (CMaps) for your font. For UTF-8 text, you can either pass the raw UTF-8 string in parentheses, or use hexadecimal Unicode values. Example:
BT /SimSun 24 Tf % Embedded Chinese font (你好,世界!)Tj % Raw UTF-8 string (works if font supports it) % Or use hexadecimal Unicode: <E4BD A0 E5A5 BD EFBC 8C E4B8 96 E795 8C EFBC 81>Tj ET
For Hebrew & Arabic (RTL Languages)
- RTL text direction: PDF doesn’t natively handle right-to-left layout, so you’ll need to adjust the text transformation matrix to reverse the direction. Use
Tmto flip the x-axis:BT /Amiri 24 Tf % Embedded Arabic/Hebrew font -1 0 0 1 300 700 Tm % Flip x-axis, position text at (300,700) (שלום עולם)Tj % Hebrew text (logical order, will render RTL) ET - Character ordering: Note that you need to store the text in logical order (the order you’d type it), not visual order—the flipped matrix handles the right-to-left display.
- Font support: Ensure your font includes glyphs for the target language’s characters.
内容的提问来源于stack exchange,提问作者user4602228

