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 void setLineDash(float unitsOn, float unitsOff, float phase) 

Source Link

Document

Changes the value of the line dash pattern.

Usage

From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java

License:Open Source License

private void buildFooter(final PdfWriter writer, final Document document) {
    PdfContentByte cb = writer.getDirectContent();

    cb.saveState();/* w  ww .j  a  v  a  2  s .  c o  m*/

    String text = footer.getPage() + " " + writer.getPageNumber() + " " + footer.getIn() + " ";

    float textBase = document.bottom() - 25;
    float textSize = FontUtils.getDejavu().getWidthPoint(text, 7);

    cb.setColorFill(ColorUtils.getLightColor());
    cb.setColorStroke(ColorUtils.getLightColor());
    cb.setLineWidth(1);
    cb.setLineDash(2, 2, 1);
    cb.moveTo(document.left(), document.bottom() - 10);
    cb.lineTo(document.right(), document.bottom() - 10);
    cb.stroke();
    cb.beginText();
    cb.setFontAndSize(FontUtils.getDejavu(), 7);

    float adjust = FontUtils.getDejavu().getWidthPoint("0", 7);

    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);

    textSize = FontUtils.getDejavu().getWidthPoint(footer.getGeneratedBy(), 7);

    cb.setTextMatrix(document.right() - textSize, textBase - 10);
    cb.showText(footer.getGeneratedBy());

    textSize = FontUtils.getDejavu().getWidthPoint(generationDate, 7);

    cb.setTextMatrix(document.right() - textSize, textBase - 20);
    cb.showText(generationDate);
    cb.endText();

    try {
        textSize = FontUtils.getDejavu().getWidthPoint(footer.getAdditionalText(), 7);

        ColumnText ct = new ColumnText(cb);

        ct.setSimpleColumn(new Phrase(footer.getAdditionalText(), FontUtils.getDejavuRegular7Light()),
                document.left() + 240, textBase + 10, document.left() + 390, textBase - 25, 10,
                Element.ALIGN_LEFT);
        ct.go();
    } catch (DocumentException e) {
        LOG.warn("Problem with additional text generation in report footer.");
    }

    try {
        ColumnText ct = new ColumnText(cb);

        ct.setSimpleColumn(document.left(), textBase + 10, document.left() + 230, textBase - 25, 10,
                Element.ALIGN_LEFT);
        ct.addText(new Phrase(footer.getCompanyName() + "\n", FontUtils.getDejavuRegular7Light()));

        if (!"".equals(footer.getAddress())) {
            ct.addText(new Phrase(footer.getAddress() + "\n", FontUtils.getDejavuRegular7Light()));
        }
        if (!"".equals(footer.getPhoneEmail())) {
            ct.addText(new Phrase(footer.getPhoneEmail(), FontUtils.getDejavuRegular7Light()));
        }

        ct.go();
    } catch (DocumentException e) {
        LOG.warn("Problem with company text generation in report footer.");
    }

    cb.addTemplate(total, document.right() - adjust, textBase);
    cb.restoreState();

}

From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java

License:Open Source License

private void buildHeader(final PdfWriter writer, final Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();//from w  ww . ja  va 2  s.  c  o m
    String text = footer.getPage() + " " + writer.getPageNumber() + " " + footer.getIn() + " ";

    float textBase = document.top() + 22;
    float textSize = FontUtils.getDejavu().getWidthPoint(text, 7);

    cb.setColorFill(ColorUtils.getLightColor());
    cb.setColorStroke(ColorUtils.getLightColor());
    cb.beginText();
    cb.setFontAndSize(FontUtils.getDejavu(), 7);

    float adjust = FontUtils.getDejavu().getWidthPoint("0", 7);

    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(total, document.right() - adjust, textBase);
    cb.setLineWidth(1);
    cb.setLineDash(2, 2, 1);
    cb.moveTo(document.left(), document.top() + 12);
    cb.lineTo(document.right(), document.top() + 12);
    cb.stroke();
    cb.restoreState();

}

From source file:com.t2.compassionMeditation.ViewSessionsActivity.java

License:Open Source License

/**
 * Create a PDF file based on the contents of the graph
 *//*from  w ww  . jav a  2  s.  c om*/
void CreatePdf() {

    // Run the export on a separate thread.
    new Thread(new Runnable() {
        @Override
        public void run() {

            Document document = new Document();
            try {
                Date calendar = Calendar.getInstance().getTime();
                mResultsFileName = "BioZenResults_";
                mResultsFileName += (calendar.getYear() + 1900) + "-" + (calendar.getMonth() + 1) + "-"
                        + calendar.getDate() + "_";
                mResultsFileName += calendar.getHours() + "-" + calendar.getMinutes() + "-"
                        + calendar.getSeconds() + ".pdf";

                PdfWriter writer = PdfWriter.getInstance(document,
                        new FileOutputStream(android.os.Environment.getExternalStorageDirectory()
                                + java.io.File.separator + mResultsFileName));
                document.open();
                PdfContentByte contentByte = writer.getDirectContent();
                BaseFont baseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
                        BaseFont.NOT_EMBEDDED);
                // Note top of PDF = 900
                float chartWidth = 332;
                float chartHeight = 45;

                float spaceHeight = chartHeight + 30;
                int horizontalPos = 180;
                float verticalPos = 780;

                // Write document header
                contentByte.beginText();
                contentByte.setFontAndSize(baseFont, 20);
                contentByte.showTextAligned(PdfContentByte.ALIGN_CENTER, "T2 BioZen Report", 300, 800, 0);
                contentByte.showTextAligned(PdfContentByte.ALIGN_CENTER,
                        "Generated on: " + calendar.toLocaleString(), 300, 770, 0);
                contentByte.endText();

                contentByte.setLineWidth(1f);
                verticalPos -= spaceHeight;
                long startTime = startCal.getTimeInMillis();
                long endTime = endCal.getTimeInMillis();

                float maxChartValue = 0;
                float chartYAvg;

                BioSession tmpSession = sessionItems.get(0);
                int maxKeys = tmpSession.keyItemNames.length;

                // Loop through all of the the keys
                for (int key = 0; key < maxKeys; key++) {

                    //Draw a border rect
                    contentByte.setRGBColorStrokeF(0, 0, 0);
                    contentByte.setLineWidth(1f);
                    contentByte.rectangle(horizontalPos, verticalPos, chartWidth, chartHeight);
                    contentByte.stroke();

                    // Write band name
                    contentByte.beginText();
                    contentByte.setFontAndSize(baseFont, 12);
                    BioSession tmpSession1 = sessionItems.get(0);
                    contentByte.showTextAligned(PdfContentByte.ALIGN_RIGHT, tmpSession1.keyItemNames[key], 170,
                            (verticalPos + (chartHeight / 2)) - 5, 0);
                    contentByte.endText();

                    maxChartValue = 0;
                    // First find the max Y
                    for (BioSession session : sessionItems) {
                        if (session.time >= startTime && session.time <= endTime) {
                            chartYAvg = session.avgFilteredValue[key];
                            if (chartYAvg > maxChartValue)
                                maxChartValue = chartYAvg;
                        }
                    }

                    float lastY = -1;
                    float xIncrement = 0;
                    if (sessionItems.size() > 0) {
                        xIncrement = chartWidth / sessionItems.size();
                    }

                    float yIncrement = 0;
                    if (maxChartValue > 0) {
                        yIncrement = chartHeight / maxChartValue;
                    }

                    float highValue = 0;
                    int highTime = 0;
                    float highY = 0;
                    float highX = 0;
                    int lowTime = 0;
                    float lowY = 100;
                    float lowX = chartWidth;
                    float lowValue = maxChartValue;

                    int lCount = 0;
                    String keyName = "";

                    ArrayList<RegressionItem> ritems = new ArrayList<RegressionItem>();

                    // Loop through the session points of this key
                    String rawYValues = "";
                    for (BioSession session : sessionItems) {
                        keyName = session.keyItemNames[key];
                        if (session.time >= startTime && session.time <= endTime) {
                            chartYAvg = session.avgFilteredValue[key];
                            rawYValues += chartYAvg + ", ";
                            if (lastY < 0)
                                lastY = (float) chartYAvg;

                            contentByte.setLineWidth(3f);
                            contentByte.setRGBColorStrokeF(255, 0, 0);

                            float graphXFrom = horizontalPos + (lCount * xIncrement);
                            float graphYFrom = verticalPos + (lastY * yIncrement);
                            float graphXTo = (horizontalPos + ((lCount + 1) * xIncrement));
                            float graphYTo = verticalPos + (chartYAvg * yIncrement);
                            //                     Log.e(TAG, "[" + graphXFrom + ", " + graphYFrom + "] to [" + graphXTo + ", " + graphYTo + "]");
                            // Draw the actual graph
                            contentByte.moveTo(graphXFrom, graphYFrom);
                            contentByte.lineTo(graphXTo, graphYTo);
                            contentByte.stroke();

                            //Add regression Item
                            ritems.add(new RegressionItem(lCount, (chartYAvg * yIncrement)));

                            if (chartYAvg > highValue) {
                                highValue = chartYAvg;
                                highY = graphYTo;
                                highX = graphXTo;
                                highTime = (int) (session.time / 1000);
                            }

                            if (chartYAvg < lowValue) {
                                lowValue = chartYAvg;
                                lowY = graphYTo;
                                lowX = graphXTo;
                                lowTime = (int) (session.time / 1000);
                            }

                            lCount++;
                            lastY = (float) chartYAvg;

                        } // End if (session.time >= startTime && session.time <= endTime )
                    } // End for (BioSession session : sessionItems)            

                    //Draw high low dates
                    if (highY != 0 && lowY != 0) {
                        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
                        String hDate = dateFormat.format(new Date((long) highTime * 1000L));
                        String lDate = dateFormat.format(new Date((long) lowTime * 1000L));
                        contentByte.beginText();
                        contentByte.setFontAndSize(baseFont, 8);
                        contentByte.showTextAligned(PdfContentByte.ALIGN_CENTER, hDate, highX, highY, 0);
                        contentByte.showTextAligned(PdfContentByte.ALIGN_CENTER, lDate, lowX, lowY, 0);
                        contentByte.endText();
                    }

                    //Draw Regression Line
                    RegressionResult regression = calculateRegression(ritems);
                    contentByte.saveState();
                    contentByte.setRGBColorStrokeF(0, 0, 250);
                    contentByte.setLineDash(3, 3, 0);
                    contentByte.moveTo(horizontalPos, verticalPos + (float) regression.intercept);
                    contentByte.lineTo(horizontalPos + chartWidth, (float) ((verticalPos + regression.intercept)
                            + (float) (regression.slope * (chartWidth / xIncrement))));
                    contentByte.stroke();
                    contentByte.restoreState();
                    contentByte.setRGBColorStrokeF(0, 0, 0);

                    //               Log.e(TAG, keyName + ": [" + rawYValues + "]");
                    // Get ready for the next key (and series of database points )
                    verticalPos -= spaceHeight;

                    if (verticalPos < 30) {
                        document.newPage();
                        verticalPos = 780 - spaceHeight;
                    }

                } // End for (int key = 0; key < maxKeys; key++)

                //document.add(new Paragraph("You can also write stuff directly tot he document like this!"));
            } catch (DocumentException de) {
                System.err.println(de.getMessage());
                Log.e(TAG, de.toString());
            } catch (IOException ioe) {
                System.err.println(ioe.getMessage());
                Log.e(TAG, ioe.toString());
            } catch (Exception e) {
                System.err.println(e.getMessage());
                Log.e(TAG, e.toString());
            }

            // step 5: we close the document
            document.close();
            fileExportCompleteHandler.sendEmptyMessage(EXPORT_SUCCESS);

        }
    }).start();

}