Apache的POI图像比例图像 [英] Apache POI image scale image

查看:275
本文介绍了Apache的POI图像比例图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想插入图片的Word文档。低于code,将图像的原始大小:

Hello I want insert image to word document. Below code inserts the image in its original size:

private def writePhotosToDoc(model: WordReportModel, doc: XWPFDocument): Unit = {
    val paragraphIndex = detectVariable(Variables.PHOTO_APPLICATION, doc)

    if (!paragraphIndex.exists(_ == ("idx", -1))) {
        val taskParagraph = doc.getParagraphs.asScala(
            paragraphIndex("idx")
        )
        taskParagraph.removeRun(
            paragraphIndex("irx")
        )
        model.attachments.foreach{
            case(key, value) =>
                val p = doc.createParagraph()
                p.getCTP.setPPr(taskParagraph.getCTP.getPPr)
                p.setAlignment(ParagraphAlignment.CENTER)
                val r = p.createRun()
                r.addBreak()

                val bi = ImageIO.read(value.head)
                val width = bi.getWidth
                val height = bi.getHeight

                r.addPicture(
                    new FileInputStream(value.head),
                    Document.PICTURE_TYPE_PNG,
                    value.head.getName,
                    Units.toEMU(width),
                    Units.toEMU(height)
                )
        }
    }
}

如何插入图片,并设置规模。

How to insert image and set scale.

推荐答案

我看了<一个href=\"http://stackoverflow.com/questions/7674115/how-to-add-a-picture-to-a-docx-document-with-apache-poi-xwpf-in-java\">this线程,并试图用不同的大小,以插入和它似乎是工作的罚款。

I saw this thread and tried to insert with different sizes and it seems to be working fine.

这是执行的开始。请注意,您必须首先创建一个普通的docx文件,然后编辑和添加图像(否则该文件不正确创建):

This is where the execution starts. Note that you have to first create a plain docx file and then edit and add the image (otherwise the file is not created properly):

XWPFDocument doc = new XWPFDocument();
doc.write(new FileOutputStream(new File("test.docx")));

CustomXWPFDocument document = new CustomXWPFDocument(new FileInputStream(new File("test.docx")));
FileOutputStream fos = new FileOutputStream(new File("test.docx"));

String blipId = document.addPictureData(new FileInputStream(new File("res/Tulips.jpg")), Document.PICTURE_TYPE_JPEG);


System.out.println(document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG));

//System.out.println(document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG));
document.createPicture(blipId,document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG), 100, 150);


document.write(fos);
fos.flush();
fos.close();

这是在 CustomXWPFDocument 类的(从<一所href=\"http://stackoverflow.com/questions/7674115/how-to-add-a-picture-to-a-docx-document-with-apache-poi-xwpf-in-java\">question):

And this is the CustomXWPFDocument class (taken from the question):

public class CustomXWPFDocument extends XWPFDocument
{
    public CustomXWPFDocument(InputStream in) throws IOException
    {
        super(in);
    }

    public void createPicture(String blipId,int id, int width, int height)
    {
        final int EMU = 9525;
        width *= EMU;
        height *= EMU;
        //String blipId = getAllPictures().get(id).getPackageRelationship().getId();


        CTInline inline = createParagraph().createRun().getCTR().addNewDrawing().addNewInline();

        String picXml = "" +
                "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
                "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
                "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
                "         <pic:nvPicPr>" +
                "            <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>" +
                "            <pic:cNvPicPr/>" +
                "         </pic:nvPicPr>" +
                "         <pic:blipFill>" +
                "            <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
                "            <a:stretch>" +
                "               <a:fillRect/>" +
                "            </a:stretch>" +
                "         </pic:blipFill>" +
                "         <pic:spPr>" +
                "            <a:xfrm>" +
                "               <a:off x=\"0\" y=\"0\"/>" +
                "               <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" +
                "            </a:xfrm>" +
                "            <a:prstGeom prst=\"rect\">" +
                "               <a:avLst/>" +
                "            </a:prstGeom>" +
                "         </pic:spPr>" +
                "      </pic:pic>" +
                "   </a:graphicData>" +
                "</a:graphic>";

        //CTGraphicalObjectData graphicData = inline.addNewGraphic().addNewGraphicData();
        XmlToken xmlToken = null;
        try
        {
            xmlToken = XmlToken.Factory.parse(picXml);
        }
        catch(XmlException xe)
        {
            xe.printStackTrace();
        }
        inline.set(xmlToken);
        //graphicData.set(xmlToken);

        inline.setDistT(0);
        inline.setDistB(0);
        inline.setDistL(0);
        inline.setDistR(0);

        CTPositiveSize2D extent = inline.addNewExtent();
        extent.setCx(width);
        extent.setCy(height);

        CTNonVisualDrawingProps docPr = inline.addNewDocPr();
        docPr.setId(id);
        docPr.setName("Picture " + id);
        docPr.setDescr("Generated");
    }
}

这篇关于Apache的POI图像比例图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆