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
How do you deploy your changes on the server? Do you use Maven in development of the code?

looks like you didn't add jar into the lib folder…

1) Download IText archive http://sourceforge.net/projects/itext/files/iText/iText5.5.5/
2) unzip it
3) Put needed jars into {tomcat}/webapps/alfresco/WEB-INF/lib

Yes I use Maven. I did add the jar - I added even three…. But your version took care of the problem! Thanks
Ok I created the table, but it appears that when I insert the table I somehow delete the content that was in the pdf document. And since I have to insert cyrillic text I put it in the cells but it doesn't show. I even tried to set width with

table.setTotalWidth(104);

but it did nothing
Thanks a lot Smiley Very Happy,
Irena

s_palyukh
Star Contributor
Star Contributor
Should be something like that (http://stackoverflow.com/questions/3335126/itext-add-content-to-existing-pdf-file😞



// Create output PDF
Document document = new Document(PageSize.A4);

ContentWriter writer = contentService.getWriter(actionedUponNodeRef, ContentModel.PROP_CONTENT, true);

PdfWriter writer = PdfWriter.getInstance(document, writer.getContentOutputStream());

document.open();

PdfContentByte cb = writer.getDirectContent();

// Load existing PDF

ContentReader cread = contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
PdfReader reader = new PdfReader(cread.getContentInputStream());
PdfImportedPage page = writer.getImportedPage(reader, 1);

// Copy first page of existing PDF into output PDF
document.newPage();
cb.addTemplate(page, 0, 0);

// Add your new data / text here
// for example…
document.add(new Paragraph("my timestamp"));

document.close();

I am going to try that, but before that I have one more question. When I try to read a property from my custom content model I get an error. I am trying with this code


….
   QName PROP_QNAME_NAME = QName.createQName(myModel.NAMESPACE_MY_MODEL, "name");   
   String fileName = (String) nodeService.getProperty(actionedUponNodeRef, PROP_QNAME_NAME);
   String[] array = fileName.split("_");
….

cell = new PdfPCell(new Phrase(" "+array[0]));
        cell.setRowspan(2);
        table.addCell(cell);

….



I guess it's because of the nodeService. Can I do this? (Add nodeService and contentService? )



   <bean id="insert-archive-stamp" class="neoarchive.action.executer.InsertArchiveStamp" parent="action-executer">
      <property name="contentService">
         <ref bean="contentService" />
      </property>

                <property name="nodeService">
         <ref bean="NodeService" />
      </property>

      <property name="publicAction">
         <value>false</value>
      </property>
   </bean>




Thanks,
Irena

s_palyukh
Star Contributor
Star Contributor
sure, you can add any service that you need and inject it through the spring context

Before I was able to add my filled table, but now it isn't showing at all.
I just need it to be in the corner of the first page.
- How can I insert my table/text in the right top corner without moving the content that previously was there?
- What happens if the pdf I am inserting my text/table has more than one page?
- How do I take the rest of the pages?
This is my code



Document document = new Document(PageSize.A4);
       // step 1
       try{
          ContentWriter writer = contentService.getWriter(actionedUponNodeRef, ContentModel.PROP_CONTENT, true);
          // step 2
          PdfWriter.getInstance(document, writer.getContentOutputStream());
          // step 3
          document.open();
      
          //NEW
          PdfContentByte cb =  (PdfContentByte)((PdfWriter) writer).getDirectContent();
          ContentReader cread = contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
          PdfReader reader = new PdfReader(cread.getContentInputStream());
          PdfImportedPage page = (PdfImportedPage)((PdfWriter) writer).getImportedPage(reader, 1);
          document.newPage();
          cb.addTemplate(page, 0, 0);
      
      
    // a table with three columns
        PdfPTable table = new PdfPTable(3);
        // the cell object
        PdfPCell cell;
       
        // we add a cell with colspan 3
        cell = new PdfPCell(new Phrase("Primeno " +date[0]));// day + "." + month + "." + year ));
        cell.setColspan(4);
        table.addCell(cell);
       
        cell = new PdfPCell(new Phrase("Org.edin"));
        table.addCell(cell);
        table.addCell("Broj");
        table.addCell("Podbroj");
       
        cell = new PdfPCell(new Phrase(""+array[0]));
        cell.setRowspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
       
        cell = new PdfPCell(new Phrase(""+array[1]));
        cell.setRowspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
       
        cell = new PdfPCell(new Phrase(""+array[2]));
        cell.setRowspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
       
        table.setWidthPercentage(30);
        table.setHorizontalAlignment(Element.ALIGN_RIGHT);
      
       // step 4
       document.add(table);
      
       // step 5
       document.close();
     }
     catch(Exception e){
       
     }
      


s_palyukh
Star Contributor
Star Contributor
Take a look at the following code http://itextpdf.com/sandbox/events/Watermarking . Maybe it will help you