Java Code Examples for com.lowagie.text.pdf.PdfContentByte#setColorStroke()

The following examples show how to use com.lowagie.text.pdf.PdfContentByte#setColorStroke() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LineSeparator.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Draws a horizontal line.
 * @param canvas	the canvas to draw on
 * @param leftX		the left x coordinate
 * @param rightX	the right x coordindate
 * @param y			the y coordinate
 */
public void drawLine(PdfContentByte canvas, float leftX, float rightX, float y) {
	float w;
    if (getPercentage() < 0)
        w = -getPercentage();
    else
        w = (rightX - leftX) * getPercentage() / 100.0f;
    float s;
    switch (getAlignment()) {
        case Element.ALIGN_LEFT:
            s = 0;
            break;
        case Element.ALIGN_RIGHT:
            s = rightX - leftX - w;
            break;
        default:
            s = (rightX - leftX - w) / 2;
            break;
    }
    canvas.setLineWidth(getLineWidth());
    if (getLineColor() != null)
        canvas.setColorStroke(getLineColor());
    canvas.moveTo(s + leftX, y + offset);
    canvas.lineTo(s + w + leftX, y + offset);
    canvas.stroke();
}
 
Example 2
Source File: LineSeparator.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Draws a horizontal line.
 * @param canvas	the canvas to draw on
 * @param leftX		the left x coordinate
 * @param rightX	the right x coordindate
 * @param y			the y coordinate
 */
public void drawLine(PdfContentByte canvas, float leftX, float rightX, float y) {
	float w;
    if (getPercentage() < 0)
        w = -getPercentage();
    else
        w = (rightX - leftX) * getPercentage() / 100.0f;
    float s;
    switch (getAlignment()) {
        case Element.ALIGN_LEFT:
            s = 0;
            break;
        case Element.ALIGN_RIGHT:
            s = rightX - leftX - w;
            break;
        default:
            s = (rightX - leftX - w) / 2;
            break;
    }
    canvas.setLineWidth(getLineWidth());
    if (getLineColor() != null)
        canvas.setColorStroke(getLineColor());
    canvas.moveTo(s + leftX, y + offset);
    canvas.lineTo(s + w + leftX, y + offset);
    canvas.stroke();
}
 
Example 3
Source File: GroupsTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Prints a square and fills half of it with a gray rectangle.
 * @param x
 * @param y
 * @param cb
 * @throws Exception
 */
public static void pictureBackdrop(float x, float y, PdfContentByte cb) throws Exception {
    cb.setColorStroke(Color.black);
    cb.setColorFill(Color.red);
    cb.rectangle(x, y, 100, 200);
    cb.fill();
    cb.setLineWidth(2);
    cb.rectangle(x, y, 200, 200);
    cb.stroke();
}
 
Example 4
Source File: TransparencyTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Prints a square and fills half of it with a gray rectangle.
 * 
 * @param x
 * @param y
 * @param cb
 * @throws Exception
 */
private static void pictureBackdrop(float x, float y, PdfContentByte cb)
		throws Exception {
	cb.setColorStroke(Color.black);
	cb.setColorFill(Color.gray);
	cb.rectangle(x, y, 100, 200);
	cb.fill();
	cb.setLineWidth(2);
	cb.rectangle(x, y, 200, 200);
	cb.stroke();
}
 
Example 5
Source File: PageNumbersWatermarkTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    // write the headertable
    table.setTotalWidth(document.right() - document.left());
    table.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 50, cb);
    // compose the footer
    String text = "Page " + writer.getPageNumber() + " of ";
    float textSize = helv.getWidthPoint(text, 12);
    float textBase = document.bottom() - 20;
    cb.beginText();
    cb.setFontAndSize(helv, 12);
    // for odd pagenumbers, show the footer at the left
    if ((writer.getPageNumber() & 1) == 1) {
        cb.setTextMatrix(document.left(), textBase);
        cb.showText(text);
        cb.endText();
        cb.addTemplate(tpl, document.left() + textSize, textBase);
    }
    // for even numbers, show the footer at the right
    else {
        float adjust = helv.getWidthPoint("0", 12);
        cb.setTextMatrix(document.right() - textSize - adjust, textBase);
        cb.showText(text);
        cb.endText();
        cb.addTemplate(tpl, document.right() - adjust, textBase);
    }

    // draw a Rectangle around the page
    cb.setColorStroke(Color.orange);
    cb.setLineWidth(2);
    cb.rectangle(20, 20, document.getPageSize().getWidth() - 40, document.getPageSize().getHeight() - 40);
    cb.stroke();

    // starting on page 3, a watermark with an Image that is made transparent
    if (writer.getPageNumber() >= 3) {

        cb.setGState(gstate);
        cb.setColorFill(Color.red);
        cb.beginText();
        cb.setFontAndSize(helv, 48);
        cb.showTextAligned(Element.ALIGN_CENTER, "Watermark Opacity " + writer.getPageNumber(), document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2, 45);
        cb.endText();
        try {
            cb.addImage(headerImage, headerImage.getWidth(), 0, 0, headerImage.getHeight(), 440, 80);
        }
        catch(Exception e) {
            throw new ExceptionConverter(e);
        }
    }
    cb.restoreState();
    cb.sanityCheck();
}
 
Example 6
Source File: PatternTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Painting Patterns.
 * 
 * @param args
 *            no arguments needed
 */
@Test
public void main() throws Exception {


	// step 1: creation of a document-object
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);

	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file
	PdfWriter writer = PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("pattern.pdf"));

	// step 3: we open the document
	document.open();

	// step 4: we add some content
	PdfContentByte cb = writer.getDirectContent();
	PdfTemplate tp = cb.createTemplate(400, 300);
	PdfPatternPainter pat = cb.createPattern(15, 15, null);
	pat.rectangle(5, 5, 5, 5);
	pat.fill();
	pat.sanityCheck();

	PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV",
			new CMYKColor(0.9f, .2f, .3f, .1f));
	SpotColor spot = new SpotColor(spc_cmyk, 0.25f);
	tp.setPatternFill(pat, spot, .9f);
	tp.rectangle(0, 0, 400, 300);
	tp.fill();
	tp.sanityCheck();

	cb.addTemplate(tp, 50, 50);
	PdfPatternPainter pat2 = cb.createPattern(10, 10, null);
	pat2.setLineWidth(2);
	pat2.moveTo(-5, 0);
	pat2.lineTo(10, 15);
	pat2.stroke();
	pat2.moveTo(0, -5);
	pat2.lineTo(15, 10);
	pat2.stroke();
	cb.setLineWidth(1);
	cb.setColorStroke(Color.black);
	cb.setPatternFill(pat2, Color.red);
	cb.rectangle(100, 400, 30, 210);
	cb.fillStroke();
	cb.setPatternFill(pat2, Color.green);
	cb.rectangle(150, 400, 30, 100);
	cb.fillStroke();
	cb.setPatternFill(pat2, Color.blue);
	cb.rectangle(200, 400, 30, 130);
	cb.fillStroke();
	cb.setPatternFill(pat2, new GrayColor(0.5f));
	cb.rectangle(250, 400, 30, 80);
	cb.fillStroke();
	cb.setPatternFill(pat2, new GrayColor(0.7f));
	cb.rectangle(300, 400, 30, 170);
	cb.fillStroke();
	cb.setPatternFill(pat2, new GrayColor(0.9f));
	cb.rectangle(350, 400, 30, 40);
	cb.fillStroke();

	cb.sanityCheck();

	// step 5: we close the document
	document.close();
}
 
Example 7
Source File: SpotColorsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Demonstrates the use of spotcolors.
    */
@Test
   public void main() throws Exception {
       
       
       // step 1: creation of a document-object
       Document document = new Document();
           
           // step 2:
           // we create a writer that listens to the document
           // and directs a PDF-stream to a file
           PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("spotcolor.pdf"));
           BaseFont bf = BaseFont.createFont("Helvetica", "winansi", BaseFont.NOT_EMBEDDED);
           
           // step 3: we open the document
           document.open();
           
           // step 4: we grab the ContentByte and do some stuff with it
           PdfContentByte cb = writer.getDirectContent();
           
           // step 5: we instantiate PdfSpotColor
           
           // Note: I made up these names unless someone give me a PANTONE swatch as gift ([email protected])
           PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV", new CMYKColor(0.9f, .2f, .3f, .1f));
           PdfSpotColor spc_rgb = new PdfSpotColor("PANTONE 147", new Color(114, 94, 38));
           PdfSpotColor spc_g = new PdfSpotColor("PANTONE 100 CV", new GrayColor(0.9f));
           
           // Stroke a rectangle with CMYK alternate
           cb.setColorStroke(spc_cmyk, .5f);
           cb.setLineWidth(10f);
           // draw a rectangle
           cb.rectangle(100, 700, 100, 100);
           // add the diagonal
           cb.moveTo(100, 700);
           cb.lineTo(200, 800);
           // stroke the lines
           cb.stroke();
           
           // Fill a rectangle with CMYK alternate
           cb.setColorFill(spc_cmyk, 0.25f);
           cb.rectangle(250, 700, 100, 100);
           cb.fill();
           
           // Stroke a circle with RGB alternate
           cb.setColorStroke(spc_rgb, 0.9f);
           cb.setLineWidth(5f);
           cb.circle(150f, 500f, 100f);
           cb.stroke();
           
           // Fill the circle with RGB alternate
           cb.setColorFill(spc_rgb, 0.9f);
           cb.circle(150f, 500f, 50f);
           cb.fill();
           
           // example with colorfill
           cb.setColorFill(spc_g, 0.5f);
           cb.moveTo(100f, 200f);
           cb.lineTo(200f, 250f);
           cb.lineTo(400f, 150f);
           cb.fill();
           // cb.sanityCheck is called during newPage().
           document.newPage();
           String text = "Some text to show";
           document.add(new Paragraph(text, new Font(Font.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk, 0.25f))));
           document.add(new Paragraph(text, new Font(Font.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk, 0.5f))));
           
           // example with template
           PdfTemplate t = cb.createTemplate(500f, 500f);
           // Stroke a rectangle with CMYK alternate
           t.setColorStroke(new SpotColor(spc_cmyk, .5f));
           t.setLineWidth(10f);
           // draw a rectangle
           t.rectangle(100, 10, 100, 100);
           // add the diagonal
           t.moveTo(100, 10);
           t.lineTo(200, 100);
           // stroke the lines
           t.stroke();
           
           // Fill a rectangle with CMYK alternate
           t.setColorFill(spc_g, 0.5f);
           t.rectangle(100, 125, 100, 100);
           t.fill();
           t.beginText();
           t.setFontAndSize(bf, 20f);
           t.setTextMatrix(1f, 0f, 0f, 1f, 10f, 10f);
           t.showText("Template text upside down");
           t.endText();
           t.rectangle(0, 0, 499, 499);
           t.stroke();
           t.sanityCheck();
           cb.addTemplate(t, -1.0f, 0.00f, 0.00f, -1.0f, 550f, 550f);
           
           cb.sanityCheck();

       
       // step 5: we close the document
       document.close();
   }
 
Example 8
Source File: PDFPage.java    From birt with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Draws a line with the line-style specified in advance from the start
 * position to the end position with the given linewidth, color, and style
 * at the given pdf layer. If the line-style is NOT set before invoking this
 * method, "solid" will be used as the default line-style.
 *
 * @param startX
 *            the start X coordinate of the line
 * @param startY
 *            the start Y coordinate of the line
 * @param endX
 *            the end X coordinate of the line
 * @param endY
 *            the end Y coordinate of the line
 * @param width
 *            the lineWidth
 * @param color
 *            the color of the line
 * @param contentByte
 *            the given pdf layer
 */
private void drawRawLine( float startX, float startY, float endX,
		float endY, float width, Color color, PdfContentByte contentByte )
{
	startY = transformY( startY );
	endY = transformY( endY );
	contentByte.concatCTM( 1, 0, 0, 1, startX, startY );

	contentByte.moveTo( 0, 0 );
	contentByte.lineTo( endX - startX, endY - startY );

	contentByte.setLineWidth( width );
	contentByte.setColorStroke( color );
	contentByte.stroke( );
}