PDF Text Effect



PDF Text Effect

By Warren Hinchliffe
Challenge:

In reproducing an existing statement, I found that the PDF report required text in the titles of the report to be rotated by 90 degrees.
Stacking the characters one on top of the other looked horrible and the client would not budge on the layout, it was not negotiable.

Solution:

This is complex and I hope the explanation is good enough.

When a PDF document is created with WebFOCUS, the document is text (Tested up to V7.6.4).

When a text string is added to the document it is in the format “a b c d h v Tm ({text})Tj”.

Where
a - horizontal 'scaling' component of the new text matrix.
b - 'rotation' component of the new text matrix.
c - vertical 'scaling' component of the new text matrix.
d - 'rotation' component of the new text matrix.
h - horizontal translation component of the new text matrix.
v - vertical translation component of the new text matrix

a,b,c and d values were 0 or the font size.

By manipulating the values of a,b,c and d to positive, negative values and changing the position of the values, the text was able to be rotated.

The result of doing this allowed the easy rotation of the text in increments of 45 degree. Rotation can be done at any angle, but this required a lot of calculations.

By manipulating the values of h and v the text was able to be moved around the page.

A FEX was written to read the PDF document and identify text strings to be rotated.
Once identified a, b, c, d, h, v and {text} could be changed to rotate the text, move the text and change to text to something else.

Changing text is important, in particular if a report column needs to be thin, but the title is long, When the width of the column is squeezed, the title is truncated, therefore setting the title to 2 or three characters, then changing to what is required is important.

This was the easy part!

When manipulated, the document would not open, it was broken.

The way the document is structured, is to specify an object, then specify its length.

Most objects are typically the text on a page, then followed by its length. In changing the rotation, position and possibly the text, the document objects length had changed.

The next step is to identify the objects length and change it to its new length. To do this each lines length was recalculated, summed up and the length replaced.

Although the document seems perfect, it still did not open, this was because using a TABLE request added trailing spaces to the resultant output file, these had to be removed.

Initially the Unix SED command was used, but was later replaced with a Dialog Manager Loop to read in the line and TRUNCATE the text, then write out the line.

The document still had issues with Unix format, the document needed to be PC format (CR-LF). A test was added to check &FOCMODE to add a Carriage Return when the OS was Unix.

The PDF produced perfectly.

Serendipity

If a,b,c and d font size values are changed to a different size, the font size changes, but if a,b is changed to one value and c, d is changed to another, the text can be stretched in height or width.

Inbuilt text rendering capabilities were also taken advantage of to also provide an option to show text a outline of filled outline.

This is done by "{x} w {y} Tr" before and after the text string.

Where:
x - The width of the outline line.
y - The render type
     0 - Normal (Filled text).
     1 - Outlined.
     2 - Outlined and filled.
     3 - Neither filled nor outlined text (invisible).

There is one drawback.
You cannot use PCHOLD.

Instead you can use:
SET HTMLFORMTYPE = PDF
-HTMLFORM {FILEDEF name of the PDF}

to return the PDF document to the browser.

This is PDF:

This is PDF FX

Attached zip contains source code, sample program and sample output. The most up to date copy can be found in the Focal Point topic, Rotating Text in PDF's and other interesting things.

If any question, please contact Warren at warren.hinchliffe@linkmarketservices.com.au.