如何将table从word文档复制到java中的pdf文档?
How to copy the table from word document to pdf document in java?
**我正在处理的代码是将文本和图像复制到 PDF 中,但表格未从 word 文档中复制
在这里,我首先使用 apache poi 从 word 文档中获取文本和图像,然后我想将 word 文档中的表格写入 pdf 文档。
- 功能考虑页面尺寸为A4为标准
看看下面代码中的 convertWordToPdf 函数。
public static void convertWordToPdf(final String src, final String desc) {
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();
List<XWPFTable> tabList = doc.getTables();
final Iterator<XWPFTable> tabIterator = tabList.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();
}
从word文档中获取table并使用ItextAPI写回
List tablesList = doc.getTables();
pdftable = 新 PdfPTable(3) ; // Table 列表移动
对于 (XWPFTable xwpfTable : tablesList) {
pdftable = new PdfPTable(xwpfTable.getRow(0).getTableCells().size()) ;
列表行 = xwpfTable.getRows();
对于(XWPFTable行 xwpfTable行:行){
列表单元格 = xwpfTableRow.getTableCells();
对于 (XWPFTableCell xwpfTableCell : cell) {
如果(xwpfTable单元格!=null)
{
//table = new Table(cell.size());
pdftable.addCell(xwpfTableCell.getText());
}
}
}
pdfdoc.add(pdftable);
}
**我正在处理的代码是将文本和图像复制到 PDF 中,但表格未从 word 文档中复制 在这里,我首先使用 apache poi 从 word 文档中获取文本和图像,然后我想将 word 文档中的表格写入 pdf 文档。
- 功能考虑页面尺寸为A4为标准
看看下面代码中的 convertWordToPdf 函数。
public static void convertWordToPdf(final String src, final String desc) {
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();
List<XWPFTable> tabList = doc.getTables();
final Iterator<XWPFTable> tabIterator = tabList.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();
}
从word文档中获取table并使用ItextAPI写回
List tablesList = doc.getTables();
pdftable = 新 PdfPTable(3) ; // Table 列表移动 对于 (XWPFTable xwpfTable : tablesList) { pdftable = new PdfPTable(xwpfTable.getRow(0).getTableCells().size()) ; 列表行 = xwpfTable.getRows(); 对于(XWPFTable行 xwpfTable行:行){ 列表单元格 = xwpfTableRow.getTableCells(); 对于 (XWPFTableCell xwpfTableCell : cell) { 如果(xwpfTable单元格!=null) { //table = new Table(cell.size()); pdftable.addCell(xwpfTableCell.getText()); } } } pdfdoc.add(pdftable);}