Example usage for com.lowagie.text.pdf PdfContentByte setLineDash

List of usage examples for com.lowagie.text.pdf PdfContentByte setLineDash

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfContentByte setLineDash.

Prototype


public final void setLineDash(float[] array, float phase) 

Source Link

Document

Changes the value of the line dash pattern.

Usage

From source file:br.org.archimedes.io.pdf.PDFWriter.java

License:Open Source License

public void write(Layer layer) {

    PdfContentByte cb = helper.getPdfContentByte();
    cb.setLineWidth((float) layer.getThickness());
    Color layerColor = layer.getPrintColor();
    cb.setRGBColorStroke(layerColor.getRed(), layerColor.getGreen(), layerColor.getBlue());
    if (layer.getLineStyle() == LineStyle.STIPPED) {
        cb.setLineDash(5, 0);
    } else {/*w  w  w  . jav  a2s .c  om*/
        cb.setLineDash(0);
    }

    ElementEPLoader elementEPLoader = new ElementEPLoader();
    ElementExporterEPLoader exporterLoader = new ElementExporterEPLoader();

    for (Element element : layer.getElements()) {
        String elementId = elementEPLoader.getElementId(element);
        ElementExporter<Element> exporter = exporterLoader.getExporter(elementId);
        try {
            exporter.exportElement(element, helper);
        } catch (IOException e) {
            // Something went wrong when writting this element.
            // Just skip it and trace the log.
            e.printStackTrace();
        } catch (NotSupportedException e) {
            // wont reach here
        }
    }
}

From source file:classroom.filmfestival_c.Movies19.java

protected static void drawGrid(PdfWriter writer, List<String> places, String day, float left,
        float column_width, float top, float row_height) {

    // CANVAS/*from   ww w  . j  a  v a 2s  . c o  m*/
    PdfContentByte directcontent = writer.getDirectContent();
    Rectangle art = writer.getBoxSize("art");

    // LINES

    directcontent.setLineWidth(1);
    float bottom = art.getTop();

    // rows
    int rows = places.size();
    for (int i = 0; i <= rows; i++) {
        directcontent.moveTo(art.getLeft(), art.getTop() - (i * row_height));
        directcontent.lineTo(art.getRight(), art.getTop() - (i * row_height));
        bottom = art.getTop() - (i * row_height);
    }

    // Rectangle
    directcontent.moveTo(art.getLeft(), art.getTop());
    directcontent.lineTo(art.getLeft(), bottom);
    directcontent.moveTo(left, art.getTop());
    directcontent.lineTo(left, bottom);
    directcontent.moveTo(art.getLeft() + (COLUMNS * column_width), art.getTop());
    directcontent.lineTo(art.getLeft() + (COLUMNS * column_width), bottom);
    directcontent.stroke();

    // columns
    directcontent.saveState();
    directcontent.setLineWidth(0.3f);
    directcontent.setColorStroke(SILVER);
    directcontent.setLineDash(3, 1);
    for (int i = 2; i < COLUMNS; i++) {
        directcontent.moveTo(art.getLeft() + (i * column_width), art.getTop());
        directcontent.lineTo(art.getLeft() + (i * column_width), bottom);
    }
    directcontent.stroke();
    directcontent.restoreState();

    // TEXT

    // date
    directcontent.beginText();
    directcontent.setFontAndSize(FONT, 12);
    directcontent.showTextAligned(Element.ALIGN_RIGHT, day, art.getLeft() - 5, art.getTop(), 90);
    directcontent.endText();

    // time
    for (int i = 1; i < COLUMNS; i++) {
        directcontent.beginText();
        directcontent.setFontAndSize(FONT, 8);
        directcontent.showTextAligned(Element.ALIGN_LEFT, TIME[i - 1], art.getLeft() + (i * column_width) + 5,
                top + 5, 90);
        directcontent.endText();
    }

    // places
    for (int i = 0; i < rows; i++) {
        directcontent.beginText();
        directcontent.setFontAndSize(FONT, 12);
        directcontent.showTextAligned(Element.ALIGN_CENTER, places.get(i), art.getLeft() + 16,
                art.getTop() - ((i + 0.5f) * row_height), 90);
        directcontent.endText();
    }
}

From source file:org.jaffa.modules.printing.services.FormPrintEngineIText.java

License:Open Source License

/**
 * This will fill in the page with data,
 * m_currentPageData contains the details of the current page being printed
 * @throws FormPrintException Thrown if there is any form processing problems
 *///  ww w . j  av a 2s.co  m
protected void fillPageFields() throws FormPrintException {
    log.debug("fillPageFields: Page=" + getCurrentPage());

    try {
        PdfContentByte cb = m_writer.getDirectContent();
        PageDetailsExtended page = (PageDetailsExtended) getCurrentPageData();

        //                // Test code to throw a barcode on the page...
        //                Barcode39 code39 = new Barcode39();
        //                code39.setCode("CODE39-1234567890");
        //                code39.setStartStopText(false);
        //                code39.setSize(0);
        //                Image image39 = code39.createImageWithBarcode(cb, null, null);
        //                com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(2);
        //                table.setWidthPercentage(100);
        //                table.getDefaultCell().setBorder(com.lowagie.text.Rectangle.NO_BORDER);
        //                table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        //                table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
        //                table.getDefaultCell().setFixedHeight(70);
        //                table.addCell("CODE 39");
        //                table.addCell(new Phrase(new Chunk(image39, 0, 0)));
        //                m_generatedDoc.add(table);
        //                //--------------------------------------------------

        // Loop through each field to be inserted
        for (Iterator i = page.fieldList.iterator(); i.hasNext();) {
            String fieldname = (String) i.next();

            // Get the properties for displaying this field
            FieldProperties props = (FieldProperties) page.fieldProperties.get(fieldname);

            // Get the data to display
            FormPrintEngine.DomValue data = new FormPrintEngine.DomValue(fieldname, props.sampleData);

            // Caluclate Clipping Region
            float x1 = Math.min(props.x1, props.x2);
            float x2 = Math.max(props.x1, props.x2);
            float y1 = Math.min(props.y1, props.y2);
            float y2 = Math.max(props.y1, props.y2);
            float w = Math.abs(props.x1 - props.x2) + 1;
            float h = Math.abs(props.y1 - props.y2) + 1;

            if (log.isDebugEnabled())
                log.debug("Print Field " + fieldname + "=" + data.getObject() + " @ [(" + x1 + "," + y1 + ")->("
                        + x2 + "," + y2 + ")]");

            // Default the font if not specified
            String font = BaseFont.HELVETICA;
            if (props.fontFace != null)
                font = props.fontFace;

            // Handle Barcodes diffently withing iText, don't just use fonts
            if (font.startsWith("Barcode")) {
                String bcClassName = "com.lowagie.text.pdf." + font;
                Object bcode = null;
                String dataStr = data.getValue();
                if (dataStr != null) {
                    log.debug("Barcode Data String = " + dataStr);
                    // Try and create the correct Barcode Object
                    try {
                        Class bcClass = Class.forName(bcClassName);
                        bcode = bcClass.newInstance();
                    } catch (Exception e) {
                        String err = "Can't Create Barcode Object for barcode type '" + font + "' on field "
                                + fieldname;
                        log.error(err, e);
                    }

                    // Only continue if the barcode object was created
                    if (bcode != null) {

                        // Generate and Print barcode, based on common interface
                        if (bcode instanceof Barcode) {
                            Barcode b = (Barcode) bcode;
                            // Set some default output a barcode
                            b.setCode(dataStr);
                            if (props.fontSize <= 0) {
                                // Hide text if font size is 0, and make the barcode height the size of the box
                                b.setBarHeight(h);
                                b.setFont(null);
                            } else {
                                b.setSize(props.fontSize); // size of text under barcode
                                b.setBarHeight(h - props.fontSize - 5); // Adjust Bar Height to allow for font size
                            }
                            b.setN(2); // Wide Bars

                            // Set custom parameters
                            setBarcodeParams(fieldname, bcode, props.style);

                            // Print out barcode
                            Image image = ((Barcode) bcode).createImageWithBarcode(cb, null, null);
                            printImage(image, cb, x1, y1, x2, y2, props.align, props.fitMethod, props.rotate);

                        } else

                        // Print PDF417 barcode, not based on common interface
                        if (bcode instanceof BarcodePDF417) {
                            BarcodePDF417 b = (BarcodePDF417) bcode;
                            // Set some default output a barcode
                            b.setText(dataStr);
                            b.setErrorLevel(5);
                            // Set custom parameters
                            setBarcodeParams(fieldname, bcode, props.style);
                            log.debug("PDF417 Settings\n" + "BitColumns=" + b.getBitColumns() + "\n"
                                    + "CodeColumns=" + b.getCodeColumns() + "\n" + "CodeRows=" + b.getCodeRows()
                                    + "\n" + "ErrorLevel=" + b.getErrorLevel() + "\n" + "YHeight="
                                    + b.getYHeight() + "\n" + "AspectRatio=" + b.getAspectRatio() + "\n"
                                    + "Options=" + b.getOptions() + "\n" + "LenCodewords="
                                    + b.getLenCodewords());

                            // Print out barcode
                            //image = b.getImage();
                            printImage(b.getImage(), cb, x1, y1, x2, y2, props.align, props.fitMethod,
                                    props.rotate);

                        } else {
                            // Error, unknown barcode
                            String err = "Error, No print handler for barcode object "
                                    + bcode.getClass().getName();
                            log.error(err);
                            //throw new EngineProcessingException(err);
                        }
                    }
                } else
                    log.debug("SKIPPED BARCODE : No data for " + fieldname);

                // Handle Images differently within iText, native support for JFreeChart
            } else if ("image".equalsIgnoreCase(font)) {
                try {
                    java.awt.Image image = data.getDomImage();
                    // Add an image to the page
                    if (image != null) {
                        if (fieldname.startsWith("watermark")) {
                            // Add an image-based watermark to the under content layer
                            PdfContentByte contentUnder = m_writer.getDirectContentUnder();
                            if (props.opacity != 1f) {
                                PdfGState gs = new PdfGState();
                                gs.setFillOpacity(props.opacity);
                                contentUnder.setGState(gs);
                            }
                            printImage(image, contentUnder, x1, y1, x2, y2, props.align, props.fitMethod,
                                    props.rotate);
                        } else {
                            // Add an image to main page layer
                            printImage(image, cb, x1, y1, x2, y2, props.align, props.fitMethod, props.rotate);
                        }
                    }
                } catch (IOException e) {
                    // Add Error on page.
                    Phrase text = new Phrase("Image Error", FontFactory
                            .getFont(FontFactory.HELVETICA_BOLDOBLIQUE, 8f, 0, ColorHelper.getColor("red")));
                    ColumnText ct = new ColumnText(cb);
                    ct.setSimpleColumn(text, x1, y1, x2, y2, 8f, Element.ALIGN_LEFT);
                }
            } else if (fieldname.startsWith("watermark")) {
                // Add a text-based watermark
                String text = data.getValue();
                PdfContentByte contentUnder = m_writer.getDirectContentUnder();
                if (props.opacity != 1f) {
                    PdfGState gs = new PdfGState();
                    gs.setFillOpacity(props.opacity);
                    contentUnder.setGState(gs);
                }
                // The text aligns (left, center, right) on the pivot point.
                // Default to align left.
                float pivotX = x1;
                float pivotY = y1;
                if (Element.ALIGN_CENTER == props.align) {
                    pivotX = (x1 / 2) + (x2 / 2);
                    pivotY = y1;
                } else if (Element.ALIGN_RIGHT == props.align) {
                    pivotX = x2;
                    pivotY = y1;
                }
                Phrase watermark = new Phrase(text, FontFactory.getFont(props.fontFace, props.fontSize,
                        decodeFontStyle(props.style), ColorHelper.getColor(defaultWatermarkColor)));
                ColumnText.showTextAligned(contentUnder, props.align, watermark, pivotX, pivotY, props.rotate);
            } else {
                // Handle printing of basic Text
                float lineHeight = props.fontSize;
                String str = data.getValue();
                if (str != null) {
                    // Add a bounded column to add text to.
                    Phrase text = new Phrase(str, FontFactory.getFont(props.fontFace, props.fontSize,
                            decodeFontStyle(props.style), ColorHelper.getColor(props.color)));
                    ColumnText ct = new ColumnText(cb);
                    if (props.fitMethod == FIT_METHOD_CLIP)
                        // set up column with height/width restrictions
                        ct.setSimpleColumn(text, x1, y1, x2, y2, lineHeight, props.align);
                    else
                        // set up column without (i.e. large) height/width restrictions
                        ct.setSimpleColumn(text, x1, y1, 1000, 0, lineHeight, props.align);
                    ct.go();
                }
            }

            // Draw outline boxes arround fields
            if (isTemplateMode()) {
                cb.setLineWidth(0.5f);
                cb.setLineDash(4f, 2f);
                cb.setColorStroke(new Color(0xA0, 0xA0, 0xA0));
                cb.moveTo(x1, y1);
                cb.lineTo(x1, y2);
                cb.lineTo(x2, y2);
                cb.lineTo(x2, y1);
                cb.lineTo(x1, y1);
                cb.stroke();
            }
        } // end for-loop
    } catch (DocumentException e) {
        String err = "Error printing data - " + e.getMessage();
        log.error(err, e);
        throw new EngineProcessingException(err);
        //        } catch (IOException e) {
        //            String err = "Error printing data - " + e.getMessage();
        //            log.error(err ,e);
        //            throw new EngineProcessingException(err);
    }

}

From source file:org.mapfish.print.map.renderers.vector.LineStringRenderer.java

License:Open Source License

protected static void applyStyle(RenderingContext context, PdfContentByte dc, PJsonObject style,
        PdfGState state) {/*from   www .  j  av  a 2 s  .c o m*/
    if (style == null)
        return;
    if (style.optString("strokeColor") != null) {
        dc.setColorStroke(ColorWrapper.convertColor(style.getString("strokeColor")));
    }
    if (style.optString("strokeOpacity") != null) {
        state.setStrokeOpacity(style.getFloat("strokeOpacity"));
    }
    final float width = style.optFloat("strokeWidth", 1) * context.getStyleFactor();
    dc.setLineWidth(width);
    final String linecap = style.optString("strokeLinecap");
    if (linecap != null) {
        if (linecap.equalsIgnoreCase("butt")) {
            dc.setLineCap(PdfContentByte.LINE_CAP_BUTT);
        } else if (linecap.equalsIgnoreCase("round")) {
            dc.setLineCap(PdfContentByte.LINE_CAP_ROUND);
        } else if (linecap.equalsIgnoreCase("square")) {
            dc.setLineCap(PdfContentByte.LINE_CAP_PROJECTING_SQUARE);
        } else {
            throw new InvalidValueException("strokeLinecap", linecap);
        }
    }
    final String dashStyle = style.optString("strokeDashstyle");
    if (dashStyle != null) {
        if (dashStyle.equalsIgnoreCase("dot")) {
            final float[] def = new float[] { 0.1f, 2 * width };
            dc.setLineDash(def, 0);
        } else if (dashStyle.equalsIgnoreCase("dash")) {
            final float[] def = new float[] { 2 * width, 2 * width };
            dc.setLineDash(def, 0);
        } else if (dashStyle.equalsIgnoreCase("dashdot")) {
            final float[] def = new float[] { 3 * width, 2 * width, 0.1f, 2 * width };
            dc.setLineDash(def, 0);
        } else if (dashStyle.equalsIgnoreCase("longdash")) {
            final float[] def = new float[] { 4 * width, 2 * width };
            dc.setLineDash(def, 0);
        } else if (dashStyle.equalsIgnoreCase("longdashdot")) {
            final float[] def = new float[] { 5 * width, 2 * width, 0.1f, 2 * width };
            dc.setLineDash(def, 0);
        } else if (dashStyle.equalsIgnoreCase("solid")) {

        } else {
            throw new InvalidValueException("strokeDashstyle", dashStyle);
        }
    }
}

From source file:org.unitime.timetable.export.PDFPrinter.java

License:Open Source License

@Override
public void printLine(String... fields) {
    PdfPCellEvent setLineDashEvent = new PdfPCellEvent() {
        @Override//from ww w . ja  v a  2s.c  o m
        public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvas) {
            PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
            cb.setLineDash(new float[] { 2, 2 }, 0);
        }
    };

    for (int idx = 0; idx < fields.length; idx++) {
        if (iHiddenColumns.contains(idx))
            continue;
        String f = fields[idx];
        if (f == null || f.isEmpty() || (iCheckLast
                && f.equals(iLastLine == null || idx >= iLastLine.length ? null : iLastLine[idx])))
            f = "";

        boolean number = sNumber.matcher(f).matches();

        Font font = PdfFont.getFont();
        Phrase p = new Phrase(f, PdfFont.getSmallFont());

        PdfPCell cell = new PdfPCell(p);
        cell.setBorder(iLastLine == null ? Rectangle.TOP : Rectangle.NO_BORDER);
        cell.setVerticalAlignment(Element.ALIGN_TOP);
        cell.setHorizontalAlignment(number ? Element.ALIGN_RIGHT : Element.ALIGN_LEFT);
        cell.setPaddingBottom(4f);
        cell.setCellEvent(setLineDashEvent);
        if (number)
            cell.setPaddingRight(10f);
        iTable.addCell(cell);

        float width = 0;
        if (f.indexOf('\n') >= 0) {
            for (StringTokenizer s = new StringTokenizer(f, "\n"); s.hasMoreTokens();)
                width = Math.max(width, font.getBaseFont().getWidthPoint(s.nextToken(), font.getSize()));
        } else
            width = Math.max(width, font.getBaseFont().getWidthPoint(f, font.getSize()));
        iMaxWidth[idx] = Math.max(iMaxWidth[idx], width + (number ? 10 : 0));
    }
    iLastLine = fields;
}