PDFont pdFont, List<Object> tokens, PDAppearanceStream appearanceStream)
throws IOException
{
PrintWriter printWriter = new PrintWriter(output, true);
float fontSize = 0.0f;
PDRectangle boundingBox = appearanceStream.getBBox();
if (boundingBox == null)
{
boundingBox = fieldWidget.getRectangle().createRetranslatedRectangle();
}
printWriter.println("BT");
if (defaultAppearance != null)
{
String daString = defaultAppearance.getString();
PDFStreamParser daParser = new PDFStreamParser(new ByteArrayInputStream(
daString.getBytes("ISO-8859-1")));
daParser.parse();
List<Object> daTokens = daParser.getTokens();
fontSize = calculateFontSize(pdFont, boundingBox, tokens, daTokens);
int fontIndex = daTokens.indexOf(Operator.getOperator("Tf"));
if (fontIndex != -1)
{
daTokens.set(fontIndex - 1, new COSFloat(fontSize));
}
ContentStreamWriter daWriter = new ContentStreamWriter(output);
daWriter.writeTokens(daTokens);
}
PDRectangle borderEdge = getSmallestDrawnRectangle(boundingBox, tokens);
// Acrobat calculates the left and right padding dependent on the offset of the border edge
// This calculation works for forms having been generated by Acrobat.
// Need to revisit this for forms being generated with other software.
float paddingLeft = Math.max(2, Math.round(4 * borderEdge.getLowerLeftX()));
float paddingRight = Math.max(2,
Math.round(4 * (boundingBox.getUpperRightX() - borderEdge.getUpperRightX())));
float verticalOffset = getVerticalOffset(boundingBox, pdFont, fontSize, tokens);
// Acrobat shifts the value so it aligns to the bottom if
// the font's caps are larger than the height of the borderEdge
//
// This is based on a small sample of test files and might not be generally the case.
// The fontHeight calculation has been taken from getVerticalOffset().
// We potentially need to revisit that calculation
float fontHeight = boundingBox.getHeight() - verticalOffset * 2;
if (fontHeight + 2 * borderEdge.getLowerLeftX() > borderEdge.getHeight())
{
verticalOffset = pdFont.getBoundingBox().getHeight() / 1000 * fontSize
- borderEdge.getHeight();
}
float leftOffset = 0f;
// Acrobat aligns left regardless of the quadding if the text is wider than the remaining width
float stringWidth = (pdFont.getStringWidth(value) / 1000) * fontSize;
int q = getQ();
if (q == PDTextField.QUADDING_LEFT
|| stringWidth > borderEdge.getWidth() - paddingLeft - paddingRight)
{
leftOffset = paddingLeft;
}
else if (q == PDTextField.QUADDING_CENTERED)
{
leftOffset = (boundingBox.getWidth() - stringWidth) / 2;
}
else if (q == PDTextField.QUADDING_RIGHT)
{
leftOffset = boundingBox.getWidth() - stringWidth - paddingRight;
}
else
{
// Unknown quadding value - default to left
printWriter.println(paddingLeft + " " + verticalOffset + " Td");
LOG.debug("Unknown justification value, defaulting to left: " + q);
}
printWriter.println(leftOffset + " " + verticalOffset + " Td");
// add the value as hex string to deal with non ISO-8859-1 data values
if (!isMultiLineValue(value) || stringWidth > borderEdge.getWidth() - paddingLeft -
paddingRight)
{
printWriter.println("<" + new COSString(value).getHexString() + "> Tj");
}
else