Search in sources :

Example 1 with TextAlignment

use of com.itextpdf.layout.properties.TextAlignment 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();
    }
}
Also used : PdfException(com.itextpdf.kernel.exceptions.PdfException) PdfName(com.itextpdf.kernel.pdf.PdfName) PdfDictionary(com.itextpdf.kernel.pdf.PdfDictionary) Canvas(com.itextpdf.layout.Canvas) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) PdfString(com.itextpdf.kernel.pdf.PdfString) Paragraph(com.itextpdf.layout.element.Paragraph) LayoutArea(com.itextpdf.layout.layout.LayoutArea) CanvasArtifact(com.itextpdf.kernel.pdf.canvas.CanvasArtifact) PdfFont(com.itextpdf.kernel.font.PdfFont) TextAlignment(com.itextpdf.layout.properties.TextAlignment) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

PdfException (com.itextpdf.kernel.exceptions.PdfException)1 PdfFont (com.itextpdf.kernel.font.PdfFont)1 PdfDictionary (com.itextpdf.kernel.pdf.PdfDictionary)1 PdfName (com.itextpdf.kernel.pdf.PdfName)1 PdfString (com.itextpdf.kernel.pdf.PdfString)1 CanvasArtifact (com.itextpdf.kernel.pdf.canvas.CanvasArtifact)1 PdfCanvas (com.itextpdf.kernel.pdf.canvas.PdfCanvas)1 Canvas (com.itextpdf.layout.Canvas)1 Paragraph (com.itextpdf.layout.element.Paragraph)1 LayoutArea (com.itextpdf.layout.layout.LayoutArea)1 TextAlignment (com.itextpdf.layout.properties.TextAlignment)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1