Java Code Examples for com.lowagie.text.pdf.PdfContentByte#saveState()
The following examples show how to use
com.lowagie.text.pdf.PdfContentByte#saveState() .
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: PurchaseOrderQuoteRequestsPdf.java From kfs with GNU Affero General Public License v3.0 | 6 votes |
/** * Overrides the method in PdfPageEventHelper from itext to compose the footer and show the * footer. * * @param writer The PdfWriter for this document. * @param document The document. * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) */ public void onEndPage(PdfWriter writer, Document document) { LOG.debug("onEndPage() started."); PdfContentByte cb = writer.getDirectContent(); cb.saveState(); // 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); // show the footer 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); cb.saveState(); }
Example 2
Source File: PurapPdf.java From kfs with GNU Affero General Public License v3.0 | 6 votes |
/** * Overrides the method in PdfPageEventHelper from itext to write the headerTable, compose the footer and show the * footer. * * @param writer The PdfWriter for this document. * @param document The document. * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) */ @Override public void onEndPage(PdfWriter writer, Document document) { LOG.debug("onEndPage() started."); PdfContentByte cb = writer.getDirectContent(); cb.saveState(); // write the headerTable headerTable.setTotalWidth(document.right() - document.left()); headerTable.writeSelectedRows(0, -1, document.left(), document.getPageSize().height() - 10, 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); // show the footer 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); cb.saveState(); }
Example 3
Source File: PdfGraphics2D.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 6 votes |
/** * Constructor for PDFGraphics2D. */ public PdfGraphics2D( final PdfContentByte cb, final float width, final float height, final PdfOutputProcessorMetaData metaData ) { this.metaData = metaData; dg2.setRenderingHint( RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON ); setRenderingHint( RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON ); this.transform = new AffineTransform(); paint = Color.black; background = Color.white; setFont( new Font( "sanserif", Font.PLAIN, 12 ) ); this.cb = cb; cb.saveState(); this.width = width; this.height = height; clip = new Area( new Rectangle2D.Float( 0, 0, width, height ) ); clip( clip ); oldStroke = strokeOne; stroke = strokeOne; originalStroke = strokeOne; setStrokeDiff( stroke, null ); cb.saveState(); }
Example 4
Source File: DottedLineSeparator.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * @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 5
Source File: Coversheet.java From kfs with GNU Affero General Public License v3.0 | 5 votes |
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 6
Source File: Coversheet.java From kfs with GNU Affero General Public License v3.0 | 5 votes |
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 7
Source File: Coversheet.java From kfs with GNU Affero General Public License v3.0 | 5 votes |
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 8
Source File: Coversheet.java From kfs with GNU Affero General Public License v3.0 | 5 votes |
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 9
Source File: PdfWatermarkUtils.java From bamboobsc with Apache License 2.0 | 5 votes |
public static void addWatermark(PdfStamper pdfStamper, BaseFont baseFont, Color fontColor, String waterMarkString) throws Exception { if (null == pdfStamper || null == baseFont) { throw new java.lang.Exception("PdfStamper or BaseFont is null."); } if (StringUtils.isBlank(waterMarkString)) { return; } PdfContentByte pdfContentByte = null; Rectangle pageRect = null; PdfGState pdfGState = new PdfGState(); // 设置透明度为0.4 pdfGState.setFillOpacity(0.4f); pdfGState.setStrokeOpacity(0.4f); int pageNum = pdfStamper.getReader().getNumberOfPages(); try { for (int i = 1; i <= pageNum; i++) { pageRect = pdfStamper.getReader().getPageSizeWithRotation(i); // 计算水印X,Y坐标 float x = pageRect.getWidth() / 2; float y = pageRect.getHeight() / 2; //获得PDF最顶层 pdfContentByte = pdfStamper.getOverContent(i); pdfContentByte.saveState(); // set Transparency pdfContentByte.setGState(pdfGState); pdfContentByte.beginText(); pdfContentByte.setColorFill(fontColor); pdfContentByte.setFontAndSize(baseFont, 60); // 水印文字成45度角倾斜 pdfContentByte.showTextAligned(Element.ALIGN_CENTER, waterMarkString, x, y, 45); pdfContentByte.endText(); } } catch (Exception e) { e.printStackTrace(); } finally { pdfContentByte = null; pageRect = null; } }
Example 10
Source File: ShadingTest.java From itext2 with GNU Lesser General Public License v3.0 | 5 votes |
@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 11
Source File: PageNumbersWatermarkTest.java From itext2 with GNU Lesser General Public License v3.0 | 5 votes |
/** * @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 12
Source File: DottedLineSeparator.java From itext2 with GNU Lesser General Public License v3.0 | 5 votes |
/** * @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 13
Source File: TableEvents2Test.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * @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 14
Source File: TableEvents1Test.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * @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 15
Source File: JTable2Pdf.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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 16
Source File: PageNumbersWatermarkTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * @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 17
Source File: StateTest.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
/** * 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 18
Source File: MetaState.java From itext2 with GNU Lesser General Public License v3.0 | 4 votes |
public void saveState(PdfContentByte cb) { cb.saveState(); MetaState state = new MetaState(this); savedStates.push(state); }
Example 19
Source File: MetaState.java From gcs with Mozilla Public License 2.0 | 4 votes |
public void saveState(PdfContentByte cb) { cb.saveState(); MetaState state = new MetaState(this); savedStates.push(state); }
Example 20
Source File: LineSeparator.java From gcs with Mozilla Public License 2.0 | 4 votes |
/** * @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(); }