use of com.itextpdf.layout.layout.LayoutArea in project i7j-pdfsweep by itext.
the class PdfCleanUpTool method drawOverlayText.
private void drawOverlayText(PdfCanvas canvas, String overlayText, Rectangle annotRect, PdfBoolean repeat, PdfString defaultAppearance, int justification) throws IOException {
Map<String, List> parsedDA;
try {
parsedDA = parseDAParam(defaultAppearance);
} catch (NullPointerException npe) {
throw new PdfException(CleanupExceptionMessageConstant.DEFAULT_APPEARANCE_NOT_FOUND);
}
PdfFont font;
float fontSize = 12;
List fontArgs = parsedDA.get("Tf");
PdfDictionary formDictionary = pdfDocument.getCatalog().getPdfObject().getAsDictionary(PdfName.AcroForm);
if (fontArgs != null && formDictionary != null) {
font = getFontFromAcroForm((PdfName) fontArgs.get(0));
fontSize = ((PdfNumber) fontArgs.get(1)).floatValue();
} else {
font = PdfFontFactory.createFont();
}
if (pdfDocument.isTagged()) {
canvas.openTag(new CanvasArtifact());
}
Canvas modelCanvas = new Canvas(canvas, annotRect, false);
Paragraph p = new Paragraph(overlayText).setFont(font).setFontSize(fontSize).setMargin(0);
TextAlignment textAlignment = TextAlignment.LEFT;
switch(justification) {
case 1:
textAlignment = TextAlignment.CENTER;
break;
case 2:
textAlignment = TextAlignment.RIGHT;
break;
default:
}
p.setTextAlignment(textAlignment);
List strokeColorArgs = parsedDA.get("StrokeColor");
if (strokeColorArgs != null) {
p.setStrokeColor(getColor(strokeColorArgs));
}
List fillColorArgs = parsedDA.get("FillColor");
if (fillColorArgs != null) {
p.setFontColor(getColor(fillColorArgs));
}
modelCanvas.add(p);
if (repeat != null && repeat.getValue()) {
boolean hasFull = modelCanvas.getRenderer().hasProperty(Property.FULL);
boolean isFull = hasFull ? (boolean) modelCanvas.getRenderer().getPropertyAsBoolean(Property.FULL) : false;
while (!isFull) {
p.add(overlayText);
LayoutArea previousArea = modelCanvas.getRenderer().getCurrentArea().clone();
modelCanvas.relayout();
if (modelCanvas.getRenderer().getCurrentArea().equals(previousArea)) {
// Avoid infinite loop. This might be caused by the fact that the font does not support the text we want to show
break;
}
hasFull = modelCanvas.getRenderer().hasProperty(Property.FULL);
isFull = hasFull ? (boolean) modelCanvas.getRenderer().getPropertyAsBoolean(Property.FULL) : false;
}
}
modelCanvas.getRenderer().flush();
if (pdfDocument.isTagged()) {
canvas.closeTag();
}
}
Aggregations