3 Replies Latest reply on Aug 30, 2010 1:59 PM by Martin Dames

    How do I position text in pdf absolutly?

    Bernhard Hurzeler Novice

      Can anyone tell me how we can absolutly position text in pdf using the SEAM pdf tags.


      iText has:


      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("iTextExample.pdf"));
      PdfContentByte cb = writer.getDirectContent();
      // add text at an absolute position
      cb.beginText();
      cb.setTextMatrix(100, 300);
      cb.showText("Text at position 100, 300.");
      cb.endText();
      



      but I can't see any SEAM tag to do this.


      If we need to extend the SEAM capability, how do you suggest we do this in the best way?


      Thanks for your help!

        • 1. Re: How do I position text in pdf absolutly?
          Martin Dames Newbie

          Hi,


          I need this information also. Is there any way to integrate iText manually into Seam? Without Facelets? I do have a PDF creating class, which I really like to reuse.


          Thank you.

          • 2. Re: How do I position text in pdf absolutly?
            Martin Dames Newbie

            Hi,


            I found a dirty solution for this! You must use your iText natively with the PdfContentByte. Do this you should


            - inherit UIDocument from Seam, which does the PDF magic for you.
            - overwrite method encodeEnd(..)
            - get the PdfContentByte with super.getPdfContent();
            - use iText natively
            - use another binding in your facelets page


            Here is an example for my quick'n'dirty solution:


            ...
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.pdf.ui.UIDocument;

            import com.lowagie.text.pdf.PdfContentByte;
            import com.lowagie.text.pdf.PdfPCell;
            import com.lowagie.text.pdf.PdfPTable;

            @Name("myuidoc")
            public class PDF extends UIDocument {
                 
                 @Override
                 public void encodeEnd(FacesContext arg0) throws IOException {
                      PdfContentByte cb =  super.getPdfContent();
                      
                      PdfPTable invoiceMetaDataTable = new PdfPTable(2);
                      invoiceMetaDataTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
                      invoiceMetaDataTable.addCell("Datum:");
                      invoiceMetaDataTable.addCell(new Date().toString());
                      invoiceMetaDataTable.addCell("Steuer Nr.:");
                      invoiceMetaDataTable.addCell("XXXXXXX");
                      invoiceMetaDataTable.addCell("Rechnungs Nr.:");
                      invoiceMetaDataTable.addCell("659");
                      invoiceMetaDataTable.setTotalWidth(200.0f);
                      
                      PdfPTable invoiceMetaDataTable0 = new PdfPTable(1);
                      invoiceMetaDataTable0.setTotalWidth(200.0f);
                      invoiceMetaDataTable0.getDefaultCell().setBorderWidth(1.5f);
                      invoiceMetaDataTable0.addCell(invoiceMetaDataTable);
                      invoiceMetaDataTable0.writeSelectedRows(0, 3, 350, 600, cb);
                      
                      super.encodeEnd(arg0);
                 }
            }


            Facelts:


            <p:document xmlns:ui="http://java.sun.com/jsf/facelets"
                        xmlns:f="http://java.sun.com/jsf/core"
                        xmlns:p="http://jboss.com/products/seam/pdf"
                        title="Invoice"
                        keywords="invoice, rechnung"
                        subject="Invoice"
                        binding="#{myuidoc}">


            Hope this helped!

            • 3. Re: How do I position text in pdf absolutly?
              Martin Dames Newbie

              Ok.... where is the preview button? :)




              Here is an example for my quick'n'dirty solution:


              ...
              import org.jboss.seam.annotations.Name;
              import org.jboss.seam.pdf.ui.UIDocument;
              
              import com.lowagie.text.pdf.PdfContentByte;
              import com.lowagie.text.pdf.PdfPCell;
              import com.lowagie.text.pdf.PdfPTable;
              
              @Name("myuidoc")
              public class PDF extends UIDocument {
                   
                   @Override
                   public void encodeEnd(FacesContext arg0) throws IOException {
                        PdfContentByte cb =  super.getPdfContent();
                        
                        PdfPTable invoiceMetaDataTable = new PdfPTable(2);
                        invoiceMetaDataTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
                        invoiceMetaDataTable.addCell("Datum:");
                        invoiceMetaDataTable.addCell(new Date().toString());
                        invoiceMetaDataTable.addCell("Steuer Nr.:");
                        invoiceMetaDataTable.addCell("XXXXXXX");
                        invoiceMetaDataTable.addCell("Rechnungs Nr.:");
                        invoiceMetaDataTable.addCell("659");
                        invoiceMetaDataTable.setTotalWidth(200.0f);
                        
                        PdfPTable invoiceMetaDataTable0 = new PdfPTable(1);
                        invoiceMetaDataTable0.setTotalWidth(200.0f);
                        invoiceMetaDataTable0.getDefaultCell().setBorderWidth(1.5f);
                        invoiceMetaDataTable0.addCell(invoiceMetaDataTable);
                        invoiceMetaDataTable0.writeSelectedRows(0, 3, 350, 600, cb);
                        
                        super.encodeEnd(arg0);
                   }
              }






              Facelts:




              <p:document xmlns:ui="http://java.sun.com/jsf/facelets"
                          xmlns:f="http://java.sun.com/jsf/core"
                          xmlns:p="http://jboss.com/products/seam/pdf"
                          title="Invoice"
                          keywords="invoice, rechnung"
                          subject="Invoice"
                          binding="#{myuidoc}">
              





              Hope this helped!