简体   繁体   中英

How to copy the table from word document to pdf document in java?

** Code I am working on to get the text and images getting copied in the PDF but tables are not getting copied from word document Here i am fetching the text and images first from the word document using apache poi and then i want to write the tables from the word document to pdf document.

  1. Function considering the page size to be A4 as a standard one

Have a look at convertWordToPdf function in the below code .

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();
        }
  • Get the tables from the word document and use the Itext API to write them back

    List tablesList = doc.getTables();
    pdftable = new PdfPTable(3) ; // Table List Move for (XWPFTable xwpfTable : tablesList) { pdftable = new PdfPTable(xwpfTable.getRow(0).getTableCells().size()) ; List row = xwpfTable.getRows(); for (XWPFTableRow xwpfTableRow : row) { List cell = xwpfTableRow.getTableCells(); for (XWPFTableCell xwpfTableCell : cell) { if(xwpfTableCell!=null) { //table = new Table(cell.size()); pdftable.addCell(xwpfTableCell.getText()); } } } pdfdoc.add(pdftable);

     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM