Search code examples
pdfpdf-generation

Is there an easy way to wrap text in PDF source code?


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.


Solution

  • 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