apache poi图像缩放图像

qpgpyjmq  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(545)

你好,我想在word文档中插入图像。下面的代码以原始大小插入图像:

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)
                )
        }
    }
}

如何插入图像和设置比例。

yr9zkbsy

yr9zkbsy1#

我看到这个线程,并试图插入不同的大小,它似乎是工作良好。
这就是执行的开始。请注意,您必须首先创建一个普通的docx文件,然后编辑并添加图像(否则无法正确创建文件):

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类(取自问题):

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");
    }
}

相关问题