0

I'm working on a C# project using iTextSharp for various reports. I use one class to pull data from an order and then another class to display the data on the report. I'm pulling the note data (a string) from a separate class, using the loop below:

for (int i = 0; i < ovNotes.Length; i++)
{
    OVData.orderNotes += StripHTML(ovNotes[i].HTML);
}

ovNotes is a string, and so its OVData.orderNotes

The StripHTML method does exactly what it says, removes all the <p>,<br /> etc tags that appear in the ovNotes

Next, in the actual class to display all the information:

Phrase orderNotes = this.FormatPhrase(xOVData.orderNotes);
ColumnText.ShowTextAligned(instance.DirectContent, 0, orderNotes, 36f, 440f, 0f);

This formats the order notes to give them a consistent font, font size, etc. However, when the code runs, the order notes do not appear. When I substitute orderNotes with a made up phrase it displays fine, with no other code changed.

Phrase orderNotes = this.FormatPhrase(xOVData.orderNotes);
phrase testPhrase = this.FormatPhrase("This is a test");
ColumnText.ShowTextAligned(instance.DirectContent, 0, testPhrase, 36f, 440f, 0f);
/* Desired (and correct) output
* NOTES:
*   This is a test
*/

The Phrase orderNotes contains all of the notes from xOVdata.OrderNotes, but for whatever reason it refuses to display. I did notice, however, that the orderNotes also contain all of the &nbsp that the original notes had, as the StripHTML method doesn't remove those. I've looked around and the only results I found were issues with other (human) languages, or using iTextSharp to edit an existing PDF.

What could be preventing the imported string from displaying properly, especially when it contains all the correct data in the line directly above the ColumnText.ShowText...?

  • 1
    Write `xOVData.orderNotes` to the `System.out` and show us what it contains. Replace the hardcoded `"This is a test"` with that output and look what happens. At first sight, this doesn't look like an iText problem. An impartial observer would assume that `xOVData.orderNotes` may be an empty `String` (for instance because the `StripHTML()` method removes both tags and content). – Bruno Lowagie Mar 24 '15 at 07:59
  • So I ran the System.out in both the first and second classes. The results for both are the following: Test123   test2   test1234 test3   I used the StripHTML method off of the internet (copy pasting others' code always a good idea) and verified it's only removing HTML tags – wschlosser Mar 24 '15 at 16:56
  • It looks to be that ColumnText.ShowTextAligned() doesn't handle escape characters well. The orderNotes I'm trying to import have a \n before the first one, which causes none of them to display. If I add a \n in between 'test123' and 'test2' only 'test123' is displayed. Removing the \n allows all 4 test notes to be displayed properly. – wschlosser Mar 24 '15 at 17:46

1 Answers1

0

Please read my answer to Add paragraph dynamically at the bottom with iText library in android

As I explain in that answer, the showTextAligned() method can only be used to add a single line (text will not by distributed over different lines, newlines are not allowed). In your comments, you indicate that your String starts with a newline character. That is not valid in the context of showTextAligned().

To solve your problem, you'll either have to switch to the solution as explained in my answer, or you'll have to remove all the newline characters.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165