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

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

Introduction

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

Prototype


public void stroke() 

Source Link

Document

Strokes the path.

Usage

From source file:org.revager.export.PDFPageEventHelper.java

License:Open Source License

@Override
public void onEndPage(PdfWriter writer, Document document) {
    int columnNumber;

    try {/*from   w ww . java 2s. c  o  m*/
        Rectangle page = document.getPageSize();
        float pageWidth = page.getWidth() - document.leftMargin() - document.rightMargin();

        /*
         * Write marks
         */
        setMarks(writer, document);

        /*
         * Define fonts
         */
        headBaseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        Font headFont = new Font(headBaseFont, headFontSize);

        footBaseFont = BaseFont.createFont(BaseFont.HELVETICA_OBLIQUE, BaseFont.CP1252, BaseFont.EMBEDDED);
        Font footFont = new Font(footBaseFont, footFontSize);

        /*
         * Cell fill for space between head/foot and content
         */
        PdfPCell cellFill = new PdfPCell();
        cellFill.setMinimumHeight(PDFTools.cmToPt(0.8f));
        cellFill.setBorderWidth(0);

        /*
         * Write head
         */
        if (headLogoPath != null) {
            columnNumber = 2;
        } else {
            columnNumber = 1;
        }

        PdfPTable head = new PdfPTable(columnNumber);

        Phrase phraseTitle = new Phrase(headTitle, headFont);

        PdfPCell cellTitle = new PdfPCell(phraseTitle);
        cellTitle.setHorizontalAlignment(Element.ALIGN_LEFT);
        cellTitle.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cellTitle.setPaddingTop(0);
        cellTitle.setPaddingBottom(PDFTools.cmToPt(0.2f));
        cellTitle.setPaddingLeft(0);
        cellTitle.setPaddingRight(0);
        cellTitle.setBorderWidthTop(0);
        cellTitle.setBorderWidthBottom(0.5f);
        cellTitle.setBorderWidthLeft(0);
        cellTitle.setBorderWidthRight(0);

        head.addCell(cellTitle);

        if (headLogoPath != null) {
            Image headLogo = Image.getInstance(headLogoPath);
            headLogo.scaleToFit(PDFTools.cmToPt(5.0f), PDFTools.cmToPt(1.1f));

            PdfPCell cellLogo = new PdfPCell(headLogo);
            cellLogo.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cellLogo.setVerticalAlignment(Element.ALIGN_BOTTOM);
            cellLogo.setPaddingTop(0);
            cellLogo.setPaddingBottom(PDFTools.cmToPt(0.15f));
            cellLogo.setPaddingLeft(0);
            cellLogo.setPaddingRight(0);
            cellLogo.setBorderWidthTop(0);
            cellLogo.setBorderWidthBottom(0.5f);
            cellLogo.setBorderWidthLeft(0);
            cellLogo.setBorderWidthRight(0);

            head.addCell(cellLogo);

            head.addCell(cellFill);
        }

        head.addCell(cellFill);

        head.setTotalWidth(pageWidth);
        head.writeSelectedRows(0, -1, document.leftMargin(),
                page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());

        /*
         * Write foot
         */
        if (footText == null) {
            footText = " ";
        }

        PdfPTable foot = new PdfPTable(1);

        foot.addCell(cellFill);

        PdfPCell cellFootText = new PdfPCell(new Phrase(footText, footFont));
        cellFootText.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cellFootText.setVerticalAlignment(Element.ALIGN_TOP);
        cellFootText.setPaddingTop(PDFTools.cmToPt(0.15f));
        cellFootText.setPaddingBottom(0);
        cellFootText.setPaddingLeft(0);
        cellFootText.setPaddingRight(0);
        cellFootText.setBorderWidthTop(0.5f);
        cellFootText.setBorderWidthBottom(0);
        cellFootText.setBorderWidthLeft(0);
        cellFootText.setBorderWidthRight(0);

        foot.addCell(cellFootText);

        /*
         * Print page numbers
         */
        PdfContentByte contentByte = writer.getDirectContent();
        contentByte.saveState();

        String text = MessageFormat.format(translate("Page {0} of") + " ", writer.getPageNumber());

        float textSize = footBaseFont.getWidthPoint(text, footFontSize);
        float textBase = document.bottom() - PDFTools.cmToPt(1.26f);
        contentByte.beginText();
        contentByte.setFontAndSize(footBaseFont, footFontSize);

        float adjust;
        if (footText.trim().equals("")) {
            adjust = (pageWidth / 2) - (textSize / 2) - footBaseFont.getWidthPoint("0", footFontSize);
        } else {
            adjust = 0;
        }

        contentByte.setTextMatrix(document.left() + adjust, textBase);
        contentByte.showText(text);
        contentByte.endText();
        contentByte.addTemplate(template, document.left() + adjust + textSize, textBase);

        contentByte.stroke();
        contentByte.restoreState();

        foot.setTotalWidth(pageWidth);
        foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
                writer.getDirectContent());
    } catch (Exception e) {
        /*
         * Not part of unit testing because this exception is only thrown if
         * an internal error occurs.
         */
        throw new ExceptionConverter(e);
    }
}

From source file:org.xhtmlrenderer.pdf.ITextOutputDevice.java

License:Open Source License

private void followPath(Shape s, int drawType) {
    PdfContentByte cb = _currentPage;
    if (s == null)
        return;/* w w  w .j  ava  2s.  c  om*/

    if (drawType == STROKE) {
        if (!(_stroke instanceof BasicStroke)) {
            s = _stroke.createStrokedShape(s);
            followPath(s, FILL);
            return;
        }
    }
    if (drawType == STROKE) {
        setStrokeDiff(_stroke, _oldStroke);
        _oldStroke = _stroke;
        ensureStrokeColor();
    } else if (drawType == FILL) {
        ensureFillColor();
    }

    PathIterator points;
    if (drawType == CLIP) {
        points = s.getPathIterator(IDENTITY);
    } else {
        points = s.getPathIterator(_transform);
    }
    float[] coords = new float[6];
    int traces = 0;
    while (!points.isDone()) {
        ++traces;
        int segtype = points.currentSegment(coords);
        normalizeY(coords);
        switch (segtype) {
        case PathIterator.SEG_CLOSE:
            cb.closePath();
            break;

        case PathIterator.SEG_CUBICTO:
            cb.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
            break;

        case PathIterator.SEG_LINETO:
            cb.lineTo(coords[0], coords[1]);
            break;

        case PathIterator.SEG_MOVETO:
            cb.moveTo(coords[0], coords[1]);
            break;

        case PathIterator.SEG_QUADTO:
            System.out.println("Quad to " + coords[0] + " " + coords[1] + " " + coords[2] + " " + coords[3]);
            cb.curveTo(coords[0], coords[1], coords[2], coords[3]);
            break;
        }
        points.next();
    }

    switch (drawType) {
    case FILL:
        if (traces > 0) {
            if (points.getWindingRule() == PathIterator.WIND_EVEN_ODD)
                cb.eoFill();
            else
                cb.fill();
        }
        break;
    case STROKE:
        if (traces > 0)
            cb.stroke();
        break;
    default: // drawType==CLIP
        if (traces == 0)
            cb.rectangle(0, 0, 0, 0);
        if (points.getWindingRule() == PathIterator.WIND_EVEN_ODD)
            cb.eoClip();
        else
            cb.clip();
        cb.newPath();
    }
}

From source file:oscar.eform.util.EFormPDFServlet.java

License:Open Source License

private void writeContent(Properties printCfg, Properties props, Properties measurements, float height,
        PdfContentByte cb) throws Exception {
    for (Enumeration e = printCfg.propertyNames(); e.hasMoreElements();) {
        StringBuilder temp = new StringBuilder(e.nextElement().toString());
        String[] cfgVal = printCfg.getProperty(temp.toString()).split(" *, *");

        String[] fontType = null;
        int fontFlags = 0;
        if (cfgVal[4].indexOf(";") > -1) {
            fontType = cfgVal[4].split(";");
            if (fontType[1].trim().equals("italic"))
                fontFlags = Font.ITALIC;
            else if (fontType[1].trim().equals("bold"))
                fontFlags = Font.BOLD;
            else if (fontType[1].trim().equals("bolditalic"))
                fontFlags = Font.BOLDITALIC;
            else//from   ww  w.ja v a  2s  .co m
                fontFlags = Font.NORMAL;
        } else {
            fontFlags = Font.NORMAL;
            fontType = new String[] { cfgVal[4].trim() };
        }

        String encoding = null;
        if (fontType[0].trim().equals("BaseFont.HELVETICA")) {
            fontType[0] = BaseFont.HELVETICA;
            encoding = BaseFont.CP1252; //latin1 encoding
        } else if (fontType[0].trim().equals("BaseFont.HELVETICA_OBLIQUE")) {
            fontType[0] = BaseFont.HELVETICA_OBLIQUE;
            encoding = BaseFont.CP1252;
        } else if (fontType[0].trim().equals("BaseFont.ZAPFDINGBATS")) {
            fontType[0] = BaseFont.ZAPFDINGBATS;
            encoding = BaseFont.ZAPFDINGBATS;
        } else {
            fontType[0] = BaseFont.COURIER;
            encoding = BaseFont.CP1252;
        }

        BaseFont bf = BaseFont.createFont(fontType[0], encoding, BaseFont.NOT_EMBEDDED);
        String propValue = props.getProperty(temp.toString());
        //if not in regular config then check measurements
        if (propValue == null) {
            propValue = measurements.getProperty(temp.toString(), "");
        }

        ColumnText ct = new ColumnText(cb);
        // write in a rectangle area
        if (cfgVal.length >= 9) {
            Font font = new Font(bf, Integer.parseInt(cfgVal[5].trim()), fontFlags);
            ct.setSimpleColumn(Integer.parseInt(cfgVal[1].trim()),
                    (height - Integer.parseInt(cfgVal[2].trim())), Integer.parseInt(cfgVal[7].trim()),
                    (height - Integer.parseInt(cfgVal[8].trim())), Integer.parseInt(cfgVal[9].trim()),
                    (cfgVal[0].trim().equals("left") ? Element.ALIGN_LEFT
                            : (cfgVal[0].trim().equals("right") ? Element.ALIGN_RIGHT : Element.ALIGN_CENTER)));

            ct.setText(new Phrase(12, propValue, font));
            ct.go();
            continue;
        }

        // draw line directly
        if (temp.toString().startsWith("__$line")) {
            cb.setRGBColorStrokeF(0f, 0f, 0f);
            cb.setLineWidth(Float.parseFloat(cfgVal[4].trim()));
            cb.moveTo(Float.parseFloat(cfgVal[0].trim()), Float.parseFloat(cfgVal[1].trim()));
            cb.lineTo(Float.parseFloat(cfgVal[2].trim()), Float.parseFloat(cfgVal[3].trim()));
            cb.stroke();

        } else if (temp.toString().startsWith("__")) {
            cb.beginText();
            cb.setFontAndSize(bf, Integer.parseInt(cfgVal[5].trim()));
            cb.showTextAligned(
                    (cfgVal[0].trim().equals("left") ? PdfContentByte.ALIGN_LEFT
                            : (cfgVal[0].trim().equals("right") ? PdfContentByte.ALIGN_RIGHT
                                    : PdfContentByte.ALIGN_CENTER)),
                    (cfgVal.length >= 7 ? (cfgVal[6].trim()) : propValue), Integer.parseInt(cfgVal[1].trim()),
                    (height - Integer.parseInt(cfgVal[2].trim())), 0);
            cb.endText();
        } else { // write prop text
            cb.beginText();
            cb.setFontAndSize(bf, Integer.parseInt(cfgVal[5].trim()));
            cb.showTextAligned(
                    (cfgVal[0].trim().equals("left") ? PdfContentByte.ALIGN_LEFT
                            : (cfgVal[0].trim().equals("right") ? PdfContentByte.ALIGN_RIGHT
                                    : PdfContentByte.ALIGN_CENTER)),
                    (cfgVal.length >= 7 ? ((propValue.equals("") ? "" : cfgVal[6].trim())) : propValue),
                    Integer.parseInt(cfgVal[1].trim()), (height - Integer.parseInt(cfgVal[2].trim())), 0);

            cb.endText();
        }
    }
}

From source file:oscar.eform.util.EFormPDFServlet.java

License:Open Source License

private void plotProperties(Properties tp, Properties props, List<String> xDate, List<String> yHeight,
        float height, PdfContentByte cb, boolean countEven) {
    StringBuilder temp = null;//from  w  w  w.j  a  v a2 s . co  m
    String tempValue = null;
    String className = null;
    String[] tempYcoords;
    int origX = 0;
    int origY = 0;
    Properties args = new Properties();

    for (Enumeration e = tp.propertyNames(); e.hasMoreElements();) {
        temp = new StringBuilder(e.nextElement().toString());
        tempValue = tp.getProperty(temp.toString()).trim();
        if (temp.toString().equals("__finalEDB"))
            args.setProperty(temp.toString(), props.getProperty(tempValue));
        else if (temp.toString().equals("__xDateScale"))
            args.setProperty(temp.toString(), props.getProperty(tempValue));
        else if (temp.toString().equals("__dateFormat"))
            args.setProperty(temp.toString(), tempValue);
        else if (temp.toString().equals("__nMaxPixX"))
            args.setProperty(temp.toString(), tempValue);
        else if (temp.toString().equals("__nMaxPixY"))
            args.setProperty(temp.toString(), tempValue);
        else if (temp.toString().equals("__fStartX"))
            args.setProperty(temp.toString(), tempValue);
        else if (temp.toString().equals("__fEndX"))
            args.setProperty(temp.toString(), tempValue);
        else if (temp.toString().equals("__fStartY"))
            args.setProperty(temp.toString(), tempValue);
        else if (temp.toString().equals("__fEndY"))
            args.setProperty(temp.toString(), tempValue);
        else if (temp.toString().equals("__origX"))
            origX = Integer.parseInt(tempValue);
        else if (temp.toString().equals("__origY"))
            origY = Integer.parseInt(tempValue);
        else if (temp.toString().equals("__className"))
            className = tempValue;
        else {
            MiscUtils.getLogger()
                    .debug("Adding xDate " + temp.toString() + " VAL: " + props.getProperty(temp.toString()));
            MiscUtils.getLogger()
                    .debug("Adding yHeight " + tempValue + " VAL: " + props.getProperty(tempValue));
            xDate.add(props.getProperty(temp.toString()));
            yHeight.add(props.getProperty(tempValue));
        }
    } // end for read in from config file                                                

    FrmPdfGraphic pdfGraph = FrmGraphicFactory.create(className);
    pdfGraph.init(args);

    Properties gProp = pdfGraph.getGraphicXYProp(xDate, yHeight);

    //draw the pic
    cb.setLineWidth(1.5f);

    if (countEven) {
        cb.setRGBColorStrokeF(0f, 0f, 255f);
    } else {
        cb.setRGBColorStrokeF(255f, 0f, 0f);
    }

    for (Enumeration e = gProp.propertyNames(); e.hasMoreElements();) {
        temp = new StringBuilder(e.nextElement().toString());
        tempValue = gProp.getProperty(temp.toString(), "");

        if (tempValue.equals("")) {
            continue;
        }

        tempYcoords = tempValue.split(",");
        for (int idx = 0; idx < tempYcoords.length; ++idx) {
            tempValue = tempYcoords[idx];
            cb.circle((origX + Float.parseFloat(temp.toString())),
                    (height - origY + Float.parseFloat(tempValue)), 1.5f);
            cb.stroke();
        }
    }
}

From source file:questions.importpages.NameCard.java

public static void main(String[] args) {
    try {//from  ww w  .  j  av a  2 s . co  m
        createOneCard();
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        PdfReader reader = new PdfReader(CARD);
        document.open();
        PdfContentByte canvas = writer.getDirectContent();
        canvas.addTemplate(writer.getImportedPage(reader, 1), 36, 600);
        canvas.addTemplate(writer.getImportedPage(reader, 2), 200, 600);
        canvas.moveTo(0, 600);
        canvas.lineTo(595, 600);
        canvas.stroke();
        document.close();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:questions.importpages.NameCards.java

public static void createSheet(int p) throws DocumentException, IOException {
    Rectangle rect = new Rectangle(PageSize.A4);
    Document document = new Document(rect);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(SHEET[p - 1]));
    document.open();//from www .  j  a va 2s.  c o  m
    PdfContentByte canvas = writer.getDirectContentUnder();
    PdfReader reader = new PdfReader(NameCard.CARD);
    PdfImportedPage front = writer.getImportedPage(reader, p);
    float x = rect.getWidth() / 2 - front.getWidth();
    float y = (rect.getHeight() - (front.getHeight() * 5)) / 2;
    canvas.setLineWidth(0.5f);
    canvas.moveTo(x, y - 15);
    canvas.lineTo(x, y);
    canvas.lineTo(x - 15, y);
    canvas.moveTo(x + front.getWidth(), y - 15);
    canvas.lineTo(x + front.getWidth(), y);
    canvas.moveTo(x + front.getWidth() * 2, y - 15);
    canvas.lineTo(x + front.getWidth() * 2, y);
    canvas.lineTo(x + front.getWidth() * 2 + 15, y);
    canvas.stroke();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 2; j++) {
            canvas.addTemplate(front, x, y);
            x += front.getWidth();
        }
        x = rect.getWidth() / 2 - front.getWidth();
        y += front.getHeight();
        canvas.moveTo(x, y);
        canvas.lineTo(x - 15, y);
        canvas.moveTo(x + front.getWidth() * 2, y);
        canvas.lineTo(x + front.getWidth() * 2 + 15, y);
        canvas.stroke();
    }
    canvas.moveTo(x, y + 15);
    canvas.lineTo(x, y);
    canvas.moveTo(x + front.getWidth(), y + 15);
    canvas.lineTo(x + front.getWidth(), y);
    canvas.moveTo(x + front.getWidth() * 2, y + 15);
    canvas.lineTo(x + front.getWidth() * 2, y);
    canvas.stroke();
    document.close();
}

From source file:questions.ocg.AddOptionalContentToExistingPdf.java

public static void main(String[] args) throws IOException, DocumentException {
    // creating an empty document
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT01));
    writer.setPdfVersion(PdfWriter.VERSION_1_5);
    document.open();// ww  w . j av a  2  s .  c o  m
    writer.setPageEmpty(false);
    document.close();

    PdfReader reader = new PdfReader(RESULT01);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT02));
    PdfContentByte cb = stamper.getOverContent(1);
    writer = stamper.getWriter();
    PdfLayer nested = new PdfLayer("Nested Layers", writer);
    PdfLayer nested_1 = new PdfLayer("Nested Layer 1", writer);
    PdfLayer nested_2 = new PdfLayer("Nested Layer 2", writer);
    PdfLayer layer21 = new PdfLayer("Layer 2", writer);
    PdfLayer layer22 = new PdfLayer("Layer 2", writer);
    nested_2.addChild(layer21);
    nested_2.addChild(layer22);
    nested.addChild(nested_1);
    nested.addChild(nested_2);
    cb.beginLayer(nested);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layers"), 50, 775, 0);
    cb.endLayer();
    cb.beginLayer(nested_1);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 1"), 100, 800, 0);
    cb.endLayer();
    cb.beginLayer(nested_2);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 2"), 100, 750, 0);
    cb.endLayer();
    cb.beginLayer(layer21);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 2.1 in the group"), 150, 775, 0);
    cb.endLayer();
    cb.beginLayer(layer22);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 2.2 in the group"), 150, 725, 0);
    cb.endLayer();
    PdfLayer group = PdfLayer.createTitle("Grouped layers", writer);
    PdfLayer layer1 = new PdfLayer("Group: layer 1", writer);
    PdfLayer layer2 = new PdfLayer("Group: layer 2", writer);
    group.addChild(layer1);
    group.addChild(layer2);
    cb.beginLayer(layer1);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 1 in the group"), 50, 700, 0);
    cb.endLayer();
    cb.beginLayer(layer2);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 2 in the group"), 50, 675, 0);
    cb.endLayer();
    PdfLayer radiogroup = PdfLayer.createTitle("Radio Group", writer);
    PdfLayer radio1 = new PdfLayer("Radiogroup: layer 1", writer);
    radio1.setOn(true);
    PdfLayer radio2 = new PdfLayer("Radiogroup: layer 2", writer);
    radio2.setOn(false);
    PdfLayer radio3 = new PdfLayer("Radiogroup: layer 3", writer);
    radio3.setOn(false);
    radiogroup.addChild(radio1);
    radiogroup.addChild(radio2);
    radiogroup.addChild(radio3);
    ArrayList<PdfLayer> options = new ArrayList<PdfLayer>();
    options.add(radio1);
    options.add(radio2);
    options.add(radio3);
    writer.addOCGRadioGroup(options);
    cb.beginLayer(radio1);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 1"), 50, 600, 0);
    cb.endLayer();
    cb.beginLayer(radio2);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 2"), 50, 575, 0);
    cb.endLayer();
    cb.beginLayer(radio3);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 3"), 50, 550, 0);
    cb.endLayer();
    PdfLayer not_printed = new PdfLayer("not printed", writer);
    not_printed.setOnPanel(false);
    not_printed.setPrint("Print", false);
    cb.beginLayer(not_printed);
    ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase("PRINT THIS PAGE"), 300, 700, 90);
    cb.endLayer();
    PdfLayer zoom = new PdfLayer("Zoom 0.75-1.25", writer);
    zoom.setOnPanel(false);
    zoom.setZoom(0.75f, 1.25f);
    cb.beginLayer(zoom);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT,
            new Phrase("Only visible if the zoomfactor is between 75 and 125%"), 30, 530, 90);
    cb.endLayer();
    stamper.close();

    reader = new PdfReader(RESULT02);
    stamper = new PdfStamper(reader, new FileOutputStream(RESULT03));
    Map ocg = stamper.getPdfLayers();
    for (Iterator i = ocg.keySet().iterator(); i.hasNext();) {
        System.out.println(i.next());
    }
    PdfLayer layer = (PdfLayer) ocg.get("Layer 2");
    layer.setOn(false);
    cb = stamper.getOverContent(1);
    cb.beginLayer(layer);
    cb.moveTo(0, 0);
    cb.lineTo(500, 500);
    cb.stroke();
    cb.endLayer();
    PdfLayer parent = (PdfLayer) ocg.get("Layer 2(2)");
    PdfLayer newLayer = new PdfLayer("Child layer", stamper.getWriter());
    newLayer.setOn(false);
    parent.addChild(newLayer);
    cb.beginLayer(newLayer);
    cb.moveTo(0, 500);
    cb.lineTo(500, 0);
    cb.stroke();
    cb.endLayer();
    stamper.close();
}

From source file:questions.separators.DottedGlue.java

public static void main(String[] args) {
    Document document = new Document();
    try {//from  w  w  w. j a  v  a  2s. c  om
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();

        PdfContentByte canvas = writer.getDirectContent();
        canvas.moveTo(document.left(), document.top());
        canvas.lineTo(document.left(), 550);
        canvas.moveTo(document.right(), document.top());
        canvas.lineTo(document.right(), 550);
        canvas.stroke();

        Chunk separator = new Chunk(new DottedLineSeparator());

        // two parts
        Paragraph p1 = new Paragraph();
        p1.add(new Chunk("Chapter 1"));
        p1.add(separator);
        p1.add(new Chunk("p 10"));

        // 3 parts
        Paragraph p2 = new Paragraph();
        p2.add(new Chunk("Chapter 2"));
        p2.add(separator);
        p2.add(new Chunk("x"));
        p2.add(separator);
        p2.add(new Chunk("y"));

        // line ends with separator
        Paragraph p3 = new Paragraph();
        p3.add(new Chunk("Chapter 3"));
        p3.add(separator);

        // line starts with a separator
        Paragraph p4 = new Paragraph();
        p4.add(separator);
        p4.add(new Chunk("chapter 4"));

        // line IS a separator
        Paragraph p5 = new Paragraph();
        p5.add(separator);

        // line IS a separator + leading
        Paragraph p6 = new Paragraph(40);
        p6.add(separator);

        // separator galore
        Paragraph p7 = new Paragraph();
        p7.setAlignment(Element.ALIGN_JUSTIFIED);
        for (int i = 0; i < 10; i++) {
            p7.add(new Chunk("This is a test hippopotamus hippopotamus"));
            if (i % 4 == 0)
                p7.add(" hippopotamus hippopotamus hippopotamus hippopotamus");
            p7.add(separator);
        }

        document.add(p1);
        document.add(p1);
        document.add(p2);
        document.add(p3);
        document.add(p4);
        document.add(p5);
        document.add(p6);
        document.add(p7);

        ColumnText ct = new ColumnText(writer.getDirectContent());
        ct.addElement(p1);
        ct.addElement(p1);
        ct.addElement(p2);
        ct.addElement(p3);
        ct.addElement(p4);
        ct.addElement(p5);
        ct.addElement(p6);
        ct.addElement(p7);
        ct.setSimpleColumn(36, 36, 436, 536);
        ct.go();
        canvas.rectangle(36, 36, 400, 500);
        canvas.stroke();
        document.close();
    } catch (Exception de) {
        de.printStackTrace();
    }
}

From source file:questions.separators.TabbedWords1.java

public static void main(String[] args) {
    Document document = new Document();
    try {//from ww  w . j ava 2 s  . com
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();

        Paragraph p;
        Chunk tab1 = new Chunk(new VerticalPositionMark(), 100);
        Chunk tab2 = new Chunk(new LineSeparator(), 120);
        Chunk tab3 = new Chunk(new DottedLineSeparator(), 200);
        Chunk tab4 = new Chunk(new VerticalPositionMark(), 250);
        Chunk tab5 = new Chunk(new VerticalPositionMark(), 300);

        PdfContentByte canvas = writer.getDirectContent();
        ColumnText column = new ColumnText(canvas);
        for (int i = 0; i < 40; i++) {
            p = new Paragraph("TEST");
            p.add(tab1);
            p.add(new Chunk(String.valueOf(i)));
            p.add(tab2);
            p.add(new Chunk(String.valueOf(i * 2)));
            p.add(tab3);
            p.add(new Chunk(SeparatedWords2.WORDS[39 - i]));
            p.add(tab4);
            p.add(new Chunk(String.valueOf(i * 4)));
            p.add(tab5);
            p.add(new Chunk(SeparatedWords2.WORDS[i]));
            column.addElement(p);
        }
        column.setSimpleColumn(60, 36, 400, 806);
        canvas.moveTo(60, 36);
        canvas.lineTo(60, 806);
        canvas.moveTo(160, 36);
        canvas.lineTo(160, 806);
        canvas.moveTo(180, 36);
        canvas.lineTo(180, 806);
        canvas.moveTo(260, 36);
        canvas.lineTo(260, 806);
        canvas.moveTo(310, 36);
        canvas.lineTo(310, 806);
        canvas.moveTo(360, 36);
        canvas.lineTo(360, 806);
        canvas.moveTo(400, 36);
        canvas.lineTo(400, 806);
        canvas.stroke();
        column.go();

        document.setMargins(60, 195, 36, 36);
        document.newPage();

        for (int i = 0; i < 40; i++) {
            p = new Paragraph("TEST");
            p.add(tab1);
            p.add(new Chunk(String.valueOf(i)));
            p.add(tab2);
            p.add(new Chunk(String.valueOf(i * 2)));
            p.add(tab3);
            p.add(new Chunk(SeparatedWords2.WORDS[39 - i]));
            p.add(tab4);
            p.add(new Chunk(String.valueOf(i * 4)));
            p.add(tab5);
            p.add(new Chunk(SeparatedWords2.WORDS[i]));
            document.add(p);
        }
        document.close();
    } catch (Exception de) {
        de.printStackTrace();
    }
}

From source file:questions.separators.TabbedWords2.java

public static void main(String[] args) {
    Document document = new Document();
    try {/*w w  w.  j a v  a 2  s . c o m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();

        Paragraph p;
        Chunk tab1 = new Chunk(new VerticalPositionMark(), 100, true);
        Chunk tab2 = new Chunk(new LineSeparator(), 120, true);
        Chunk tab3 = new Chunk(new DottedLineSeparator(), 200, true);
        Chunk tab4 = new Chunk(new VerticalPositionMark(), 240, true);
        Chunk tab5 = new Chunk(new VerticalPositionMark(), 300, true);

        PdfContentByte canvas = writer.getDirectContent();
        ColumnText column = new ColumnText(canvas);
        for (int i = 0; i < 40; i++) {
            p = new Paragraph("TEST");
            p.add(tab1);
            p.add(new Chunk(String.valueOf(i)));
            p.add(tab2);
            p.add(new Chunk(String.valueOf(i * 2)));
            p.add(tab3);
            p.add(new Chunk(SeparatedWords2.WORDS[39 - i]));
            p.add(tab4);
            p.add(new Chunk(String.valueOf(i * 4)));
            p.add(tab5);
            p.add(new Chunk(SeparatedWords2.WORDS[i]));
            column.addElement(p);
        }
        column.setSimpleColumn(60, 36, 400, 806);
        canvas.moveTo(60, 36);
        canvas.lineTo(60, 806);
        canvas.moveTo(160, 36);
        canvas.lineTo(160, 806);
        canvas.moveTo(180, 36);
        canvas.lineTo(180, 806);
        canvas.moveTo(260, 36);
        canvas.lineTo(260, 806);
        canvas.moveTo(300, 36);
        canvas.lineTo(300, 806);
        canvas.moveTo(360, 36);
        canvas.lineTo(360, 806);
        canvas.moveTo(400, 36);
        canvas.lineTo(400, 806);
        canvas.stroke();
        column.go();

        document.setMargins(60, 195, 36, 36);
        document.newPage();

        canvas.moveTo(document.left(), document.bottom());
        canvas.lineTo(document.left(), document.top());
        canvas.moveTo(document.right(), document.bottom());
        canvas.lineTo(document.right(), document.top());
        canvas.stroke();
        for (int i = 0; i < 40; i++) {
            p = new Paragraph("TEST");
            p.add(tab1);
            p.add(new Chunk(String.valueOf(i)));
            p.add(tab2);
            p.add(new Chunk(String.valueOf(i * 2)));
            p.add(tab3);
            p.add(new Chunk(SeparatedWords2.WORDS[39 - i]));
            p.add(tab4);
            p.add(new Chunk(String.valueOf(i * 4)));
            p.add(tab5);
            p.add(new Chunk(SeparatedWords2.WORDS[i]));
            document.add(p);
        }
        document.close();
    } catch (Exception de) {
        de.printStackTrace();
    }
}