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

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

Introduction

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

Prototype

public void saveState() 

Source Link

Document

Saves the graphic state.

Usage

From source file:org.kuali.kfs.module.purap.pdf.PurapPdf.java

License:Open Source License

/**
 * Overrides the method in PdfPageEventHelper from itext to include our watermark text to indicate that
 * this is a Test document and include the environment, if the environment is not a production environment.
 *
 * @param writer    The PdfWriter for this document.
 * @param document  The document.//ww  w  .  j  av  a2 s . c  o m
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onStartPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
@Override
public void onStartPage(PdfWriter writer, Document document) {
    if (!KRADUtils.isProductionEnvironment()) {
        PdfContentByte cb = writer.getDirectContentUnder();
        cb.saveState();
        cb.beginText();
        cb.setFontAndSize(helv, 48);
        String watermarkText = "Test document (" + environment + ")";
        cb.showTextAligned(Element.ALIGN_CENTER, watermarkText, document.getPageSize().width() / 2,
                document.getPageSize().height() / 2, 45);
        cb.endText();
        cb.restoreState();
    }
    if (GlobalVariables.getUserSession() != null
            && GlobalVariables.getUserSession().retrieveObject("isPreview") != null) {
        GlobalVariables.getUserSession().removeObject("isPreview");
        PdfContentByte cb = writer.getDirectContentUnder();
        cb.saveState();
        cb.beginText();
        cb.setFontAndSize(helv, 48);
        String watermarkText = "DRAFT";
        cb.showTextAligned(Element.ALIGN_CENTER, watermarkText, document.getPageSize().width() / 2,
                document.getPageSize().height() / 2, 45);
        cb.endText();
        cb.restoreState();
    }
}

From source file:org.kuali.kfs.module.purap.pdf.PurapPdf.java

License:Open Source License

/**
 * Overrides the method in PdfPageEventHelper from itext to write the headerTable, compose the footer and show the
 * footer./*from   www .  j  a v  a  2s.c  o  m*/
 *
 * @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();
}

From source file:org.kuali.kfs.module.purap.pdf.PurchaseOrderQuoteRequestsPdf.java

License:Open Source License

/**
 * Overrides the method in PdfPageEventHelper from itext to compose the footer and show the
 * footer./*from   w  w  w.ja v a2s  .  c o  m*/
 *
 * @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();
}

From source file:org.kuali.kfs.module.tem.pdf.Coversheet.java

License:Open Source License

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);//from  www  . j ava 2  s  .c  o m
    cb.fill();
    cb.restoreState();
}

From source file:org.kuali.kfs.module.tem.pdf.Coversheet.java

License:Open Source License

protected void lowerLeftAlignmentMark(final PdfContentByte cb) {
    cb.saveState();
    cb.rectangle(ALIGNMENT_MARGIN, ALIGNMENT_MARGIN, ALIGNMENT_MARK_WIDTH, ALIGNMENT_MARK_HEIGHT);
    cb.setColorFill(BLACK);/* w w  w  .ja va2s.  co  m*/
    cb.fill();
    cb.restoreState();
}

From source file:org.kuali.kfs.module.tem.pdf.Coversheet.java

License:Open Source License

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);//  www  .  j ava 2  s.c o m
    cb.fill();
    cb.restoreState();
}

From source file:org.kuali.kfs.module.tem.pdf.Coversheet.java

License:Open Source License

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);/*from w  w  w  .  ja  v a 2s  .c  o  m*/
    cb.setColorFill(BLACK);
    cb.fill();
    cb.restoreState();
}

From source file:org.kuali.ole.module.purap.pdf.PurapPdf.java

License:Educational Community License

/**
 * Overrides the method in PdfPageEventHelper from itext to include our watermark text to indicate that
 * this is a Test document and include the environment, if the environment is not a production environment.
 *
 * @param writer   The PdfWriter for this document.
 * @param document The document./*from  w  w w.  ja  v  a2  s.c o  m*/
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onStartPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
public void onStartPage(PdfWriter writer, Document document) {
    if (!KRADUtils.isProductionEnvironment()) {
        PdfContentByte cb = writer.getDirectContentUnder();
        cb.saveState();
        cb.beginText();
        cb.setFontAndSize(helv, 48);
        String watermarkText = "Test document (" + environment + ")";
        cb.showTextAligned(Element.ALIGN_CENTER, watermarkText, document.getPageSize().width() / 2,
                document.getPageSize().height() / 2, 45);
        cb.endText();
        cb.restoreState();
    }
    if (GlobalVariables.getUserSession() != null
            && GlobalVariables.getUserSession().retrieveObject("isPreview") != null) {
        GlobalVariables.getUserSession().removeObject("isPreview");
        PdfContentByte cb = writer.getDirectContentUnder();
        cb.saveState();
        cb.beginText();
        cb.setFontAndSize(helv, 48);
        String watermarkText = "DRAFT";
        cb.showTextAligned(Element.ALIGN_CENTER, watermarkText, document.getPageSize().width() / 2,
                document.getPageSize().height() / 2, 45);
        cb.endText();
        cb.restoreState();
    }
}

From source file:org.kuali.ole.module.purap.pdf.PurapPdf.java

License:Educational Community License

/**
 * Overrides the method in PdfPageEventHelper from itext to write the headerTable, compose the footer and show the
 * footer./*from ww  w  . j  a  v  a2s .c om*/
 *
 * @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();
    // 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();
}

From source file:org.mapfish.print.map.MapChunkDrawer.java

License:Open Source License

public void renderImpl(Rectangle rectangle, PdfContentByte dc) {
    final PJsonObject parent = context.getGlobalParams();
    PJsonArray layers = parent.getJSONArray("layers");
    String srs = parent.getString("srs");

    if (!context.getConfig().isScalePresent(transformer.getScale())) {
        throw new InvalidJsonValueException(params, "scale", transformer.getScale());
    }/*from   ww  w. j  a  v  a 2s. com*/

    Transformer mainTransformer = null;
    if (!Double.isNaN(overviewMap)) {
        //manage the overview map
        mainTransformer = context.getLayout().getMainPage().getMap().createTransformer(context, params);
        transformer.zoom(mainTransformer, (float) (1.0 / overviewMap));
        transformer.setRotation(0); //overview always north up!
        context.setStyleFactor((float) (transformer.getPaperW() / mainTransformer.getPaperW() / overviewMap));
        layers = parent.optJSONArray("overviewLayers", layers);
    }

    transformer.setMapPos(rectangle.getLeft(), rectangle.getBottom());
    if (rectangle.getWidth() < transformer.getPaperW() - 0.2) {
        throw new RuntimeException("The map width on the paper is wrong");
    }
    if (rectangle.getHeight() < transformer.getPaperH() - 0.2) {
        throw new RuntimeException("The map height on the paper is wrong (" + rectangle.getHeight() + "!="
                + transformer.getPaperH() + ")");
    }

    //create the readers/renderers
    List<MapReader> readers = new ArrayList<MapReader>(layers.size());
    for (int i = 0; i < layers.size(); ++i) {
        PJsonObject layer = layers.getJSONObject(i);
        if (mainTransformer == null || layer.optBool("overview", true)) {
            final String type = layer.getString("type");
            MapReader.create(readers, type, context, layer);
        }
    }

    //check if we cannot merge a few queries
    for (int i = 1; i < readers.size();) {
        MapReader reader1 = readers.get(i - 1);
        MapReader reader2 = readers.get(i);
        if (reader1.testMerge(reader2)) {
            readers.remove(i);
        } else {
            ++i;
        }

    }

    //draw some background
    if (backgroundColor != null) {
        dc.saveState();
        try {
            dc.setColorFill(backgroundColor);
            dc.rectangle(rectangle.getLeft(), rectangle.getBottom(), rectangle.getWidth(),
                    rectangle.getHeight());
            dc.fill();
        } finally {
            dc.restoreState();
        }
    }

    //Do the rendering.
    //
    //Since we need to load tiles in parallel from the
    //servers, what follows is not trivial. We don't write directly to the PDF's
    //DirectContent, we always go through the ParallelMapTileLoader that will
    //make sure that everything is added to the PDF in the correct order.
    //
    //All uses of the DirectContent (dc) or the PDFWriter is forbiden outside
    //of renderOnPdf methods and when they are used, one must take a lock on
    //context.getPdfLock(). That is done for you when renderOnPdf is called, but not done
    //in the readTile method. That's why PDFUtils.getImage needs to do it when
    //creating the template.
    //
    //If you don't follow those rules, you risk to have random inconsistency
    //in your PDF files and/or infinite loops in iText.
    ParallelMapTileLoader parallelMapTileLoader = new ParallelMapTileLoader(context, dc);
    dc.saveState();
    try {
        final PdfLayer mapLayer = new PdfLayer(name, context.getWriter());
        transformer.setClipping(dc);

        //START of the parallel world !!!!!!!!!!!!!!!!!!!!!!!!!!!

        for (int i = 0; i < readers.size(); i++) {
            final MapReader reader = readers.get(i);

            //mark the starting of a new PDF layer
            parallelMapTileLoader.addTileToLoad(new MapTileTask.RenderOnly() {
                public void renderOnPdf(PdfContentByte dc) throws DocumentException {
                    PdfLayer pdfLayer = new PdfLayer(reader.toString(), context.getWriter());
                    mapLayer.addChild(pdfLayer);
                    dc.beginLayer(pdfLayer);
                }
            });

            //render the layer
            reader.render(transformer, parallelMapTileLoader, srs, i == 0);

            //mark the end of the PDF layer
            parallelMapTileLoader.addTileToLoad(new MapTileTask.RenderOnly() {
                public void renderOnPdf(PdfContentByte dc) throws DocumentException {
                    dc.endLayer();
                }
            });
        }
    } finally {
        //wait for all the tiles to be loaded
        parallelMapTileLoader.waitForCompletion();

        //END of the parallel world !!!!!!!!!!!!!!!!!!!!!!!!!!

        dc.restoreState();
    }

    if (mainTransformer != null) {
        //only for key maps: draw the real map extent
        drawMapExtent(dc, mainTransformer);
        context.setStyleFactor(1.0f);
    }
}