Skip to content

Commit

Permalink
PDFBOX-1529: support margins parameters, as suggested by Dave Powell
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1922771 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
THausherr committed Dec 30, 2024
1 parent b908fe5 commit ad81bb3
Showing 1 changed file with 88 additions and 8 deletions.
96 changes: 88 additions & 8 deletions tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public class TextToPDF implements Callable<Integer>
* The line height as a factor of the font size
*/
private static final float DEFAULT_LINE_HEIGHT_FACTOR = 1.05f;

/**
* The default margin
*/
private static final float DEFAULT_MARGIN = 40;

private PDRectangle mediaBox = PDRectangle.LETTER;
private PDFont font = null;
Expand All @@ -87,6 +92,9 @@ public class TextToPDF implements Callable<Integer>
@Option(names = "-charset", description = "the charset to use. \n(default: ${DEFAULT-VALUE})")
private Charset charset = Charset.defaultCharset();

@Option(names = "-margins", arity="0..4", description = "Left Right Top Bottom margins (default: ${DEFAULT-VALUE})")
private float margins[] = {DEFAULT_MARGIN, DEFAULT_MARGIN, DEFAULT_MARGIN, DEFAULT_MARGIN};

@Option(names = "-standardFont",
description = "the font to use for the text. Either this or -ttf should be specified but not both.\nCandidates: ${COMPLETION-CANDIDATES} (default: ${DEFAULT-VALUE})")
private FontName standardFont = FontName.HELVETICA;
Expand All @@ -100,6 +108,10 @@ public class TextToPDF implements Callable<Integer>
@Option(names = {"-o", "--output"}, description = "the generated PDF file", required = true)
private File outfile;

private float leftMargin = DEFAULT_MARGIN;
private float rightMargin = DEFAULT_MARGIN;
private float topMargin = DEFAULT_MARGIN;
private float bottomMargin = DEFAULT_MARGIN;

private enum PageSizes
{
Expand Down Expand Up @@ -167,7 +179,12 @@ public Integer call()
setFontSize(fontSize);
setMediaBox(pageSize.getPageSize());
setLandscape(landscape);

setLineSpacing(lineSpacing);
setLeftMargin(margins[0]);
setRightMargin(margins[1]);
setTopMargin(margins[2]);
setBottomMargin(margins[3]);

boolean hasUtf8BOM = false;
if (charset.equals(StandardCharsets.UTF_8))
{
Expand Down Expand Up @@ -237,7 +254,6 @@ public void createPDFFromText( PDDocument doc, Reader text ) throws IOException
{
font = new PDType1Font(standardFont);
}
final int margin = 40;
float fontHeight = font.getBoundingBox().getHeight() / FONTSCALE;
PDRectangle actualMediaBox =
landscape ? new PDRectangle(mediaBox.getHeight(), mediaBox.getWidth()) : mediaBox;
Expand All @@ -249,7 +265,7 @@ public void createPDFFromText( PDDocument doc, Reader text ) throws IOException
PDPage page = new PDPage(actualMediaBox);
PDPageContentStream contentStream = null;
float y = -1;
float maxStringLength = page.getMediaBox().getWidth() - 2 * margin;
float maxStringLength = page.getMediaBox().getWidth() - leftMargin - rightMargin;

// There is a special case of creating a PDF document from an empty string.
boolean textIsEmpty = true;
Expand Down Expand Up @@ -333,7 +349,7 @@ public void createPDFFromText( PDDocument doc, Reader text ) throws IOException
}
while (lineIndex < lineWords.length && lengthIfUsingNextWord < maxStringLength);

if (y - lineHeight < margin)
if (y - lineHeight < bottomMargin)
{
// We have crossed the end-of-page boundary and need to extend the
// document by another page.
Expand All @@ -347,9 +363,9 @@ public void createPDFFromText( PDDocument doc, Reader text ) throws IOException
contentStream = new PDPageContentStream(doc, page);
contentStream.setFont(font, fontSize);
contentStream.beginText();
y = page.getMediaBox().getHeight() - margin;
y = page.getMediaBox().getHeight() - topMargin;
y += lineHeight - fontHeight * fontSize; // adjust for lineSpacing != 1
contentStream.newLineAtOffset(margin, y);
contentStream.newLineAtOffset(leftMargin, y);
}

if (contentStream == null)
Expand All @@ -368,9 +384,9 @@ public void createPDFFromText( PDDocument doc, Reader text ) throws IOException
contentStream = new PDPageContentStream(doc, page);
contentStream.setFont(font, fontSize);
contentStream.beginText();
y = page.getMediaBox().getHeight() - margin;
y = page.getMediaBox().getHeight() - topMargin;
y += lineHeight - fontHeight * fontSize; // adjust for lineSpacing != 1
contentStream.newLineAtOffset(margin, y);
contentStream.newLineAtOffset(leftMargin, y);
}
}
}
Expand Down Expand Up @@ -441,6 +457,70 @@ public void setLineSpacing(float lineSpacing)
this.lineSpacing = lineSpacing;
}

/**
* @return Returns the left margin.
*/
public float getLeftMargin()
{
return leftMargin;
}

/**
* @param leftMargin The left margin to be set.
*/
public void setLeftMargin(float leftMargin)
{
this.leftMargin = leftMargin;
}

/**
* @return Returns the right margin.
*/
public float getRightMargin()
{
return rightMargin;
}

/**
* @param rightMargin The right margin to be set.
*/
public void setRightMargin(float rightMargin)
{
this.rightMargin = rightMargin;
}

/**
* @return Returns the top margin.
*/
public float getTopMargin()
{
return topMargin;
}

/**
* @param topMargin The top margin to be set.
*/
public void setTopMargin(float topMargin)
{
this.topMargin = topMargin;
}

/**
* @return Returns the bottom margin.
*/
public float getBottomMargin()
{
return bottomMargin;
}

/**
* @param bottomMargin The bottom margin to be set.
*/
public void setBottomMargin(float bottomMargin)
{
this.bottomMargin = bottomMargin;
}

/**
* Sets page size of produced PDF.
*
Expand Down

0 comments on commit ad81bb3

Please sign in to comment.