@@ -63,7 +63,7 @@ public class TextToPDF implements Callable<Integer>
6363 /**
6464 * The line height as a factor of the font size
6565 */
66- private static final float LINE_HEIGHT_FACTOR = 1.05f ;
66+ private static final float DEFAULT_LINE_HEIGHT_FACTOR = 1.05f ;
6767
6868 private PDRectangle mediaBox = PDRectangle .LETTER ;
6969 private PDFont font = null ;
@@ -74,6 +74,9 @@ public class TextToPDF implements Callable<Integer>
7474
7575 @ Option (names = "-fontSize" , description = "the size of the font to use (default: ${DEFAULT-VALUE})" )
7676 private float fontSize = DEFAULT_FONT_SIZE ;
77+
78+ @ Option (names = "-lineSpacing" , description = "the factor of the font size for the line height (default: ${DEFAULT-VALUE})" )
79+ private float lineSpacing = DEFAULT_LINE_HEIGHT_FACTOR ;
7780
7881 @ Option (names = "-landscape" , description = "set orientation to landscape" )
7982 private boolean landscape = false ;
@@ -240,7 +243,7 @@ public void createPDFFromText( PDDocument doc, Reader text ) throws IOException
240243 landscape ? new PDRectangle (mediaBox .getHeight (), mediaBox .getWidth ()) : mediaBox ;
241244
242245 // calculate line height and increase by a factor.
243- float lineHeight = fontHeight * fontSize * LINE_HEIGHT_FACTOR ;
246+ float lineHeight = fontHeight * fontSize * lineSpacing ;
244247 BufferedReader data = new BufferedReader (text );
245248 String nextLine ;
246249 PDPage page = new PDPage (actualMediaBox );
@@ -345,6 +348,7 @@ public void createPDFFromText( PDDocument doc, Reader text ) throws IOException
345348 contentStream .setFont (font , fontSize );
346349 contentStream .beginText ();
347350 y = page .getMediaBox ().getHeight () - margin ;
351+ y += lineHeight - fontHeight * fontSize ; // adjust for lineSpacing != 1
348352 contentStream .newLineAtOffset (margin , y );
349353 }
350354
@@ -365,6 +369,7 @@ public void createPDFFromText( PDDocument doc, Reader text ) throws IOException
365369 contentStream .setFont (font , fontSize );
366370 contentStream .beginText ();
367371 y = page .getMediaBox ().getHeight () - margin ;
372+ y += lineHeight - fontHeight * fontSize ; // adjust for lineSpacing != 1
368373 contentStream .newLineAtOffset (margin , y );
369374 }
370375 }
@@ -407,6 +412,7 @@ public float getFontSize()
407412 {
408413 return fontSize ;
409414 }
415+
410416 /**
411417 * @param aFontSize The fontSize to set.
412418 */
@@ -415,6 +421,26 @@ public void setFontSize(float aFontSize)
415421 this .fontSize = aFontSize ;
416422 }
417423
424+ /**
425+ * @return Returns the lineSpacing.
426+ */
427+ public float getLineSpacing ()
428+ {
429+ return lineSpacing ;
430+ }
431+
432+ /**
433+ * @param lineSpacing The lineSpacing to set.
434+ */
435+ public void setLineSpacing (float lineSpacing )
436+ {
437+ if (lineSpacing <= 0 )
438+ {
439+ throw new IllegalArgumentException ("line spacing must be positive: " + lineSpacing );
440+ }
441+ this .lineSpacing = lineSpacing ;
442+ }
443+
418444 /**
419445 * Sets page size of produced PDF.
420446 *
0 commit comments