Tôi hiện đang làm việc với dự án Java bằng cách sử dụng apache poi. Bây giờ trong dự án của tôi, tôi muốn chuyển đổi tệp doc thành tệp pdf. Việc chuyển đổi được thực hiện thành công nhưng tôi chỉ nhận được văn bản trong pdf không phải bất kỳ kiểu văn bản hoặc màu văn bản nào. Tệp pdf của tôi trông giống như màu đen & màu trắng. Trong khi tệp tài liệu của tôi được tô màu và có kiểu văn bản khác nhau.Apache POI HWPF - sự cố khi chuyển đổi tệp doc sang pdf
Đây là mã của tôi,
POIFSFileSystem fs = null;
Document document = new Document();
try {
System.out.println("Starting the test");
fs = new POIFSFileSystem(new FileInputStream("/document/test2.doc"));
HWPFDocument doc = new HWPFDocument(fs);
WordExtractor we = new WordExtractor(doc);
OutputStream file = new FileOutputStream(new File("/document/test.pdf"));
PdfWriter writer = PdfWriter.getInstance(document, file);
Range range = doc.getRange();
document.open();
writer.setPageEmpty(true);
document.newPage();
writer.setPageEmpty(true);
String[] paragraphs = we.getParagraphText();
for (int i = 0; i < paragraphs.length; i++) {
org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);
// CharacterRun run = pr.getCharacterRun(i);
// run.setBold(true);
// run.setCapitalized(true);
// run.setItalic(true);
paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");
System.out.println("Length:" + paragraphs[i].length());
System.out.println("Paragraph" + i + ": " + paragraphs[i].toString());
// add the paragraph to the document
document.add(new Paragraph(paragraphs[i]));
}
System.out.println("Document testing completed");
} catch (Exception e) {
System.out.println("Exception during test");
e.printStackTrace();
} finally {
// close the document
document.close();
}
}
hãy giúp tôi.
Thnx trước.