1

It is a bit difficult to find examples of or possible solutions for PDF source code - but I am hoping to find out if it is possible to automatically wrap text in PDF source code?

For instance I have the following stream:

stream
    BT
        /F1 10 Tf 
        100 692 Td
        ( Hello world! I want to... ) Tj 
        0 -15 Td
        ( Hola mundo! Quiero... ) Tj 
    ET
endstream

For brevity I have added in the ... to the text above but it can be assumed that they intend to carry on for many, many lines. As far as I understand from the few examples I can find all you can do is try and split up the text into lines and wrap them each in their own ( ... ) Tj.

I am wondering though if there is an easier way to ensure text wrapping automatically occurs? I am also hoping for a method that doesn't require too much overhead where possible - my goal is small file sizes.

5
  • 3
    No, you have to do it yourself. PDF is not a typesetter, but a way of storing the results of typesetting. You use the widths of the glyphs, and the width of the page to decide how much goes on each line. Obviously, setting ragged-right is easiest. For full justification, where you must adjust word and letter spacing to fit, you can use the appropriate PDF operators to change the spacing, rather than putting it in manually. Commented Jul 12, 2023 at 10:46
  • So best options are to either use monospace fonts and/or adjust word/letter spacing to know how much text I can fit on a single line, and then split the text up into multiple lines as needed? Thank you for the tips.
    – James
    Commented Jul 12, 2023 at 11:09
  • Using only monospace fonts wouldn't solve the problem, it would evade it. And for interesting fonts you need the glyph widths anyway
    – mkl
    Commented Jul 12, 2023 at 11:30
  • @mkl Considering the problem is "can you wrap text in PDFs automatically" and the answer is "no" then isn't problem evasion the name of the game (i.e. there is technically no solution)? Be it if I want to use monospace fonts or if I want to use 'interesting fonts' and therefore define word/letter spacing?
    – James
    Commented Jul 12, 2023 at 11:46
  • 2
    I didn't want to sound dismissive. Merely experience is that focusing on monospaced fonts allows you to quickly get proper results but people tend to be unhappy with them after a day or so as they are used to be able to use arbitrary fonts. Thus, one should early on have proportional fonts on one's mind when implementing pdf generation.
    – mkl
    Commented Jul 13, 2023 at 5:25

1 Answer 1

1

Ok this is a tough one since

Q) "..if it is possible to automatically wrap text in PDF source code?"

A) YES BUT not as expected, nor likely wanted. (Normal PDF body text does not wrap, and nor do pages, nor objects needing two pages, such as tabular data)

The exception is a "Comment" as "Typewriter" or "Free Text" here the typewriter font of Courier has been named as "FreestyleScript", thus "without embedding" works on some Windows machines.

For me alone it looks the same in all PDF viewers. That is, the "Free Style" text spills off the page boundary at that size.

enter image description here enter image description here

So "Why not use that?" and the problem is as soon as you click on the page it changes appearance.

enter image description here enter image description here

To be useful we need to "Flatten" or "Re-print" back to the "Body", as unwrapped lines!

enter image description here

Also during "Flattening" the internal structure will often change drastically so there are "just", He<004F><004F>o Wor<004F>d and the First "Lorum" line!

enter image description here

so same as before at best will be single lines.

/F0 16 Tf
1 0 0 1 24. 492. Tm
[(Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Volutpat odio facilisis mauris sit amet massa vitae. Id venenatis a)] TJ
1
  • 1
    I like the creativity to your solution but as you say it isn't quite what I was hoping for. I can see that writing even a single line of text takes up a lot of space and is far beyond what I would like to do be able to do "by hand" (i.e. by just writing source code). I do understand though that there isn't really any alternative solution as there is no "native" text wrapping (or page wrapping or object wrapping).
    – James
    Commented Jul 13, 2023 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.