【问题标题】:Not able to append word document when I am inserting multiple image into word using java当我使用java将多个图像插入word时无法附加word文档
【发布时间】:2016-09-20 20:54:21
【问题描述】:

我使用以下代码拍摄屏幕快照并保存到 word 文件中。 此代码用作按钮.. 但是当我获取屏幕截图时,它会替换现有文件.. 我怎样才能附加这个..请建议 提前致谢。 :)

private void btnmanActionPerformed(java.awt.event.ActionEvent evt) {
    setVisible(false);

    if (btnman.getText().equals("Quick ScreenShot")) {

        try {
            Calendar now = Calendar.getInstance();
            Robot robot = new Robot();
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle captureRect = new Rectangle(0, 0, 1430, 840);
            BufferedImage screenFullImage = robot.createScreenCapture(captureRect);

            ImageIO.write(screenFullImage, "JPG", new File(path.getText() + formatter.format(now.getTime()) + ".jpg"));
            img = path.getText() + formatter.format(now.getTime()) + ".jpg";
            System.out.println(img);
            setVisible(true);
        } catch (Exception es) {
            System.out.println("not done");
        }
    } else {

        try {
            Calendar now = Calendar.getInstance();
            Robot robot = new Robot();
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle captureRect = new Rectangle(0, 0, 1400, 850);
            BufferedImage screenFullImage = robot.createScreenCapture(captureRect);

            ImageIO.write(screenFullImage, "JPG", new File("E:\\" + formatter.format(now.getTime()) + ".jpg"));
            img = "E:\\" + formatter.format(now.getTime()) + ".jpg";
            System.out.println(img);
            setVisible(true);
        } catch (Exception es) {
            System.out.println("not done");
        }


        XWPFDocument doc = new XWPFDocument();

        XWPFParagraph title = doc.createParagraph();
        XWPFRun run = title.createRun();
        run.setText(path.getText());
        run.setBold(true);
        title.setAlignment(ParagraphAlignment.CENTER);
        try {
            is = new FileInputStream(img);

            //run.addBreak();
            run.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, img, Units.toEMU(500), Units.toEMU(600));

            //is.close();
            System.out.println(img);
            System.out.print(path.getText());

            FileOutputStream fos = new FileOutputStream(path.getText());


            doc.write(fos);
            fos.close();
            System.out.println("Enterd successfully");

        } catch (Exception es) {
            System.err.println("es");
        }


    }
}

【问题讨论】:

    标签: java apache-poi


    【解决方案1】:

    它会覆盖文件而不是追加,因为您每次都在创建一个新的(空的)XWPFDocument

    XWPFDocument doc = new XWPFDocument();
    

    并使用相同的(我假设的)路径保存它。

    FileOutputStream fos = new FileOutputStream(path.getText());
    doc.write(fos);
    

    所以它最终替换了文件。

    尝试改用:

    XWPFDocument doc = new XWPFDocument(new FileInputStream(path.getText()));
    

    如果之前的文档已经存在,则打开它。

    【讨论】:

    • 非常感谢 Alex,它非常需要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多