Estou tendo dificuldades para manipular um modelo de formulário em formato .docx utilizando a biblioteca APACHE POI 4.0, eu renomeei os campos para formatação os deixando entre “#”, o problema é o seguinte, o código gera uma cópia do modelo, porém não copia a marca d’água de fundo, nem obedece parágrafos e fonte… o código que tenho até o momento é este.
public static void main(String args[]) throws FileNotFoundException, IOException {
int count = 0;
XWPFDocument document = new XWPFDocument();
XWPFDocument docx = new XWPFDocument(new FileInputStream("teste.docx"));
XWPFWordExtractor we = new XWPFWordExtractor(docx);
String text = we.getText() ;
text = text.replace("#NOME#", "NOME TESTE");
text = text.replace("#PAIS#", "PAIS TESTE");
text = text.replace("#PROFISSAO#", "PROFISSÃO TESTE");
text = text.replace("#CPF#", "CPF TESTE");
text = text.replace("#CIDADE#", "CIDADE TESTE");
char[] c = text.toCharArray();
for(int i= 0; i < c.length;i++){
if(c[i] == '\n'){
count ++;
}
}
System.out.println(c[0]);
StringTokenizer st = new StringTokenizer(text,"\n");
XWPFParagraph para = document.createParagraph();
para.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = para.createRun();
List<XWPFParagraph>paragraphs = new ArrayList<XWPFParagraph>();
List<XWPFRun>runs = new ArrayList<XWPFRun>();
int k = 0;
for(k=0;k<count+1;k++){
paragraphs.add(document.createParagraph());
}
k=0;
while(st.hasMoreElements()){
paragraphs.get(k).setAlignment(ParagraphAlignment.LEFT);
paragraphs.get(k).setSpacingAfter(0);
paragraphs.get(k).setSpacingBefore(0);
run = paragraphs.get(k).createRun();
run.setText(st.nextElement().toString());
k++;
}
document.write(new FileOutputStream("teste2.docx"));
}