Example usage for com.itextpdf.text.pdf PdfContentByte setLineWidth

List of usage examples for com.itextpdf.text.pdf PdfContentByte setLineWidth

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfContentByte setLineWidth.

Prototype


public void setLineWidth(final double w) 

Source Link

Document

Changes the line width.

Usage

From source file:com.ephesoft.dcma.imagemagick.MultiPageExecutor.java

License:Open Source License

/**
 * The <code>addImageToPdf</code> method is used to add image to pdf and make it searchable by adding image text in invisible mode
 * w.r.t parameter 'isPdfSearchable' passed.
 * //  w  w w.  j  a v  a2s.c  om
 * @param pdfWriter {@link PdfWriter} writer of pdf in which image has to be added
 * @param htmlUrl {@link HocrPage} corresponding html file for fetching text and coordinates
 * @param imageUrl {@link String} url of image to be added in pdf
 * @param isPdfSearchable true for searchable pdf else otherwise
 * @param widthOfLine
 */
private void addImageToPdf(PdfWriter pdfWriter, HocrPage hocrPage, String imageUrl, boolean isPdfSearchable,
        final int widthOfLine) {
    if (null != pdfWriter && null != imageUrl && imageUrl.length() > 0) {
        try {
            LOGGER.info("Adding image" + imageUrl + " to pdf using iText");
            Image pageImage = Image.getInstance(imageUrl);
            float dotsPerPointX = pageImage.getDpiX() / PDF_RESOLUTION;
            float dotsPerPointY = pageImage.getDpiY() / PDF_RESOLUTION;
            PdfContentByte pdfContentByte = pdfWriter.getDirectContent();

            pageImage.scaleToFit(pageImage.getWidth() / dotsPerPointX, pageImage.getHeight() / dotsPerPointY);

            pageImage.setAbsolutePosition(0, 0);

            // Add image to pdf
            pdfWriter.getDirectContentUnder().addImage(pageImage);
            pdfWriter.getDirectContentUnder().add(pdfContentByte);

            // If pdf is to be made searchable
            if (isPdfSearchable) {
                LOGGER.info("Adding invisible text for image: " + imageUrl);
                float pageImagePixelHeight = pageImage.getHeight();
                Font defaultFont = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.BOLD, CMYKColor.BLACK);

                // Fetch text and coordinates for image to be added
                Map<String, int[]> textCoordinatesMap = getTextWithCoordinatesMap(hocrPage, widthOfLine);
                Set<String> ketSet = textCoordinatesMap.keySet();

                // Add text at specific location
                for (String key : ketSet) {
                    int[] coordinates = textCoordinatesMap.get(key);
                    float bboxWidthPt = (coordinates[2] - coordinates[0]) / dotsPerPointX;
                    float bboxHeightPt = (coordinates[3] - coordinates[1]) / dotsPerPointY;
                    pdfContentByte.beginText();

                    // To make text added as invisible
                    pdfContentByte.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE);
                    pdfContentByte.setLineWidth(Math.round(bboxWidthPt));

                    // Ceil is used so that minimum font of any text is 1
                    // For exception of unbalanced beginText() and endText()
                    if (bboxHeightPt > 0.0) {
                        pdfContentByte.setFontAndSize(defaultFont.getBaseFont(),
                                (float) Math.ceil(bboxHeightPt));
                    } else {
                        pdfContentByte.setFontAndSize(defaultFont.getBaseFont(), 1);
                    }
                    float xCoordinate = (float) (coordinates[0] / dotsPerPointX);
                    float yCoordinate = (float) ((pageImagePixelHeight - coordinates[3]) / dotsPerPointY);
                    pdfContentByte.moveText(xCoordinate, yCoordinate);
                    pdfContentByte.showText(key);
                    pdfContentByte.endText();
                }
            }
            pdfContentByte.closePath();
        } catch (BadElementException badElementException) {
            LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: "
                    + badElementException.toString());
        } catch (DocumentException documentException) {
            LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: "
                    + documentException.toString());
        } catch (MalformedURLException malformedURLException) {
            LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: "
                    + malformedURLException.toString());
        } catch (IOException ioException) {
            LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: "
                    + ioException.toString());
        }
    }
}

From source file:com.mycompany.mavenproject1.Createpdf.java

private void generateLayout(Document doc, PdfContentByte cb) {

    try {//w  w w. j  a  v a 2 s .  c  om

        cb.setLineWidth(1f);

        // Invoice Header box layout
        cb.rectangle(420, 700, 150, 60);
        cb.moveTo(420, 720);
        cb.lineTo(570, 720);
        cb.moveTo(420, 740);
        cb.lineTo(570, 740);
        cb.moveTo(480, 700);
        cb.lineTo(480, 760);
        cb.stroke();

        // Invoice Header box Text Headings 
        createHeadings(cb, 422, 743, "Account No.");
        createHeadings(cb, 422, 723, "Invoice No.");
        createHeadings(cb, 422, 703, "Invoice Date");

        // Invoice Detail box layout 
        cb.rectangle(20, 50, 550, 600);
        cb.moveTo(20, 630);
        cb.lineTo(570, 630);
        cb.moveTo(50, 50);
        cb.lineTo(50, 650);
        cb.moveTo(150, 50);
        cb.lineTo(150, 650);
        cb.moveTo(430, 50);
        cb.lineTo(430, 650);
        cb.moveTo(500, 50);
        cb.lineTo(500, 650);
        cb.stroke();

        // Invoice Detail box Text Headings 
        createHeadings(cb, 22, 633, "Qty");
        createHeadings(cb, 52, 633, "Item Number");
        createHeadings(cb, 152, 633, "Item Description");
        createHeadings(cb, 432, 633, "Price");
        createHeadings(cb, 502, 633, "Ext Price");

        //add the images
        Image companyLogo = Image.getInstance("images/olympics_logo.gif");
        companyLogo.setAbsolutePosition(25, 700);
        companyLogo.scalePercent(25);
        doc.add(companyLogo);

    }

    catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:com.softwaremagico.tm.pdf.complete.elements.CellCompleteBoxEvent.java

License:Open Source License

public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
    PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
    canvas.setLineWidth(borderThickness);

    int bottomMargin = isBorderEnabled(Border.BOTTOM) ? margin : 0;
    int topMargin = isBorderEnabled(Border.TOP) ? margin : 0;
    int leftMargin = isBorderEnabled(Border.LEFT) ? margin : 0;
    int rightMargin = isBorderEnabled(Border.RIGHT) ? margin : 0;

    if (isBorderEnabled(Border.TOP)) {
        canvas.moveTo(position.getLeft() + leftMargin, position.getTop() - topMargin);
        canvas.lineTo(position.getRight() - rightMargin, position.getTop() - topMargin);
    }/*from   w w w  .jav a 2 s  .c  om*/
    if (isBorderEnabled(Border.BOTTOM)) {
        canvas.moveTo(position.getLeft() + leftMargin, position.getBottom() + bottomMargin);
        canvas.lineTo(position.getRight() - rightMargin, position.getBottom() + bottomMargin);
    }
    if (isBorderEnabled(Border.RIGHT)) {
        canvas.moveTo(position.getRight() - rightMargin, position.getBottom() + bottomMargin);
        canvas.lineTo(position.getRight() - rightMargin, position.getTop() - topMargin);
    }
    if (isBorderEnabled(Border.LEFT)) {
        canvas.moveTo(position.getLeft() + leftMargin, position.getBottom() + bottomMargin);
        canvas.lineTo(position.getLeft() + leftMargin, position.getTop() - topMargin);
    }
    canvas.stroke();
}

From source file:com.vectorprint.report.itext.debug.DebugHelper.java

License:Open Source License

public static void debugRect(PdfContentByte canvas, Rectangle rect, float[] dash, float borderWidth,
        EnhancedMap settings, LayerManager layerAware) {
    layerAware.startLayerInGroup(DEBUG, canvas);
    debugFont(canvas, settings);/*from   w  w  w  .  ja v  a 2  s. com*/
    canvas.setLineWidth(borderWidth);
    canvas.setLineDash(dash, 0);
    canvas.moveTo(rect.getLeft(), rect.getBottom());
    canvas.lineTo(rect.getRight(), rect.getBottom());
    canvas.lineTo(rect.getRight(), rect.getTop());
    canvas.lineTo(rect.getLeft(), rect.getTop());
    canvas.closePathStroke();
    ItextHelper.resetLineDash(canvas);
    canvas.endLayer();
}

From source file:com.vectorprint.report.itext.debug.DebugHelper.java

License:Open Source License

/**
 * This method will append to the pdf a legend explaining the visual feedback in the pdf, an overview of the styles
 * used and an overview of the properties used.
 *
 * @throws DocumentException// w  w w  .j a v  a 2 s  .  co  m
 */
public static void appendDebugInfo(PdfWriter writer, Document document, EnhancedMap settings,
        StylerFactory stylerFactory) throws DocumentException, VectorPrintException {

    PdfContentByte canvas = writer.getDirectContent();
    canvas.setFontAndSize(FontFactory.getFont(FontFactory.COURIER).getBaseFont(), 8);
    canvas.setColorFill(
            itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR)));
    canvas.setColorStroke(
            itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR)));

    Font f = FontFactory.getFont(FontFactory.COURIER, 8);

    f.setColor(itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR)));

    float top = document.getPageSize().getTop();

    document.add(new Phrase(new Chunk("table: ", f).setLocalDestination(DEBUGPAGE)));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("cell: ", f));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("image: ", f));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("text: ", f));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("background color is shown in a small rectangle", f));
    document.add(Chunk.NEWLINE);
    canvas.setLineWidth(2);
    canvas.setLineDash(new float[] { 0.3f, 5 }, 0);
    float left = document.leftMargin();
    canvas.rectangle(left + 80, top - 25, left + 80, 8);
    canvas.closePathStroke();
    canvas.setLineWidth(0.3f);
    canvas.setLineDash(new float[] { 2, 2 }, 0);
    canvas.rectangle(left + 80, top - 37, left + 80, 8);
    canvas.closePathStroke();
    canvas.setLineDash(new float[] { 1, 0 }, 0);
    canvas.rectangle(left + 80, top - 50, left + 80, 8);
    canvas.closePathStroke();
    canvas.setLineDash(new float[] { 0.3f, 5 }, 0);
    canvas.rectangle(left + 80, top - 63, left + 80, 8);
    canvas.closePathStroke();
    document.add(Chunk.NEWLINE);

    document.add(new Phrase("fonts available: " + FontFactory.getRegisteredFonts(), f));

    document.add(Chunk.NEWLINE);

    if (settings.getBooleanProperty(false, DEBUG)) {
        document.add(new Phrase("OVERVIEW OF STYLES FOR THIS REPORT", f));
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
    }

    Font b = new Font(f);
    b.setStyle("bold");
    Set<Map.Entry<String, String>> entrySet = stylerFactory.getStylerSetup().entrySet();
    for (Map.Entry<String, String> styleInfo : entrySet) {
        String key = styleInfo.getKey();
        document.add(new Chunk(key, b).setLocalDestination(key));
        document.add(new Chunk(": " + styleInfo.getValue(), f));
        document.add(Chunk.NEWLINE);
        document.add(new Phrase("   styling configured by " + key + ": ", f));
        for (BaseStyler st : (Collection<BaseStyler>) stylerFactory.getBaseStylersFromCache((key))) {
            document.add(Chunk.NEWLINE);
            document.add(new Chunk("      ", f));
            document.add(new Chunk(st.getClass().getSimpleName(), DebugHelper.debugFontLink(canvas, settings))
                    .setLocalGoto(st.getClass().getSimpleName()));
            document.add(new Chunk(":", f));
            document.add(Chunk.NEWLINE);
            document.add(new Phrase("         non default parameters for " + st.getClass().getSimpleName()
                    + ": " + getParamInfo(st), f));
            document.add(Chunk.NEWLINE);
            if (!st.getConditions().isEmpty()) {
                document.add(new Phrase("      conditions for this styler: ", f));
            }
            for (StylingCondition sc : (Collection<StylingCondition>) st.getConditions()) {
                document.add(Chunk.NEWLINE);
                document.add(new Chunk("         ", f));
                document.add(
                        new Chunk(sc.getClass().getSimpleName(), DebugHelper.debugFontLink(canvas, settings))
                                .setLocalGoto(sc.getClass().getSimpleName()));
                document.add(Chunk.NEWLINE);
                document.add(new Phrase("            non default parameters for "
                        + sc.getClass().getSimpleName() + ": " + getParamInfo(sc), f));
            }
        }
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
    }

    document.newPage();

    document.add(new Phrase("Properties used for the report", f));
    document.add(Chunk.NEWLINE);

    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(bo);

    settings.listProperties(ps);
    ps.close();
    document.add(new Paragraph(bo.toString(), f));

    document.newPage();

    try {
        bo = new ByteArrayOutputStream();
        ps = new PrintStream(bo);
        Help.printHelpHeader(ps);
        ps.close();
        document.add(new Paragraph(bo.toString(), f));

        Help.printStylerHelp(document, f);

        Help.printConditionrHelp(document, f);

    } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException
            | NoSuchMethodException | InvocationTargetException ex) {
        log.log(Level.SEVERE, null, ex);
    }
}

From source file:com.vectorprint.report.itext.style.stylers.Shape.java

License:Open Source License

@Override
protected void draw(PdfContentByte canvas, float x, float y, float width, float height, String genericTag) {
    if (getBorderWidth() > 0) {
        canvas.setLineWidth(getBorderWidth());
        canvas.setColorStroke(itextHelper.fromColor((isDrawShadow()) ? getShadowColor() : getBorderColor()));
    }//www .  j a  v a 2  s .  c om
    if (width == -1) {
        width = getWidth();
    }
    if (height == -1) {
        height = getHeight();
    }
    canvas.setColorFill(itextHelper.fromColor((isDrawShadow()) ? getShadowColor() : getColor()));
    if (isRounded()) {
        canvas.setLineJoin(PdfContentByte.LINE_JOIN_ROUND);
    }
    float xx = x, yy = y;
    float[] points = getPoints();
    float padding = getPadding();
    switch (getShape()) {
    case free:
        float xdif = x - points[0];
        float ydif = y - points[1];
        xx = points[0] + xdif;
        yy = points[1] + ydif;
        canvas.moveTo(points[0] + xdif, points[1] + ydif);
        for (int i = 2; i < points.length; i = i + 2) {
            canvas.lineTo(points[i], points[i + 1]);
        }
        break;
    case bezier:
        xdif = x - points[0];
        ydif = y - points[1];
        xx = points[0] + xdif;
        yy = points[1] + ydif;
        canvas.moveTo(points[0] + xdif, points[1] + ydif);
        for (int i = 2; i < points.length; i = i + 4) {
            canvas.curveTo(points[i] + xdif, points[i + 1] + ydif, points[i + 2] + xdif, points[i + 3] + ydif);
        }
        break;
    case rectangle:
        if (isClose()) {
            xx = x - padding;
            yy = y - padding - height;
            canvas.rectangle(xx, yy, width + padding * 2, height + padding * 2);
        } else {
            canvas.rectangle(x, y, width, height);
        }
        break;
    case roundrectangle:
        if (isEnclosing()) {
            xx = x - padding;
            yy = y - padding - height;
            canvas.roundRectangle(xx, yy, width + padding * 2, height + padding * 2, getRadius());
        } else {
            canvas.roundRectangle(x, y, width, height, getRadius());
        }
        break;
    case ellipse:
        if (isEnclosing()) {
            xx = x - padding;
            yy = y - padding - height;
            canvas.ellipse(xx, yy, x + width + 2 * padding, y + 2 * padding);
        } else {
            canvas.ellipse(x, y, x + width, y + height);
        }
        break;
    }
    if (isClose()) {
        if (isFill()) {
            canvas.closePathFillStroke();
        } else {
            canvas.closePathStroke();
        }
    } else {
        canvas.stroke();
    }
    if (getSettings().getBooleanProperty(Boolean.FALSE, ReportConstants.DEBUG)) {
        DebugHelper.styleLink(canvas, getStyleClass(), " (styling)", xx, yy, getSettings(), getLayerManager());
    }
}

From source file:com.vectorprint.report.itext.style.stylers.Underline.java

License:Open Source License

@Override
public void draw(Rectangle rect, String genericTag) throws VectorPrintException {
    if (genericTag == null) {
        if (log.isLoggable(Level.FINE)) {
            log.fine("not drawing underline because genericTag is null (no data for underline)");
        }/*w  w  w. ja va2  s .c om*/
        return;
    }
    DelayedData delayed = getDelayed(genericTag);
    PdfContentByte canvas = getPreparedCanvas();
    try {
        com.itextpdf.text.Font f = delayed.getChunk().getFont();
        canvas.setColorStroke((getColor() == null) ? f.getColor() : itextHelper.fromColor(getColor()));
        canvas.setLineWidth(getLineWidth());
        if (delayed.getChunk().getTextRise() != 0) {
            canvas.transform(AffineTransform.getTranslateInstance(0, delayed.getChunk().getTextRise()));
        }
        HashMap<String, Object> attributes = delayed.getChunk().getAttributes();
        if (attributes != null && attributes.containsKey(Chunk.SKEW)) {
            log.warning("lines under skewed text may not be at the correct position");
        }
        canvas.moveTo(rect.getLeft(), rect.getBottom());
        canvas.lineTo(rect.getLeft() + rect.getWidth(), rect.getBottom());
        canvas.stroke();
    } catch (Exception ex) {
        resetCanvas(canvas);
        throw new VectorPrintException(ex);
    }
    resetCanvas(canvas);
}

From source file:ConexionBD.CreaPrefichaPDF.java

public static void drawRectangle(PdfContentByte content, float x, float y, float width, float height) {
    content.saveState();//  w w  w. j a v a2  s  . c o  m
    PdfGState state = new PdfGState();
    content.setGState(state);
    content.setRGBColorFill(232, 232, 232);
    content.setColorStroke(BaseColor.BLUE);
    content.setLineWidth((float) .5);
    content.rectangle(x, y, width, height);
    content.fillStroke();
    content.restoreState();
}

From source file:ConexionBD.CreaPrefichaPDF.java

public static void drawRectangleSC(PdfContentByte content, float x, float y, float width, float height) {
    content.saveState();/*from   w w  w  . j a  va 2  s .  c o  m*/
    PdfGState state = new PdfGState();
    content.setGState(state);
    content.setRGBColorFill(0xFF, 0xFF, 0xFA);
    content.setColorStroke(BaseColor.BLUE);
    content.setLineWidth((float) .5);
    content.rectangle(x, y, width, height);
    content.fillStroke();
    content.restoreState();
}

From source file:ConexionBD.CreaPrefichaPDF.java

public static void drawRectangleText(PdfContentByte content, float x, float y, float width, float height) {
    content.saveState();//  w  w  w. jav a 2  s .  com
    PdfGState state = new PdfGState();
    content.setGState(state);
    content.setRGBColorFill(0, 230, 255);
    content.setColorStroke(BaseColor.BLUE);
    content.setLineWidth((float) .5);
    content.rectangle(x, y, width, height);
    content.fillStroke();
    content.restoreState();
}