Author: jahewson Date: Tue May 19 17:37:32 2015 New Revision: 1680355 URL: http://svn.apache.org/r1680355 Log: PDFBOX-2333: Use the same code for single-line and multi-line text
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java?rev=1680355&r1=1680354&r2=1680355&view=diff ============================================================================== --- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java (original) +++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java Tue May 19 17:37:32 2015 @@ -230,38 +230,27 @@ class AppearanceGeneratorHelper } // show the text - float leftOffset; - if (!isMultiLine()) - { - // calculation of the horizontal offset from where the text will be printed - leftOffset = calculateHorizontalOffset(contentRect, font, fontSize); - contents.newLineAtOffset(leftOffset, y); - contents.showText(value); - } - else - { - leftOffset = contentRect.getLowerLeftX(); - PlainText textContent = new PlainText(value); - AppearanceStyle appearanceStyle = new AppearanceStyle(); - appearanceStyle.setFont(font); - appearanceStyle.setFontSize(fontSize); - - // Adobe Acrobat uses the font's bounding box for the leading between the lines - appearanceStyle.setLeading(font.getBoundingBox().getHeight() / - GLYPH_TO_PDF_SCALE * fontSize); - - PlainTextFormatter formatter = new PlainTextFormatter - .Builder(contents) - .style(appearanceStyle) - .text(textContent) - .width(contentRect.getWidth()) - .wrapLines(true) - .initialOffset(leftOffset, y) - .textAlign(field.getQ()) - .build(); - formatter.format(); - - } + float leftOffset = contentRect.getLowerLeftX(); + PlainText textContent = new PlainText(value); + AppearanceStyle appearanceStyle = new AppearanceStyle(); + appearanceStyle.setFont(font); + appearanceStyle.setFontSize(fontSize); + + // Adobe Acrobat uses the font's bounding box for the leading between the lines + appearanceStyle.setLeading(font.getBoundingBox().getHeight() / + GLYPH_TO_PDF_SCALE * fontSize); + + PlainTextFormatter formatter = new PlainTextFormatter + .Builder(contents) + .style(appearanceStyle) + .text(textContent) + .width(contentRect.getWidth()) + .wrapLines(true) + .initialOffset(leftOffset, y) + .textAlign(field.getQ()) + .build(); + formatter.format(); + contents.endText(); contents.restoreGraphicsState(); contents.close(); @@ -313,50 +302,6 @@ class AppearanceGeneratorHelper return fontSize; } - - /** - * Calculate the horizontal start position for the text. - * - * @param contentEdge the content edge - * @param pdFont the font to use for formatting - * @param fontSize the font size to use for formating - * - * @return the horizontal start position of the text - * - * @throws IOException If there is an error calculating the text position. - */ - private float calculateHorizontalOffset(PDRectangle contentEdge, PDFont pdFont, float fontSize) - throws IOException - { - // Acrobat aligns left regardless of the quadding if the text is wider than the remaining - // width - float stringWidth = pdFont.getStringWidth(value) / GLYPH_TO_PDF_SCALE * fontSize; - float leftOffset; - - int q = field.getQ(); - - if (q == PDTextField.QUADDING_LEFT - || stringWidth > contentEdge.getWidth()) - { - leftOffset = contentEdge.getLowerLeftX(); - } - else if (q == PDTextField.QUADDING_CENTERED) - { - leftOffset = contentEdge.getLowerLeftX() + (contentEdge.getWidth() - stringWidth) / 2; - } - else if (q == PDTextField.QUADDING_RIGHT) - { - leftOffset = contentEdge.getLowerLeftX() + contentEdge.getWidth() - stringWidth; - } - else - { - // Unknown quadding value - default to left - leftOffset = contentEdge.getLowerLeftX(); - LOG.debug("Unknown justification value, defaulting to left: " + q); - } - - return leftOffset; - } /** * Resolve the bounding box.