pdfbox-layout icon indicating copy to clipboard operation
pdfbox-layout copied to clipboard

Escape markup / control characters

Open andrewvsk opened this issue 8 years ago • 5 comments

It would be nice to have a helper function for escaping markup characters in string.

For example if text is dynamically created or stored in DB and contains special symbols (_, *. \n..) which shouldn't be rendered but at the same time could have color.

The workaround is to create manually each element: new StyledText(), new NewLine()...

andrewvsk avatar Oct 17 '17 10:10 andrewvsk

At first sorry for the late answer, I have been off for a while.

Are you using TextFlow? If that is the case, just use addText() instead of addMarkup(). Is that a solution for you?

ralfstuckert avatar Oct 28 '17 05:10 ralfstuckert

Yes, it is only one way now:

paragraph.add(new StyledText(text, size, font, color)); 
paragraph.add(new NewLine());

andrewvsk avatar Oct 28 '17 06:10 andrewvsk

Er...there are two shortcuts on Paragraph (resp. TextFlow): addText() and addMarkup(), where the latter one interprets the given text as markup, the first one doesnt.

So isn't addText() what you actually want?

ralfstuckert avatar Oct 28 '17 10:10 ralfstuckert

Ok. Lets see an example:

String str = "custom_message_1" '; // custom user defined string stored in DB and might has underscore
String txt = String.format("{color:#FF0000} Demo: %s \n", str);
p.addMarkup(txt, BaseFont.Times); 

Expected: red color: 'Demo: custom_message_1' with new line at the end Actual: red color"'Demo: custommessage1.' with new line at the end

andrewvsk avatar Oct 28 '17 15:10 andrewvsk

I have the same question. When loading text from external sources (user input, DB, ...) and still be able to style the text I have to escape these special characters. For example the user can enter the headline of a PDF and this text should be printed bold and with special color.

String headline = "e = m * c²; // user input
p.addMarkup("{color:#FFAA00}*" + headline + "*", Basefont.Times);

The headline value has to be escaped. But how? There should be some static helper function.

dan-osterrath avatar Jun 21 '18 07:06 dan-osterrath