Search in sources :

Example 1 with Canvas

use of com.itextpdf.layout.Canvas in project i7js-highlevel by itext.

the class CanvasFontMethods method createPdf.

public void createPdf(String dest) throws IOException {
    // Initialize PDF document
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    // Initialize document
    PdfPage page = pdf.addNewPage();
    PdfCanvas pdfCanvas = new PdfCanvas(page);
    Rectangle rectangle = new Rectangle(36, 36, 523, 770);
    Canvas canvas = new Canvas(pdfCanvas, rectangle);
    Paragraph p;
    p = new Paragraph("Testing font methods");
    canvas.add(p);
    PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
    canvas.setFont(font);
    p = new Paragraph("Testing font methods: changed font");
    canvas.add(p);
    canvas.setFontSize(18);
    p = new Paragraph("Testing font methods: changed font size");
    canvas.add(p);
    canvas.setFontColor(ColorConstants.BLUE);
    p = new Paragraph("Testing font methods: changed color");
    canvas.add(p);
    canvas.setBold();
    p = new Paragraph("Testing font methods: to bold");
    canvas.add(p);
    canvas.setItalic();
    p = new Paragraph("Testing font methods: to italic");
    canvas.add(p);
    canvas.setProperty(Property.BOLD_SIMULATION, false);
    canvas.setProperty(Property.ITALIC_SIMULATION, false);
    canvas.setProperty(Property.FONT_COLOR, null);
    p = new Paragraph("Testing font methods: resetting style and color");
    canvas.add(p);
    canvas.setLineThrough();
    p = new Paragraph("Testing font methods: line through (default)");
    canvas.add(p);
    canvas.setProperty(Property.UNDERLINE, null);
    canvas.setUnderline();
    p = new Paragraph("Testing font methods: underline (default)");
    canvas.add(p);
    canvas.setProperty(Property.UNDERLINE, null);
    canvas.setUnderline(2, 4);
    canvas.setUnderline(ColorConstants.BLUE, 5, 0.1f, 2, -0.5f, PdfCanvasConstants.LineCapStyle.ROUND);
    p = new Paragraph("Testing font methods: underline (custom)");
    canvas.add(p);
    canvas.setProperty(Property.UNDERLINE, null);
    canvas.setTextRenderingMode(PdfCanvasConstants.TextRenderingMode.STROKE);
    p = new Paragraph("Testing font methods: change text rendering mode");
    canvas.add(p);
    canvas.setStrokeWidth(0.1f);
    canvas.setStrokeColor(ColorConstants.BLUE);
    p = new Paragraph("Testing font methods: change stroke width and color");
    canvas.add(p);
    // Close document
    pdf.close();
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) Canvas(com.itextpdf.layout.Canvas) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) PdfFont(com.itextpdf.kernel.font.PdfFont) Rectangle(com.itextpdf.kernel.geom.Rectangle) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) PdfPage(com.itextpdf.kernel.pdf.PdfPage) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Paragraph(com.itextpdf.layout.element.Paragraph)

Example 2 with Canvas

use of com.itextpdf.layout.Canvas in project pdf-editor by Pdf-Creator.

the class PDFDocument method addRectangleWithTableItem.

public void addRectangleWithTableItem(TableItem tableItem) {
    PdfCanvas canvas = new PdfCanvas(currentPage);
    Rectangle rect = new Rectangle(tableItem.getX(), tableItem.getY(), tableItem.getW(), tableItem.getH());
    canvas.setStrokeColor(tableItem.getRectangleStrokeColor()).setFillColor(tableItem.getRectangleFillColor()).rectangle(rect).fill().stroke();
    Table table = new Table(tableItem.getCols());
    table.setFontColor(tableItem.getFontColor());
    for (String cellContent : tableItem.getCellContents()) {
        table.addCell(cellContent);
    }
    new Canvas(canvas, rect).add(table);
}
Also used : Table(com.itextpdf.layout.element.Table) Canvas(com.itextpdf.layout.Canvas) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) Rectangle(com.itextpdf.kernel.geom.Rectangle) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas)

Example 3 with Canvas

use of com.itextpdf.layout.Canvas in project pdf-editor by Pdf-Creator.

the class PDFDocument method addRectangleWithFormulaItem.

public void addRectangleWithFormulaItem(FormulaItem formulaItem) throws IOException {
    PdfCanvas canvas = new PdfCanvas(currentPage);
    Rectangle rect = new Rectangle(formulaItem.getX(), formulaItem.getY(), formulaItem.getW(), formulaItem.getW());
    canvas.setStrokeColor(formulaItem.getRectangleStrokeColor()).setFillColor(formulaItem.getRectangleStrokeColor()).rectangle(rect).fill().stroke();
    // very strange method
    TeXFormula tf = new TeXFormula(formulaItem.getFormula());
    TeXIcon ti = tf.createTeXIcon(TeXConstants.STYLE_DISPLAY, formulaItem.getFontSize());
    BufferedImage bimg = new BufferedImage(ti.getIconWidth(), ti.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
    // -- painting the formula --
    Graphics2D g2d = bimg.createGraphics();
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, ti.getIconWidth(), ti.getIconHeight());
    JLabel jl = new JLabel();
    jl.setForeground(new Color(0, 0, 0));
    ti.paintIcon(jl, g2d, 0, 0);
    // --------------------------
    // -- adding it to pdf --
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ImageIO.write(bimg, "png", byteArrayOutputStream);
    ImageData data = ImageDataFactory.create(byteArrayOutputStream.toByteArray());
    Image image = new Image(data);
    image.setFixedPosition(formulaItem.getX(), formulaItem.getY());
    image.scaleAbsolute(formulaItem.getW(), formulaItem.getH());
    new Canvas(canvas, rect).add(image);
}
Also used : ImageData(com.itextpdf.io.image.ImageData) TeXFormula(org.scilab.forge.jlatexmath.TeXFormula) Canvas(com.itextpdf.layout.Canvas) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) Rectangle(com.itextpdf.kernel.geom.Rectangle) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) Image(com.itextpdf.layout.element.Image) BufferedImage(java.awt.image.BufferedImage) TeXIcon(org.scilab.forge.jlatexmath.TeXIcon)

Example 4 with Canvas

use of com.itextpdf.layout.Canvas 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)

Example 5 with Canvas

use of com.itextpdf.layout.Canvas in project axelor-open-suite by axelor.

the class TableFooterEventHandler method handleEvent.

@SuppressWarnings("resource")
@Override
public void handleEvent(Event event) {
    PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
    PdfDocument pdfDoc = docEvent.getDocument();
    PdfPage page = docEvent.getPage();
    PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);
    Rectangle rect1 = new Rectangle(pdfDoc.getDefaultPageSize().getX() + doc.getLeftMargin(), pdfDoc.getDefaultPageSize().getBottom() - doc.getBottomMargin(), 100, 90);
    new Canvas(canvas, pdfDoc, rect1).add(tableFooter);
}
Also used : PdfDocumentEvent(com.itextpdf.kernel.events.PdfDocumentEvent) Canvas(com.itextpdf.layout.Canvas) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) Rectangle(com.itextpdf.kernel.geom.Rectangle) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) PdfPage(com.itextpdf.kernel.pdf.PdfPage) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument)

Aggregations

PdfCanvas (com.itextpdf.kernel.pdf.canvas.PdfCanvas)12 Canvas (com.itextpdf.layout.Canvas)12 Rectangle (com.itextpdf.kernel.geom.Rectangle)11 Paragraph (com.itextpdf.layout.element.Paragraph)8 PdfDocument (com.itextpdf.kernel.pdf.PdfDocument)7 PdfPage (com.itextpdf.kernel.pdf.PdfPage)7 PdfFont (com.itextpdf.kernel.font.PdfFont)6 PdfWriter (com.itextpdf.kernel.pdf.PdfWriter)6 Text (com.itextpdf.layout.element.Text)4 ImageData (com.itextpdf.io.image.ImageData)2 Image (com.itextpdf.layout.element.Image)2 BufferedImage (java.awt.image.BufferedImage)2 GlyphLine (com.itextpdf.io.font.otf.GlyphLine)1 PdfDocumentEvent (com.itextpdf.kernel.events.PdfDocumentEvent)1 PdfException (com.itextpdf.kernel.exceptions.PdfException)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 Table (com.itextpdf.layout.element.Table)1