4 Replies Latest reply on Apr 10, 2007 8:20 PM by Jason Long

    Request for PDF p:element tag

    Dustin Norlander Apprentice

      Hello,

      I am working with the PDF support and I would like to include custom elements into the PDF. Specifically I user jfreechart and would like to include the chart (*not rendered as an image*). I can easily export the chart into a com.lowagie.text.Element. So I think it would be very useful to be able to include that directly into the PDF maybe <p:element> ?

      I think this tag would could come in handy for things beyond including charts.

      Thanks,
      Dustin

        • 1. Re: Request for PDF p:element tag
          Norman Richards Master

          That's an interesting possibility. I had intended that people would extend the PDF support by creating a subclass of ITextComponent. There's a tiny bit of XML overhead required to register the tag, but otherwise it's fairly trivial amount of code.

          I've gone ahead and added p:element experimentally. To use it you'll need to move the itext jar out of the war (into the ear or as an app-server library) or move your components into the WAR. Let me know if it is useful.

          • 2. Re: Request for PDF p:element tag
            Dustin Norlander Apprentice

            Norman,

            Many thanks for implementing this. Unfortunately it doesn't work for my purpose. I forgot that I needed access to the DocWriter in order to draw the graph onto the pdf. The element tag may end up being useful for other purposes though.

            I extended ITextComponent to create a simple tag to renderer JFreeCharts. Possibly someone will find it useful.

            public class PdfGraph extends ITextComponent {
            
             JFreeChart graph = null;
             Element element = null;
             float height;
             float width;
            
             /**
             *
             */
             public PdfGraph() {
            
             }
            
             public void setValue(JFreeChart graph) {
             this.graph = graph;
             }
            
             /* (non-Javadoc)
             * @see org.jboss.seam.pdf.ui.ITextComponent#createITextObject(javax.faces.context.FacesContext)
             */
             @Override
             public void createITextObject(FacesContext context) {
             graph = (JFreeChart) valueBinding(context, "value", graph);
             height = (Float) valueBinding(context, "height", height);
             width = (Float) valueBinding(context, "width", width);
            
             UIDocument doc = (UIDocument)this.findITextParent(this, UIDocument.class);
             DocWriter writer = doc.getWriter();
            
             if (writer instanceof PdfWriter) {
             try {
             PdfContentByte cb = ((PdfWriter)writer).getDirectContent();
             PdfTemplate tp = cb.createTemplate(width, height);
             Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
             Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
             this.graph.draw(g2d, r2d);
             g2d.dispose();
             this.element = new ImgTemplate(tp);
             } catch (Exception x) {
             x.printStackTrace();
             }
             }
             }
            
            
             /* (non-Javadoc)
             * @see org.jboss.seam.pdf.ui.ITextComponent#getITextObject()
             */
             @Override
             public Object getITextObject() {
             return element;
             }
            
             /* (non-Javadoc)
             * @see org.jboss.seam.pdf.ui.ITextComponent#handleAdd(java.lang.Object)
             */
             @Override
             public void handleAdd(Object arg0) {
             throw new RuntimeException("No children allowed");
             }
            
             /* (non-Javadoc)
             * @see org.jboss.seam.pdf.ui.ITextComponent#removeITextObject()
             */
             @Override
             public void removeITextObject() {
             element = null;
             }
            
             public float getHeight() {
             return this.height;
             }
            
             public void setHeight(float height) {
             this.height = height;
             }
            
             public float getWidth() {
             return this.width;
             }
            
             public void setWidth(float width) {
             this.width = width;
             }
            
            }


            thanks,
            Dustin

            • 3. Re: Request for PDF p:element tag
              Norman Richards Master

              Yes, that is the recommended way to do it. I will probably remove p:element unless someone else find it useful.

              Just so you know, Seam 1.3 will have JFreeChart integration. I'd welcome feedback on what features are most useful.

              • 4. Re: Request for PDF p:element tag
                Jason Long Master

                I am doing the following in order to add a vector logo to my PDF documents. I had my graphics artist make the logo in vector and then save it as a PDF. I then read this PDF fragment from the classpath and then use iText to write it to an empty region of a PDF that I created using JasperReports. I wanted t do it this way so that the logo would not be distored regardless of the size or zoom of the PDF. There is a code sample below. Is this possible using Seam PDF?

                 InputStream in = getClass().getClassLoader().getResourceAsStream("pipetracker/model/state/type/pipetracker_paperwork_logo.pdf");
                 PdfReader pdfReaderLogo = new PdfReader(in);
                 Rectangle psizeLogo = pdfReaderLogo.getPageSize(1);
                
                 // step 1: creation of a document-object
                 Document document = new Document(psize);
                // // step 2: we create a writer that listens to the document
                 PdfWriter writer = PdfWriter.getInstance(document, baos2);
                 document.open();
                 PdfContentByte cb = writer.getDirectContent();
                
                 PdfImportedPage pageReport = writer.getImportedPage(pdfReader, 1);
                 PdfImportedPage pageLogo = writer.getImportedPage(pdfReaderLogo, 1);
                
                 cb.addTemplate(pageReport, 0, 0);
                 cb.addTemplate(pageLogo, .4f, 0f, 0f, .4f, 20, 610);
                
                 document.close();