要求是将上传的文件(doc,docx,jpg,png和pdf)转换为PDF,然后将其存储在database.i中将jpg / png / pdf转换为PDF没有问题。尽管我能够从doc和docx生成pdf,如果嵌入的图片大小不是这样的输入文件,字体,下划线,表格不起作用,则对象不合适。 请帮助我获得正确的嵌入图片(有时我得到一半图像),下划线和表格以PDF格式正确编写。
通过使用下面的JARS,我在网上看到了几行编码(它完成了完整的端到端转换)但是当我们不允许使用时。
org.apache.poi.xwpf.converter.core-1.0.6.jar
org.apache.poi.xwpf.converter.pdf-1.0.6.jar
fr.opensagres.xdocreport.itext.extension-2.0.0.jar
使用的罐子:
iText 2.1.7
poi:3.12
ooxml:3.12
ooxml-schemas:3.12
scratchpad:3.12
xmlbeans:2.3.0
有人可以帮助我解决上述问题,并附上所附的代码。public class WordToPdfConverter {
public static void main(final String[] args) {
final String worddocLocation = "C:\\Users\\VBUVANES\\Desktop\\Word example with pasted images.docx";
final String pdfLocation = "C:\\Users\\VBUVANES\\Desktop\\GeneratePDF\\testdocxtopdf_result4.pdf";
final String type = "docx";
convertWordToPdf(worddocLocation, pdfLocation, type);
}
public static void convertWordToPdf(final String src, final String desc, final String fileType) {
try {
final FileInputStream fs = new FileInputStream(src);
final XWPFDocument doc = new XWPFDocument(fs);
final Document pdfdoc = new Document(PageSize.A4, 72, 72, 72, 72);
final PdfWriter pwriter = PdfWriter.getInstance(pdfdoc,
new FileOutputStream(desc));
pwriter.setInitialLeading(20);
final List<XWPFParagraph> plist = doc.getParagraphs();
pdfdoc.open();
for (int i = 0; i < plist.size(); i++) {
final XWPFParagraph pa = (XWPFParagraph)plist.get(i);
final List<XWPFRun> runs = pa.getRuns();
for (int j = 0; j < runs.size(); j++) {
final XWPFRun run = (XWPFRun) runs.get(j);
final List<XWPFPicture> piclist = run.getEmbeddedPictures();
final Iterator<XWPFPicture> iterator = piclist.iterator();
while (iterator.hasNext()) {
final XWPFPicture pic = (XWPFPicture) iterator.next();
final XWPFPictureData picdata = pic.getPictureData();
final byte[] bytepic = picdata.getData();
final Image imag = Image.getInstance(bytepic);
imag.scaleAbsoluteHeight(40);
imag.scaleAbsoluteWidth((imag.getWidth() * 30) / imag.getHeight());
pdfdoc.add(imag);
}
final int color = getCode(run.getColor());
Font f = null;
if (run.isBold() && run.isItalic())
f = FontFactory.getFont(FontFactory.TIMES_ROMAN,
run.getFontSize(), Font.BOLDITALIC,
new Color(color));
else if (run.isBold())
f = FontFactory
.getFont(FontFactory.TIMES_ROMAN,
run.getFontSize(), Font.BOLD,
new Color(color));
else if (run.isItalic())
f = FontFactory.getFont(FontFactory.TIMES_ROMAN, run
.getFontSize(), Font.ITALIC, new Color(
color));
else if (run.isStrike())
f = FontFactory.getFont(FontFactory.TIMES_ROMAN,
run.getFontSize(), Font.STRIKETHRU,
new Color(color));
else
f = FontFactory.getFont(FontFactory.TIMES_ROMAN, run
.getFontSize(), Font.NORMAL, new Color(
color));
final String text = run.getText(-1);
byte[] bs;
if (text != null) {
bs = text.getBytes();
final String str = new String(bs, "UTF-8");
final Chunk chObj1 = new Chunk(str, f);
pdfdoc.add(chObj1);
}
}
pdfdoc.add(new Chunk(Chunk.NEWLINE));
}
pdfdoc.close();
} catch (final Exception e) {
e.printStackTrace();
}
}
public static int getCode(final String code) {
int colorCode;
if (code != null)
colorCode = Long.decode("0x" + code).intValue();
else
colorCode = Long.decode("0x000000").intValue();
return colorCode;
}
}