Skip to content

Commit 8c59ec1

Browse files
committed
PDFBOX-1529: allow lineSpacing parameter, as suggested by Dave Powell
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1922746 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4a17bbc commit 8c59ec1

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)