Example usage for com.lowagie.text.pdf PdfGraphics2D getClip

List of usage examples for com.lowagie.text.pdf PdfGraphics2D getClip

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfGraphics2D getClip.

Prototype

public Shape getClip() 

Source Link

Usage

From source file:tufts.vue.PresentationNotes.java

License:Educational Community License

public static void createPresentationSlidesDeck(File file) {
    // step 1: creation of a document-object
    final Document document = new Document(PageSize.LETTER.rotate());

    try {/*w  w  w . java  2  s  .  c om*/
        GUI.activateWaitCursor();
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file            
        final PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        writer.setDefaultColorspace(PdfName.DEFAULTRGB, null);
        writer.setStrictImageSequence(true);
        // step 3: we open the document

        document.open();

        final float pageWidth = document.getPageSize().getWidth();
        final float pageHeight = document.getPageSize().getHeight();
        final float fillWidth = pageWidth - 70;
        final float fillHeight = pageHeight - 70;

        if (DEBUG.Enabled) {
            System.out.println("\n---------------------------------");
            System.out.println("PDF DOCUMENT: pageSize " + document.getPageSize());
            System.out.println("fillWidth=" + fillWidth + " fillHeight=" + fillHeight);
        }
        int currentIndex = VUE.getActivePathway().getIndex();
        VUE.getActivePathway().setIndex(-1);
        for (LWPathway.Entry entry : VUE.getActivePathway().getEntries()) {

            if (DEBUG.Enabled)
                Log.debug("\n\nHANDLING DECK ENTRY " + entry);
            final LWSlide slide = entry.produceSlide();
            final LWComponent toDraw = (slide == null ? entry.node : slide);

            final PdfTemplate template = PdfTemplate.createTemplate(writer, fillWidth, fillHeight);
            final PdfGraphics2D graphics = (PdfGraphics2D) template.createGraphics(fillWidth, fillHeight,
                    getFontMapper(), false, 60.0f);
            final DrawContext dc = new DrawContext(graphics, 1.0);
            //                 //final DrawContext dc = new DrawContext(graphics, scale);
            //final DrawContext dc = new DrawContext(graphics, toDraw); // ideally, should use this
            dc.setClipOptimized(false);
            dc.setInteractive(false); // should be un-needed
            dc.setPrintQuality();

            // We set dc.focused to the node, which is needed for portals so they know to render contents in their clip-region.
            // Normally a portal knows to do this because it's the focal (dc.focal), but in this case, the dc.focal could,
            // at best, be the slide.  The current code actually uses NO focal in creating the DrawContext above,
            // and I'm not changing that just now as that would require lots of regression testing of printing.  SMF 6/24/10
            dc.focused = entry.node;

            if (DEBUG.Enabled) {
                Log.debug("DRAWING INTO " + dc + " g=" + graphics + " clip="
                        + tufts.Util.fmt(graphics.getClip()));
                if (DEBUG.PDF) {
                    dc.g.setColor(Color.green);
                    dc.g.fillRect(-Short.MAX_VALUE / 2, -Short.MAX_VALUE / 2, Short.MAX_VALUE, Short.MAX_VALUE);
                }
            }

            try {
                if (DEBUG.Enabled)
                    dc.clearDebug();
                toDraw.drawFit(dc, 0);
            } catch (Throwable t) {
                Log.error("exception drawing " + toDraw, t);
            }

            try {

                if (DEBUG.Enabled)
                    Log.debug("painted " + DrawContext.getDebug() + " to " + dc);

                if (DEBUG.PDF) {
                    final String dcDesc = dc.toString()
                            + String.format(" scale=%.1f%%", dc.g.getTransform().getScaleX() * 100);
                    dc.setRawDrawing();
                    dc.g.setColor(Color.red);
                    dc.g.setFont(VueConstants.FixedSmallFont);
                    dc.g.drawString(dcDesc, 10, fillHeight - 27);
                    dc.g.drawString(entry.toString(), 10, fillHeight - 16);
                    dc.g.drawString(toDraw.toString(), 10, fillHeight - 5);
                }

                // the graphics dispose appears to be very important -- we've seen completely intermittant
                // problems with generating many page PDF documents, which would be well explained by
                // java or internal itext buffers running out of memory.
                graphics.dispose();

                document.add(Image.getInstance(template));
                document.newPage();
            } catch (Throwable t) {
                Log.error("exception finishing " + toDraw + " in " + dc, t);
            }
        }
        VUE.getActivePathway().setIndex(currentIndex);
        if (DEBUG.Enabled)
            Log.debug("PROCESSED ALL ENTRIES");

    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    } finally {
        GUI.clearWaitCursor();
    }

    // step 5: we close the document
    document.close();
}