如何使用 Apache HWPF 将图像插入 Doc 文件

版本

我想用 Apache HWPF 将图片插入到 Doc 文件中,但我不知道该怎么做。请帮助我。

布拉克·阿基尔迪兹

以下代码用于.docx文件。我希望这有帮助。

public void addPicture(File img,double width,double height)
    {

        String imgPath = img.getPath();

        XWPFParagraph p = doc.createParagraph();
        p.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun r = p.createRun();

        try {
                int format = XWPFDocument.PICTURE_TYPE_JPEG;

                if(imgPath.endsWith(".emf")) format = XWPFDocument.PICTURE_TYPE_EMF;
                else if(imgPath.endsWith(".wmf")) format = XWPFDocument.PICTURE_TYPE_WMF;
                else if(imgPath.endsWith(".pict")) format = XWPFDocument.PICTURE_TYPE_PICT;
                else if(imgPath.endsWith(".jpeg") || imgPath.endsWith(".jpg")) format = XWPFDocument.PICTURE_TYPE_JPEG;
                else if(imgPath.endsWith(".png")) format = XWPFDocument.PICTURE_TYPE_PNG;
                else if(imgPath.endsWith(".dib")) format = XWPFDocument.PICTURE_TYPE_DIB;
                else if(imgPath.endsWith(".gif")) format = XWPFDocument.PICTURE_TYPE_GIF;
                else if(imgPath.endsWith(".tiff")) format = XWPFDocument.PICTURE_TYPE_TIFF;
                else if(imgPath.endsWith(".eps")) format = XWPFDocument.PICTURE_TYPE_EPS;
                else if(imgPath.endsWith(".bmp")) format = XWPFDocument.PICTURE_TYPE_BMP;
                else if(imgPath.endsWith(".wpg")) format = XWPFDocument.PICTURE_TYPE_WPG;
                else {
                    System.err.println("Unsupported picture: " + imgPath +
                            ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
                }

                double pageWidth = PAGE_WIDTH - PAGE_MARGIN_LEFT - PAGE_MARGIN_RIGHT;
                double pageHeight = PAGE_HEIGHT - PAGE_MARGIN_TOP;

                if(width > pageWidth)
                {
                    double difference =  width - pageWidth  ;
                    double differenceRatio = difference / width;
                    //System.out.println("Width is resizing. Start width : "+width+" pixel difference : "+difference+" difference ratio :"+differenceRatio);
                    width = (width - (width * differenceRatio));
                    height =  (height - (height * differenceRatio));
                }

                if(height > pageHeight)
                {
                    double difference = ( height - (pageHeight) ) ;
                    double differenceRatio = difference / height;
                    //System.out.println("Height is resizing. Start Height : "+height+" pixel difference : "+difference+" difference ratio :"+differenceRatio);
                    width = (width - (width * differenceRatio));
                    height =  (height - (height * differenceRatio));
                }

                //System.out.println("Width : "+width+" Height : "+height);

                XWPFPicture pic = r.addPicture(new FileInputStream(img), format, imgPath, Units.toEMU(width), Units.toEMU(height));

            } catch (InvalidFormatException ex) {
                Logger.getLogger(WordHandler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(WordHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Apache POI HWPF-将doc文件转换为pdf时出现问题

如何从Apache POI知道文件是.docx还是.doc格式

如何在 Apache POI HWPF 文档中添加换行符,例如 \\n

(如何)我可以使用Apache Tika在.DOC或.PDF或.JAVA等文件中搜索短语吗?

如何使用xlsxwriter将图像插入Excel文件?

使用Apache POI将图像插入列以使用Excel

如何将图像、Pdf、Docx 和 Doc 文件转换为位图

Java Apache POI读取Word(.doc)文件并获取使用的命名CHARACTER样式

使用Java中的Apache POI在Doc文件中的表单元格中输入文本

当php文件不在doc根目录下时如何使用图像生成php

如何将 .png 图像插入 TClientDataSet .cds 文件?

从Outlook将图像插入Word Doc

使用JXL将图像插入Excel文件而不拉伸它

如何读取Word .doc文件?

如何读取.doc或.docx文件

如何创建可以存储pdf,doc文件的表以及如何插入文件

如何使用python将txt文件或PDF转换为Word doc?

如何仅使用 php 将 .docx / .doc 文件转换为 .pdf

批处理文件:如何将所有.doc文件备份到文件夹

如何使用JAVA POI将.doc拆分为几个.doc?

如何使用C ++中的文件读写doc,pdf文件

如何使用Gradle将AAR文件发布到Apache Archiva

如何将文件(pdf,doc等...)下载到sdcard中,并在Android中使用Volley打开文件?

识别包含图像的.doc / .docx文件

VBA:Word将.docx文件另存为.doc文件,如何另存为docx?

使用Apache POI将数组列表中的数据插入Excel文件中的块中

使用itext将图像文件插入PDF时找不到文件

如何使用PHP或在Codeigniter中读取doc文件的内容

如何使用Python从.doc文件中检索纯文本?