Replace words into a document by script operation

1
0
-1

Hello

I have a process to send by email a document with the data of the customer to be sign in by him.

The document is loading when the process execute( in each case)

After a user start a case, el process execute a script task to replace keywords for the customer's data.

This is my code in the taks's operation section:

import java.nio.charset.Charset
import java.nio.charset.CharsetEncoder

import org.bonitasoft.engine.bpm.document.Document
import org.bonitasoft.engine.bpm.document.DocumentValue
import org.codehaus.groovy.runtime.EncodingGroovyMethods

import java.nio.charset.StandardCharsets;

Document doc =apiAccessor.getProcessAPI().getDocument(docuActaAcuerdo.getId());

String storageId = doc.getContentStorageId();

byte[] content = apiAccessor.getProcessAPI().getDocumentContent(storageId);

String texto = new String(content,StandardCharsets.UTF_8);

texto = texto.replace("keyWord", solicitud.getLastName());

return new DocumentValue(texto.getBytes(StandardCharsets.UTF_8),"application/pdf","acta.pdf");

The process and task execute with out errors, but when I try to open de generated document, is empty because I thought that te problem was here:String texto = new String(content,StandardCharsets.UTF_8);

Any help?

Thanks a Lot

Comments

Submitted by Dibyajit.Roy on Sat, 06/11/2022 - 19:03

Hello ,
I am not sure if you can work with PDF directly with Document Class.
Why don't you use any Jar like Apache POI or iText.
Its very easy to manipulate PDF's using external library.

Personally , I would build a new Documents rather than editing existing one.

Regards

2 answers

1
0
-1

Im sorry, wrong section

1
0
-1

Hi,

PDF files are not plain text files. The String texto = new String(content,StandardCharsets.UTF_8); won't return the document text content.

This would work for plain text files however.

HTH
Romain

Comments

Submitted by remoncristian on Tue, 06/14/2022 - 14:24

Hi , thanks a lot for your answers...

The original document is a .doc (microsoft word). Into a script task I need to replace some keywords for customer's data

The main question is how a get/transform the content (byte[]) in a String for do the replace.?

I try this:

String texto = "Hellos world!";

return new DocumentValue(texto.getBytes(StandardCharsets.UTF_8),"application/pdf","acta.pdf");

OR

String texto = "Hellos world!";

return new DocumentValue(texto.getBytes(StandardCharsets.UTF_8),"application/doc","acta.doc");

Its work fine!.

Any other idea?...

THANKS A LOT!!

Notifications