Pregunta

I am having formatting issues with PDF Box. My goal is to print a PDF in table style format as a report. The content format will be similar to

Name Code Description Value

I retrieve my DB result set and have a List of Java objects. I extract the required information and I format them as as a list of strings as below. I cycle thru the objects, construct a string and add to an arrayList. The idea being I create a list of Strings of the exact same length/style to enforce formatting in pdf.

Code Reference

for(MyObject obj: dbresults){
   //format as below and add to list
}
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
formatter.format("%-25.25s", "This is some text with more than 25 characters.");

formatter.format("%-25.25s", "Some text with less.");

formatter.format("%-25.25s", "Some other text.");

System.out.println(formatter.toString());

Output:

|This is some text with mo|Some text with less.     |Some other text.         |

I print out this list multiple times to screen :) and via System.out or logger the format is exactly as I expect, even blocks.

However when I send to PDFBox to print to file the format gets 'corrupted' and the 'table format' is not honoured. I pass in 100 and 700 as x,y co-ords.

Code Reference

    private void printMultipleLines(
    PDPageContentStream contentStream,
    List<String> lines,
    float x,
    float y) throws IOException {
  if (lines.size() == 0) {
    return;
  }
  final int numberOfLines = lines.size();
  final float fontHeight = getFontHeight();

  contentStream.beginText();
  contentStream.appendRawCommands(fontHeight + " TL\n");
  contentStream.moveTextPositionByAmount( x, y);
  for (int i = 0; i < numberOfLines; i++) {
    contentStream.drawString(lines.get(i));
    if (i < numberOfLines - 1) {
      contentStream.appendRawCommands("T*\n");
    }
  }
  contentStream.endText();
}

To get the height of the font you can use this command:

fontHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;

Sample List for printing: This is how it looks after using java formatter class when printing to screen, All looks good, but when i print to PDFBox the format is not honoured

Tom Thumb         555-ddd   Good     23
Tom Thumb         666-ggg   Good     45
CHARLES DICKENS   777-jjj   Good     32
CHARLES DICKENS   666-hhh   Bad      11
W Yeats           888-hhh   Ok       12
R Whitely         444-999   Terrible 44 

PDF output looks like

Tom Thumb         555-ddd   Good     23
Tom Thumb         666-ggg   Good     45
CHARLES DICKENS       777-jjj   Good     32
CHARLES DICKENS       666-hhh   Bad      11
W Yeats           888-hhh   Ok       12
R Whitely          444-999   Terrible 44 

Any help much appreciated!

¿Fue útil?

Solución

do you use monospaced fonts in the PDF (as Courier) ? It would explain that you get good output in console and a bad one in PDF

regards

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top