cancel
Showing results for 
Search instead for 
Did you mean: 

Add Text/Table with values to pdf !!!

irenailievska
Champ on-the-rise
Champ on-the-rise
I need to add small table filled with properties from my custom content model in the upper right corner of the pdf document. I am supposed to add this to an existing pdf document. This should be executed when I activate the action defined in share-config-custom.xml (DocLibActions). I am a beginner in Alfresco and don`t know how to do this. Read something about iText, but I can't find how to open the pdf document. I have been looking at the tutorial from Jeff Potts about Custom Actions and so far I have added the action to the menu (you can see the action when you click more), have written a bean to register my java executer in service-context.xml and have created the Java class (It doesn't do anything at the moment). I am passing the nodeRef of the document, but how can I find it and open it with iText and add the table in the corner ? Since I have the nodeRef how can I get to the file itself? Is it possible to update the number of the version when I save the changes of the document? It is a little bit urgent so I would appreciate every help I get.

Thanks in advance,
Irena
16 REPLIES 16

s_palyukh
Star Contributor
Star Contributor

I have seen this post. It's very helpful and I was using it to write the code I need in order to show the action and call a java class as a executor. But I have no Idea how to get the pdf from nodeRef or how to insert a table in a specific place. If there is a way by using JavaScript I would be glad to try it, although I haven't worked with javaScript very much and would be needing some guidance.
Thanks in advance,
Irena

No, you can't use JS API to add table into PDF file. Use IText to create pdf table (http://itextpdf.com/examples/iia.php?id=75).

If you want to write into the file by NodeRef then look at the org.alfresco.service.cmr.repository.ContentService.




            ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
            writer.putContent(inputStream);



Ok thanks a lot for the directions Smiley Very Happy !!!

I have two more questions (sorry if they sound stupid - I don't have experience working with alfresco or java for that matter):

1. What is inputStream - because I need to insert table in the pdf document. Is it String or something else ?
2. How do I manipulate with writer ? Do I use it to create the table and to set the position for my table?
I am confused by this because in most of the examples and in the one you gave me, they create new document and I don`t know how to retrieve the existing one. They use :



Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        document.add(createFirstTable());
        // step 5
        document.close();



and as I have noticed they have


   public static final String RESULT = "results/part1/chapter04/first_table.pdf";


where they save the document. I need the document to stay in the same place.

Sorry but I am beginner and don`t really understand some of the things.

Thanks,
Irena

1) About inputStream -> http://tutorials.jenkov.com/java-io/inputstream.html
2) You need to get OutputStream from Writer, something like that:


Document document = new Document();
        // step 1
        ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
        // step 2
        PdfWriter.getInstance(document, writer.getContentOutputStream());
        // step 3
        document.open();
        // step 4
        document.add(createFirstTable());
        // step 5
        document.close();


If I use document.close() do I still need to use writter.putContent(inputStream) ?
Is this what I need from InputStream ?


InputStream inputStream = new ByteArrayInputStream(content.getBytes(encoding));

1) If I use document.close() do I still need to use writter.putContent(inputStream) ? - No, you don't
2) Is this what I need from InputStream ? - If you use PdfWriter to write content then you don't need inputstream

I tried doing it like you suggested but there are errors and I can't start the explorer at all.
Here Is my code:


  
package my.action.executer;

import java.util.List;

import my.model.myModel;

import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.ParameterDefinitionImpl;
import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

import com.itextpdf.text.Document;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class InsertArchiveStamp extends ActionExecuterAbstractBase
   implements InitializingBean {

    public static final String NAME = "watermark";
    public static final String PARAM_TEXT = "watermark-text";

    private ContentService contentService;
    protected NodeService nodeService;
   
    public void setContentService(ContentService contentService) {
   this.contentService = contentService;
    }

    public void afterPropertiesSet() throws Exception {
   Assert.notNull(contentService);
    }

    @Override
    protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {
       
         
       Document document = new Document();
       // step 1
       try{
       ContentWriter writer = contentService.getWriter(actionedUponNodeRef, ContentModel.PROP_CONTENT, true);
      
      // PdfContentByte canvas = ((PdfWriter) writer).getDirectContent();
       // step 2
       PdfWriter.getInstance(document, writer.getContentOutputStream());
       // step 3
       document.open();
      
      
       //////////////////////////////////////////////////////
     
    // a table with three columns
        PdfPTable table = new PdfPTable(4);
        // the cell object
        PdfPCell cell;
       
        // we add a cell with colspan 3
        cell = new PdfPCell(new Phrase("Примено:"));
        cell.setColspan(4);
        table.addCell(cell);
       
        cell = new PdfPCell(new Phrase("Орг.един."));
        table.addCell(cell);
        // we add the four remaining cells with addCell()
        table.addCell("Број:");
        table.addCell("Прилог:");
        table.addCell("Вредност:");
       
        cell = new PdfPCell(new Phrase(""));
        cell.setRowspan(2);
        table.addCell(cell);
       
        cell = new PdfPCell(new Phrase(""));
        cell.setRowspan(2);
        table.addCell(cell);
       
        cell = new PdfPCell(new Phrase(""));
        cell.setRowspan(2);
        table.addCell(cell);
       
        cell = new PdfPCell(new Phrase(""));
        cell.setRowspan(2);
        table.addCell(cell);
       
      
       //////////////////////////////////////////////
      
       // step 4
       document.add(table);
      
       // step 5
       document.close();
     }
     catch(Exception e){
       
     }
      
    }


    /* I THOUGHT TO PUT THE CREATING OF THE TABLE AS IN THE EXAMPLE - IN FUNCTION
    public static PdfPTable createTable() {
       // a table with three columns
        PdfPTable table = new PdfPTable(4);
        // the cell object
        PdfPCell cell;
       

        ////////////////////////////////////////////////////////////////////////////////////////////

              THIS CODE IS FOR GETTING THE VALUES FROM MY CONTENT MODEL
           
        //QName PROP_QNAME_NAME = QName.createQName(myModel.NAMESPACE_ALFRESCO_MODEL, "name");   
   //String fileName = (String) nodeService.getProperty(actionedUponNodeRef, PROP_QNAME_NAME);



        ///////////////////////////////////////////////////////////////////////////////////////////
        // we add a cell with colspan 3
        cell = new PdfPCell(new Phrase("Примено:"));
        cell.setColspan(4);
        table.addCell(cell);
       
        cell = new PdfPCell(new Phrase("Орг.един."));
        table.addCell(cell);
        // we add the four remaining cells with addCell()
        table.addCell("Број:");
        table.addCell("Прилог:");
        table.addCell("Вредност:");
       
        cell = new PdfPCell(new Phrase(""));
        cell.setRowspan(2);
        table.addCell(cell);
       
        cell = new PdfPCell(new Phrase(""));
        cell.setRowspan(2);
        table.addCell(cell);
       
        cell = new PdfPCell(new Phrase(""));
        cell.setRowspan(2);
        table.addCell(cell);
       
        cell = new PdfPCell(new Phrase(""));
        cell.setRowspan(2);
        table.addCell(cell);
       
        //table.setTotalWidth(104);
        //table.completeRow();
        // write the table to an absolute position
       // table.writeSelectedRows(0, -1, 169, table.getTotalHeight() + 20, canvas);
        return table;
    }
*/
    @Override
    protected void addParameterDefinitions(List<ParameterDefinition> paramList) {
   paramList
      .add(new ParameterDefinitionImpl(PARAM_TEXT,
         DataTypeDefinition.TEXT,
         false,
         getParamDisplayLabel(PARAM_TEXT)));
         
       return;
    }
   
    public void setNodeService(NodeService nodeService) {
      this.nodeService = nodeService;
   }
}

  



The action is called on click on the action defined in share-config-custom.xml and there is a bean defined in service-context.xml.


I need to create the table and add values from my content model. I have the piece of code marked in my executor class. I have no idea what could be wrong.
There is a file attached I copied from the console.

Thanks,
Irena

irenailievska
Champ on-the-rise
Champ on-the-rise
There says that there is no itextpdf.text.Element. I don't know how to resolve this