Wednesday, June 24, 2015

Convert the NotesDocument into PDF

Here is the interesting stuff comes again, I need to convert all the documents into pdf file. I started my research on that, initially I thought it was easy because I have already worked on that but last time I did not handle/ convert Rich Text item. Creating table, designing the PDF template all are very easy. But getting the handle of inline images in Rich Text Item is very difficult, if you do not want to spend money.

 I have done my design using itext.jar, and created my own PDF template for document. Now I captured all the infos like Text, Text List, Date/Time, Names and etc., except RTF and RT lite field.

 I started to work on attach the file into pdf, started with hard but ended with very easy. I extracted file from RTF and saved in the local machine temp folder and attached into the PDF. Thanks a lot to iText.jar.

             RichTextItem rt = (RichTextItem)  doc.getFirstItem("Body");
              Vector ve = rt.getEmbeddedObjects();
              EmbeddedObject eo = null;
           Enumeration e = ve.elements();
           if(ve.size() > 0)
           {
            attachments = new String[ve.size()];
               while (e.hasMoreElements()) {
                eo = (EmbeddedObject)e.nextElement();
                eo.extractFile("C:\\Temp\\" +eo.getSource());
                attachments[count] = "C:\\Temp\\" +eo.getSource();
                  }
           }
Code for attaching the file into PDF file... The below is not from my knowledge it is taken from one blog, I forgot because long back I have taken this piece of code. Sorry and don't mind :D

public void addAttachments(
 String src, String dest, String[] attachments) throws IOException, DocumentException {
 PdfReader reader = new PdfReader(src);
 PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
 for (int i = 0; i < attachments.length; i++) {
   addAttachment(stamper.getWriter(), new File(attachments[i]));
 }
 
 stamper.close();
}

protected void addAttachment(PdfWriter writer, File src) throws IOException {
 PdfFileSpecification fs =
   PdfFileSpecification.fileEmbedded(writer, src.getAbsolutePath(), src.getName(), null);
 writer.addFileAttachment(src.getName().substring(0, src.getName().indexOf('.')), fs);
}
Once everything is set I have entered into the tough task, that is converting the inline image to attachment/ embed the same image in PDF file. Both are difficult.


  1. I just gone through the PDF creator class and convert the document into pdf file like Prrint -> Save as PDF using PDF creator tool. But the result, I faced so many difficulties, somemachine adopt the code and close the PDF file, many machine can not close the PDF object so my notes gets crashed. So I do not want to go with PDF creator. For this PDF creator needs to be installed on your PC.
  2. I just googled and found that midas is the lotus script library, it is good but it is not free software. 
  3. I just found many automated tools like swing PDF converter, etc., but it is quite expensive. I do not want to spend money on that. 
  4. I have gone through many blogs and discussion portals. I could not find the proper code for that.
Finally I just created webpage for my application, we have many easy tool for converting the HTML page to PDF. I just converted the Notes HTML document to PDF file, and extract the attachment and saved it in local folder. I embedded the attachment into the above pdf file. I have reached my goal, I do not think it is good and efficient.

Still my research on handling the inline image of RTI is going on, :( :( :(



No comments:

Post a Comment