Java 如何使用ApachePOI将背景图像设置为中心
例如: 幻灯片尺寸:950 x 510 图像大小:500 x 4620 这是我的密码Java 如何使用ApachePOI将背景图像设置为中心,java,image,apache,apache-poi,Java,Image,Apache,Apache Poi,例如: 幻灯片尺寸:950 x 510 图像大小:500 x 4620 这是我的密码 XSLFPictureData idx = ppt.addPicture(file, pictureType); CTBackgroundProperties bgPr = this.slide.getXmlObject().getCSld().addNewBg().addNewBgPr(); CTBlipFillProperties blipPr = bgPr.addNewBlipFill(); CTBlip
XSLFPictureData idx = ppt.addPicture(file, pictureType);
CTBackgroundProperties bgPr = this.slide.getXmlObject().getCSld().addNewBg().addNewBgPr();
CTBlipFillProperties blipPr = bgPr.addNewBlipFill();
CTBlip blib = blipPr.addNewBlip();
CTRelativeRect ctRelativeRect = blipPr.addNewStretch().addNewFillRect();
double imgHeight = idx.getImageDimensionInPixels().getHeight();
double imgWidth = idx.getImageDimensionInPixels().getWidth();
double pptHeight = ppt.getPageSize().getHeight();
double pptWidth = ppt.getPageSize().getWidth();
if (pptHeight - imgHeight < 0) {
//How to calculate the offset above and below the image
ctRelativeRect.setT(?);
ctRelativeRect.setB(?);
}else if(pptWidth - imgWidth < 0) {
//
ctRelativeRect.setR(?);
ctRelativeRect.setL(?);
}
RelationPart rp = slide.addRelation(null, XSLFRelation.IMAGES, idx);
blib.setEmbed(rp.getRelationship().getId());
XSLFPictureData idx=ppt.addPicture(文件,pictureType);
CTBackgroundProperties bgPr=this.slide.getXmlObject().getCSld().addNewBg().addNewBgPr();
CTBlipFillProperties bliprp=bgPr.addNewBlipFill();
CTBlip blib=bliprp.addNewBlip();
CtRelativect CtRelativect=bliprp.addNewStretch().addNewFillRect();
double imgHeight=idx.getImageDimensionInPixels().getHeight();
double imgWidth=idx.getImageDimensionInPixels().getWidth();
double-pptHeight=ppt.getPageSize().getHeight();
double-pptWidth=ppt.getPageSize().getWidth();
如果(pptHeight-Imghight<0){
//如何计算图像上方和下方的偏移量
Ctrelativect.setT(?);
相对立根(?);
}else if(pptWidth-imgWidth<0){
//
Ctrelativect.setR(?);
Ctrelativect.setL(?);
}
RelationPart rp=slide.addRelation(null,XSLFRelation.IMAGES,idx);
setEmbed(rp.getRelationship().getId());
我不知道如何计算图像的偏移量,可以使其居中。
请给我一些建议
图为:
以下是我想要的效果:
if(灯光-灯光<0){
//如何计算图像上方和下方的偏移量
//而不是双高度偏移=(pptHeight-imgHeight)/2
双高度偏移=(imghight-pptHeight)/2
Ctrelativect.setT(高度偏移);
Ctrelativect.setB(高度偏移);
}else if(pptWidth-imgWidth<0){
双宽度偏移=(pptWidth-imgWidth)/2
Ctrelativect.setR(宽度偏移);
Ctrelativect.setL(宽度偏移);
}
Ctreativect被命名为相对,因为它的尺寸是以滑动高度和滑动宽度的百分比表示的。
您的长图像将被拉伸或压缩到这些维度。此外,所有左侧、右侧、顶部和底部的偏移量均以滑块高度和滑块宽度的百分比表示
从您想要的效果开始,图像将填满整个幻灯片宽度。因此,图片宽度将从500px扩展到950px。这是950/500的比率。在相同的比例下,高度也将被拉伸
要知道这一点,我们需要将图片尺寸从像素计算为幻灯片尺寸的百分比。知道了这一点,我们就可以将顶部偏移计算为(100%-图片高度%)/2。如果设置了相同的底部偏移,则长图片的正中间将显示在幻灯片背景中。如果顶部偏移量为(100%-图片高度单位%)/2)-100%,底部偏移量为(100%-图片高度单位%)/2+100%,则幻灯片背景显示长图片中间下方的1个幻灯片高度
除此之外,Microsoft总是将自己奇怪的测量单位考虑在内。由于避免了百分比的浮点数,这里的测量单位是千分之一
例如:
import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.*;
import org.openxmlformats.schemas.presentationml.x2006.main.*;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
import java.awt.Dimension;
public class CreatePPTXSheetsBackgroundPicture {
public static void main(String[] args) throws Exception {
XMLSlideShow slideShow = new XMLSlideShow();
XSLFPictureData pictureData = slideShow.addPicture(new FileInputStream("2hGsR.jpg"), PictureData.PictureType.JPEG);
slideShow.setPageSize(new Dimension(950, 510));
double imgHeight = pictureData.getImageDimensionInPixels().getHeight();
double imgWidth = pictureData.getImageDimensionInPixels().getWidth();
double sildeHeight = slideShow.getPageSize().getHeight();
double slideWidth = slideShow.getPageSize().getWidth();
//How to calculate the offset above and below the image
// imgWidth stretched to slideWidth => ratio = slideWidth / imgWidth
double ratio = slideWidth / imgWidth;
// sildeHeight% = 100%
// imgHeight% = (imgHeight * ratio) * 100% / sildeHeight
double imgHeightPerCent = (imgHeight * ratio) * 100 / sildeHeight;
// topMiddle% = (100% - imgHeight%) / 2
// bottomMiddle% = (100% - imgHeight%) / 2
// topMiddle+1slideHeight% = (100% - imgHeight%) / 2) - 100%
// bottomMiddle+1slideHeight% = (100% - imgHeight%) / 2 + 100%
// topMiddle-1slideHeight% = (100% - imgHeight%) / 2) + 100%
// bottomMiddle-1slideHeight% = (100% - imgHeight%) / 2 - 100%
// first slide
XSLFSlide slide = slideShow.createSlide();
CTBackgroundProperties backgroundProperties = slide.getXmlObject().getCSld().addNewBg().addNewBgPr();
CTBlipFillProperties blipFillProperties = backgroundProperties.addNewBlipFill();
CTRelativeRect ctRelativeRect = blipFillProperties.addNewStretch().addNewFillRect();
// first slide shows 1 slide above middle of long picture
// measurement unit is thousandth => percent * 1000
int top = (int)Math.round(((100 - imgHeightPerCent) / 2 + 100) * 1000);
int bottom = (int)Math.round(((100 - imgHeightPerCent) / 2 -100) * 1000);
ctRelativeRect.setT(top);
ctRelativeRect.setB(bottom);
String idx = slide.addRelation(null, XSLFRelation.IMAGES, pictureData).getRelationship().getId();
CTBlip blib = blipFillProperties.addNewBlip();
blib.setEmbed(idx);
// second slide
slide = slideShow.createSlide();
backgroundProperties = slide.getXmlObject().getCSld().addNewBg().addNewBgPr();
blipFillProperties = backgroundProperties.addNewBlipFill();
ctRelativeRect = blipFillProperties.addNewStretch().addNewFillRect();
// second slide shows middle of long picture
top = (int)Math.round(((100 - imgHeightPerCent) / 2) * 1000);
bottom = (int)Math.round(((100 - imgHeightPerCent) / 2) * 1000);
ctRelativeRect.setT(top);
ctRelativeRect.setB(bottom);
idx = slide.addRelation(null, XSLFRelation.IMAGES, pictureData).getRelationship().getId();
blib = blipFillProperties.addNewBlip();
blib.setEmbed(idx);
// third slide
slide = slideShow.createSlide();
backgroundProperties = slide.getXmlObject().getCSld().addNewBg().addNewBgPr();
blipFillProperties = backgroundProperties.addNewBlipFill();
ctRelativeRect = blipFillProperties.addNewStretch().addNewFillRect();
// third slide shows 1 slide below middle of long picture
top = (int)Math.round(((100 - imgHeightPerCent) / 2 - 100) * 1000);
bottom = (int)Math.round(((100 - imgHeightPerCent) / 2 + 100) * 1000);
ctRelativeRect.setT(top);
ctRelativeRect.setB(bottom);
idx = slide.addRelation(null, XSLFRelation.IMAGES, pictureData).getRelationship().getId();
blib = blipFillProperties.addNewBlip();
blib.setEmbed(idx);
FileOutputStream out = new FileOutputStream("CreatePPTXSheetsBackgroundPicture.pptx");
slideShow.write(out);
out.close();
}
}
第二张幻灯片显示了您的长图片的中间部分,正如您想要的效果所述
提示:
整个效果只能使用PowerPoint查看<代码>印象无法显示该效果。而且
PowerPoint 2007
可以使用小于0%和/或大于100%的偏移量显示效果,但无法设置小于0%和/或大于100%的偏移量。我尝试了很多算法,包括你的答案,但都无法达到我想要的效果。无论如何,谢谢。啊,我明白了,您只需在那里显示图像的中心即可!。然后它将是下一个:宽度应该像以前一样工作。至于高度,你应该把所有的东西都抵消掉,除了灯光。所以它就像|双高度偏移=(imghight-pptHeight)/2 CtrRelativect.setT(高度偏移)Ctrativect.setB(高度偏移);哈哈,我又见到你了。再次解决了我的问题,谢谢你的回答和提示。
import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.*;
import org.openxmlformats.schemas.presentationml.x2006.main.*;
import org.openxmlformats.schemas.drawingml.x2006.main.*;
import java.awt.Dimension;
public class CreatePPTXSheetsBackgroundPicture {
public static void main(String[] args) throws Exception {
XMLSlideShow slideShow = new XMLSlideShow();
XSLFPictureData pictureData = slideShow.addPicture(new FileInputStream("2hGsR.jpg"), PictureData.PictureType.JPEG);
slideShow.setPageSize(new Dimension(950, 510));
double imgHeight = pictureData.getImageDimensionInPixels().getHeight();
double imgWidth = pictureData.getImageDimensionInPixels().getWidth();
double sildeHeight = slideShow.getPageSize().getHeight();
double slideWidth = slideShow.getPageSize().getWidth();
//How to calculate the offset above and below the image
// imgWidth stretched to slideWidth => ratio = slideWidth / imgWidth
double ratio = slideWidth / imgWidth;
// sildeHeight% = 100%
// imgHeight% = (imgHeight * ratio) * 100% / sildeHeight
double imgHeightPerCent = (imgHeight * ratio) * 100 / sildeHeight;
// topMiddle% = (100% - imgHeight%) / 2
// bottomMiddle% = (100% - imgHeight%) / 2
// topMiddle+1slideHeight% = (100% - imgHeight%) / 2) - 100%
// bottomMiddle+1slideHeight% = (100% - imgHeight%) / 2 + 100%
// topMiddle-1slideHeight% = (100% - imgHeight%) / 2) + 100%
// bottomMiddle-1slideHeight% = (100% - imgHeight%) / 2 - 100%
// first slide
XSLFSlide slide = slideShow.createSlide();
CTBackgroundProperties backgroundProperties = slide.getXmlObject().getCSld().addNewBg().addNewBgPr();
CTBlipFillProperties blipFillProperties = backgroundProperties.addNewBlipFill();
CTRelativeRect ctRelativeRect = blipFillProperties.addNewStretch().addNewFillRect();
// first slide shows 1 slide above middle of long picture
// measurement unit is thousandth => percent * 1000
int top = (int)Math.round(((100 - imgHeightPerCent) / 2 + 100) * 1000);
int bottom = (int)Math.round(((100 - imgHeightPerCent) / 2 -100) * 1000);
ctRelativeRect.setT(top);
ctRelativeRect.setB(bottom);
String idx = slide.addRelation(null, XSLFRelation.IMAGES, pictureData).getRelationship().getId();
CTBlip blib = blipFillProperties.addNewBlip();
blib.setEmbed(idx);
// second slide
slide = slideShow.createSlide();
backgroundProperties = slide.getXmlObject().getCSld().addNewBg().addNewBgPr();
blipFillProperties = backgroundProperties.addNewBlipFill();
ctRelativeRect = blipFillProperties.addNewStretch().addNewFillRect();
// second slide shows middle of long picture
top = (int)Math.round(((100 - imgHeightPerCent) / 2) * 1000);
bottom = (int)Math.round(((100 - imgHeightPerCent) / 2) * 1000);
ctRelativeRect.setT(top);
ctRelativeRect.setB(bottom);
idx = slide.addRelation(null, XSLFRelation.IMAGES, pictureData).getRelationship().getId();
blib = blipFillProperties.addNewBlip();
blib.setEmbed(idx);
// third slide
slide = slideShow.createSlide();
backgroundProperties = slide.getXmlObject().getCSld().addNewBg().addNewBgPr();
blipFillProperties = backgroundProperties.addNewBlipFill();
ctRelativeRect = blipFillProperties.addNewStretch().addNewFillRect();
// third slide shows 1 slide below middle of long picture
top = (int)Math.round(((100 - imgHeightPerCent) / 2 - 100) * 1000);
bottom = (int)Math.round(((100 - imgHeightPerCent) / 2 + 100) * 1000);
ctRelativeRect.setT(top);
ctRelativeRect.setB(bottom);
idx = slide.addRelation(null, XSLFRelation.IMAGES, pictureData).getRelationship().getId();
blib = blipFillProperties.addNewBlip();
blib.setEmbed(idx);
FileOutputStream out = new FileOutputStream("CreatePPTXSheetsBackgroundPicture.pptx");
slideShow.write(out);
out.close();
}
}