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

The following examples show how to use com.lowagie.text.pdf.PdfContentByte#restoreState() . 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: MetaState.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
public void restoreState(int index, PdfContentByte cb) {
	int pops;
	if (index < 0) {
		pops = Math.min(-index, savedStates.size());
	} else {
		pops = Math.max(savedStates.size() - index, 0);
	}
	if (pops == 0) {
		return;
	}
	MetaState state = null;
	while (pops-- != 0) {
		cb.restoreState();
		state = (MetaState) savedStates.pop();
	}
	setMetaState(state);
}
 
Example 2
Source File: DottedLineSeparator.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.draw.DrawInterface#draw(com.lowagie.text.pdf.PdfContentByte, float, float, float, float, float)
 */
public void draw(PdfContentByte canvas, float llx, float lly, float urx, float ury, float y) {
	canvas.saveState();
	canvas.setLineWidth(lineWidth);
	canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND);
	canvas.setLineDash(0, gap, gap / 2);
       drawLine(canvas, llx, urx, y);
	canvas.restoreState();
}
 
Example 3
Source File: Coversheet.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void upperRightAlignmentMark(final PdfContentByte cb) {
    cb.saveState();
    cb.rectangle(LETTER.width() - (ALIGNMENT_MARGIN * 4) - ALIGNMENT_MARK_WIDTH,
                 (LETTER.height() + TOP_MARGIN) - ALIGNMENT_MARGIN - ALIGNMENT_MARK_HEIGHT,
                 ALIGNMENT_MARK_WIDTH,
                 ALIGNMENT_MARK_HEIGHT);
    cb.setColorFill(BLACK);
    cb.fill();
    cb.restoreState();
}
 
Example 4
Source File: Coversheet.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void lowerRightAlignmentMark(final PdfContentByte cb) {
    cb.saveState();
    cb.rectangle(LETTER.width() - (ALIGNMENT_MARGIN * 4) - ALIGNMENT_MARK_WIDTH,
                 ALIGNMENT_MARGIN,
                 ALIGNMENT_MARK_WIDTH,
                 ALIGNMENT_MARK_HEIGHT);
    cb.setColorFill(BLACK);
    cb.fill();
    cb.restoreState();
}
 
Example 5
Source File: Coversheet.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void lowerLeftAlignmentMark(final PdfContentByte cb) {
    cb.saveState();
    cb.rectangle(ALIGNMENT_MARGIN,
                 ALIGNMENT_MARGIN,
                 ALIGNMENT_MARK_WIDTH,
                 ALIGNMENT_MARK_HEIGHT);
    cb.setColorFill(BLACK);
    cb.fill();
    cb.restoreState();
}
 
Example 6
Source File: Coversheet.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void upperLeftAlignmentMark(final PdfContentByte cb) {
    cb.saveState();
    cb.rectangle(ALIGNMENT_MARGIN,
                 (LETTER.height() + TOP_MARGIN) - ALIGNMENT_MARK_HEIGHT - ALIGNMENT_MARGIN,
                 ALIGNMENT_MARK_WIDTH,
                 ALIGNMENT_MARK_HEIGHT);
    cb.setColorFill(BLACK);
    cb.fill();
    cb.restoreState();
}
 
Example 7
Source File: ShadingTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void main() throws Exception {

	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	PdfWriter writer = PdfWriter.getInstance(document,	PdfTestBase.getOutputStream( "shading.pdf"));
	document.open();

	PdfFunction function1 = PdfFunction.type2(writer, new float[] { 0, 1 },
			null, new float[] { .929f, .357f, 1, .298f }, new float[] {
					.631f, .278f, 1, .027f }, 1.048f);
	PdfFunction function2 = PdfFunction.type2(writer, new float[] { 0, 1 },
			null, new float[] { .929f, .357f, 1, .298f }, new float[] {
					.941f, .4f, 1, .102f }, 1.374f);
	PdfFunction function3 = PdfFunction.type3(writer, new float[] { 0, 1 },
			null, new PdfFunction[] { function1, function2 },
			new float[] { .708f }, new float[] { 1, 0, 0, 1 });
	PdfShading shading = PdfShading.type3(writer,
			new CMYKColor(0, 0, 0, 0),
			new float[] { 0, 0, .096f, 0, 0, 1 }, null, function3,
			new boolean[] { true, true });
	PdfContentByte cb = writer.getDirectContent();
	cb.moveTo(316.789f, 140.311f);
	cb.curveTo(303.222f, 146.388f, 282.966f, 136.518f, 279.122f, 121.983f);
	cb.lineTo(277.322f, 120.182f);
	cb.curveTo(285.125f, 122.688f, 291.441f, 121.716f, 298.156f, 119.386f);
	cb.lineTo(336.448f, 119.386f);
	cb.curveTo(331.072f, 128.643f, 323.346f, 137.376f, 316.789f, 140.311f);
	cb.clip();
	cb.newPath();
	cb.saveState();
	cb.concatCTM(27.7843f, 0, 0, -27.7843f, 310.2461f, 121.1521f);
	cb.paintShading(shading);
	cb.restoreState();

	cb.sanityCheck();

	document.close();
}
 
Example 8
Source File: PageNumbersWatermarkTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onStartPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
public void onStartPage(PdfWriter writer, Document document) {
    if (writer.getPageNumber() < 3) {
        PdfContentByte cb = writer.getDirectContentUnder();
        cb.saveState();
        cb.setColorFill(Color.pink);
        cb.beginText();
        cb.setFontAndSize(helv, 48);
        cb.showTextAligned(Element.ALIGN_CENTER, "My Watermark Under " + writer.getPageNumber(), document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2, 45);
        cb.endText();
        cb.restoreState();
    }
}
 
Example 9
Source File: MetaState.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void restoreState(int index, PdfContentByte cb) {
    int pops;
    if (index < 0)
        pops = Math.min(-index, savedStates.size());
    else
        pops = Math.max(savedStates.size() - index, 0);
    if (pops == 0)
        return;
    MetaState state = null;
    while (pops-- != 0) {
        cb.restoreState();
        state = (MetaState)savedStates.pop();
    }
    setMetaState(state);
}
 
Example 10
Source File: DottedLineSeparator.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.draw.DrawInterface#draw(com.lowagie.text.pdf.PdfContentByte, float, float, float, float, float)
 */
public void draw(PdfContentByte canvas, float llx, float lly, float urx, float ury, float y) {
	canvas.saveState();
	canvas.setLineWidth(lineWidth);
	canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND);
	canvas.setLineDash(0, gap, gap / 2);
       drawLine(canvas, llx, urx, y);
	canvas.restoreState();
}
 
Example 11
Source File: StateTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Changing the Graphics State with saveState() and restoreState().
 * 
 */
@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( "state.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();

		cb.circle(260.0f, 500.0f, 250.0f);
		cb.fill();
		cb.saveState();
		cb.setColorFill(Color.red);
		cb.circle(260.0f, 500.0f, 200.0f);
		cb.fill();
		cb.saveState();
		cb.setColorFill(Color.blue);
		cb.circle(260.0f, 500.0f, 150.0f);
		cb.fill();
		cb.restoreState();
		cb.circle(260.0f, 500.0f, 100.0f);
		cb.fill();
		cb.restoreState();
		cb.circle(260.0f, 500.0f, 50.0f);
		cb.fill();
		
		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 12
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 13
Source File: MetaState.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void cleanup(PdfContentByte cb) {
    int k = savedStates.size();
    while (k-- > 0)
        cb.restoreState();
}
 
Example 14
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 15
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();
}
 
Example 16
Source File: JTable2Pdf.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Print the table into a PDF file
 */
private void print() {
    Document document = new Document(PageSize.A4.rotate());
    try {
        PdfWriter writer =
        PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "jTable.pdf"));
        
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        
        // Create the graphics as shapes
        cb.saveState();
        Graphics2D g2 = cb.createGraphicsShapes(500, 500);
        // Print the table to the graphics
        Shape oldClip = g2.getClip();
        g2.clipRect(0, 0, 500, 500);
        table.print(g2);
        g2.setClip(oldClip);
        
        g2.dispose();
        cb.restoreState();
        
        document.newPage();
        
        // Create the graphics with pdf fonts
        cb.saveState();
        g2 = cb.createGraphics(500, 500);
        
        // Print the table to the graphics
        oldClip = g2.getClip();
        g2.clipRect(0, 0, 500, 500);
        table.print(g2);
        g2.setClip(oldClip);
        
        g2.dispose();
        cb.restoreState();
        
    } catch (Exception e) {
    	e.printStackTrace();
        System.err.println(e.getMessage());
    }
    
    document.close();
}
 
Example 17
Source File: LineSeparator.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.draw.DrawInterface#draw(com.lowagie.text.pdf.PdfContentByte, float, float, float, float, float)
 */
public void draw(PdfContentByte canvas, float llx, float lly, float urx, float ury, float y) {
    canvas.saveState();
    drawLine(canvas, llx, urx, y);
    canvas.restoreState();
}
 
Example 18
Source File: MetaState.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
public void cleanup(PdfContentByte cb) {
	int k = savedStates.size();
	while (k-- > 0) {
		cb.restoreState();
	}
}
 
Example 19
Source File: LineSeparator.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * @see com.lowagie.text.pdf.draw.DrawInterface#draw(com.lowagie.text.pdf.PdfContentByte, float, float, float, float, float)
 */
public void draw(PdfContentByte canvas, float llx, float lly, float urx, float ury, float y) {
    canvas.saveState();
    drawLine(canvas, llx, urx, y);
    canvas.restoreState();
}