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

The following examples show how to use com.lowagie.text.pdf.PdfContentByte#setRGBColorStrokeF() . 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: ShapesTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Draws some shapes.
    */
@Test
public void main() throws Exception {
       
       
       // step 1: creation of a document-object
       Document document = new Document();
       
       try {
           
           // 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( "shapes.pdf"));
           
           // step 3: we open the document
           document.open();
           
           // step 4: we grab the ContentByte and do some stuff with it
           PdfContentByte cb = writer.getDirectContent();
           
           // an example of a rectangle with a diagonal in very thick lines
           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();
           
           // an example of some circles
           cb.setLineDash(3, 3, 0);
           cb.setRGBColorStrokeF(0f, 255f, 0f);
           cb.circle(150f, 500f, 100f);
           cb.stroke();
           
           cb.setLineWidth(5f);
           cb.resetRGBColorStroke();
           cb.circle(150f, 500f, 50f);
           cb.stroke();
           
           // example with colorfill
           cb.setRGBColorFillF(0f, 255f, 0f);
           cb.moveTo(100f, 200f);
           cb.lineTo(200f, 250f);
           cb.lineTo(400f, 150f);
           // because we change the fill color BEFORE we stroke the triangle
           // the color of the triangle will be red instead of green
           cb.setRGBColorFillF(255f, 0f, 0f);
           cb.closePathFillStroke();
           
           cb.sanityCheck();
       }
       catch(DocumentException de) {
           System.err.println(de.getMessage());
       }
       catch(IOException ioe) {
           System.err.println(ioe.getMessage());
       }
       
       // step 5: we close the document
       document.close();
   }
 
Example 2
Source File: TableEvents2Test.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable, float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
 */
public void tableLayout(PdfPTable table, float[][] width, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
	
	// widths of the different cells of the first row
    float widths[] = width[0];
    
    PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
    cb.saveState();
	// border for the complete table
    cb.setLineWidth(2);
    cb.setRGBColorStroke(255, 0, 0);
    cb.rectangle(widths[0], heights[heights.length - 1], widths[widths.length - 1] - widths[0], heights[0] - heights[heights.length - 1]);
    cb.stroke();
    
    // border for the header rows
    if (headerRows > 0) {
        cb.setRGBColorStroke(0, 0, 255);
        cb.rectangle(widths[0], heights[headerRows], widths[widths.length - 1] - widths[0], heights[0] - heights[headerRows]);
        cb.stroke();
    }
    cb.restoreState();
    
    cb = canvases[PdfPTable.BASECANVAS];
    cb.saveState();
    // border for the cells
    cb.setLineWidth(.5f);
    // loop over the rows
    for (int line = 0; line < heights.length - 1; ++line) {
        widths = width[line];
    	// loop over the columns
        for (int col = 0; col < widths.length - 1; ++col) {
            if (line == 0 && col == 0)
                cb.setAction(new PdfAction("http://www.lowagie.com/iText/"),
                    widths[col], heights[line + 1], widths[col + 1], heights[line]);
            cb.setRGBColorStrokeF((float)Math.random(), (float)Math.random(), (float)Math.random());
            // horizontal borderline
            cb.moveTo(widths[col], heights[line]);
            cb.lineTo(widths[col + 1], heights[line]);
            cb.stroke();
            // vertical borderline
            cb.setRGBColorStrokeF((float)Math.random(), (float)Math.random(), (float)Math.random());
            cb.moveTo(widths[col], heights[line]);
            cb.lineTo(widths[col], heights[line + 1]);
            cb.stroke();
        }
    }
    cb.restoreState();
}
 
Example 3
Source File: TableEvents1Test.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable,
 *      float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
 */
public void tableLayout(PdfPTable table, float[][] width, float[] heights, int headerRows, int rowStart,
		PdfContentByte[] canvases) {

	// widths of the different cells of the first row
	float widths[] = width[0];

	PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
	cb.saveState();
	// border for the complete table
	cb.setLineWidth(2);
	cb.setRGBColorStroke(255, 0, 0);
	cb.rectangle(widths[0], heights[heights.length - 1], widths[widths.length - 1] - widths[0], heights[0]
			- heights[heights.length - 1]);
	cb.stroke();

	// border for the header rows
	if (headerRows > 0) {
		cb.setRGBColorStroke(0, 0, 255);
		cb.rectangle(widths[0], heights[headerRows], widths[widths.length - 1] - widths[0], heights[0]
				- heights[headerRows]);
		cb.stroke();
	}
	cb.restoreState();

	cb = canvases[PdfPTable.BASECANVAS];
	cb.saveState();
	// border for the cells
	cb.setLineWidth(.5f);
	// loop over the rows
	for (int line = 0; line < heights.length - 1; ++line) {
		// loop over the columns
		for (int col = 0; col < widths.length - 1; ++col) {
			if (line == 0 && col == 0)
				cb.setAction(new PdfAction("http://www.lowagie.com/iText/"), widths[col], heights[line + 1],
						widths[col + 1], heights[line]);
			cb.setRGBColorStrokeF((float) Math.random(), (float) Math.random(), (float) Math.random());
			// horizontal borderline
			cb.moveTo(widths[col], heights[line]);
			cb.lineTo(widths[col + 1], heights[line]);
			cb.stroke();
			// vertical borderline
			cb.setRGBColorStrokeF((float) Math.random(), (float) Math.random(), (float) Math.random());
			cb.moveTo(widths[col], heights[line]);
			cb.lineTo(widths[col], heights[line + 1]);
			cb.stroke();
		}
	}
	cb.restoreState();
}