For example:
Slide size : 950 x 510
Image size : 500 x 4620
Here is my code
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());
I don’t know how to calculate the offset of the image, can make it centered.
Please give me some advice.
Here is the image:
enter image description here
Here is the effect I want:
enter image description here
2
Answers
The
CTRelativeRect
is named relative because it’s dimensions are in percentages of slide height and slide width.Your long image will be stretched or compressed to those percent dimensions. Also all offsets left, right, top and bottom are in percent of slide height and slide width.
As of your shown wanted effect, the image shall fill the full slide width. So picture width will be stretched from 500px to 950px. This is a ratio of 950/500. In same ratio the height also will be stretched.
Knowing this we need calculating the picture dimensions form pixel into percent of slide dimensions. Knowing this we then can calculating the top offset as (100% – picture height in %) / 2. If the same bottom offset is set, then the exact middle of your long picture will be shown in slide background. If the top offset is (100% – picture height in %) / 2) – 100% and bottom offset is (100% – picture height in %) / 2 + 100%, then slide background shows 1 slide height below middle of long picture.
Additional to this
Microsoft
always brings it’s own strange measurement units into account. Because of avoiding floating point numbers for percents the measurement unit is thousandth percent here.Example:
The second slide shows the middle of your long picture, as stated as your wanted effect.
Hint:
The whole effect can only be viewed using
PowerPoint
.Impress
is not able showing that effect. AndPowerPoint 2007
can show the effect using offsets lower than 0% and/or greater than 100% but is not able setting offsets lower than 0% and/or greater than 100%.