ASF Bugzilla – Attachment 34591 Details for
Bug 60552
Corrupt slideshow after importing a picture from another slideshow
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
PFA java code
PPTTest.java (text/plain), 6.41 KB, created by
Anand
on 2017-01-05 04:31:03 UTC
(
hide
)
Description:
PFA java code
Filename:
MIME Type:
Creator:
Anand
Created:
2017-01-05 04:31:03 UTC
Size:
6.41 KB
patch
obsolete
>package com.burando.test; > >import java.awt.Color; >import java.awt.Graphics2D; >import java.awt.geom.Rectangle2D; >import java.awt.image.BufferedImage; >import java.io.ByteArrayInputStream; >import java.io.ByteArrayOutputStream; >import java.io.File; >import java.io.FileInputStream; >import java.io.FileOutputStream; >import java.io.InputStream; >import java.util.ArrayList; >import java.util.HashMap; >import java.util.HashSet; >import java.util.Iterator; >import java.util.List; >import java.util.Map; >import java.util.Set; > >import javax.imageio.ImageIO; > >import org.apache.poi.sl.usermodel.PictureData.PictureType; >import org.apache.poi.ss.usermodel.PictureData; >import org.apache.poi.util.IOUtils; >import org.apache.poi.xslf.usermodel.SlideLayout; >import org.apache.poi.xslf.usermodel.XMLSlideShow; >import org.apache.poi.xslf.usermodel.XSLFPictureData; >import org.apache.poi.xslf.usermodel.XSLFPictureShape; >import org.apache.poi.xslf.usermodel.XSLFShape; >import org.apache.poi.xslf.usermodel.XSLFSlide; >import org.apache.poi.xslf.usermodel.XSLFSlideLayout; >import org.apache.poi.xslf.usermodel.XSLFSlideMaster; >import org.apache.poi.xslf.usermodel.XSLFTextBox; >import org.apache.poi.xslf.usermodel.XSLFTextShape; > >public class PPTTest { > > private static final String SRC_PPT_FN = "source.pptx"; > private static final String DST_PPT_FN = "dest.pptx"; > private static final String CV_PIC_FN = "closeview.jpeg"; > private static final String LV_PIC_FN = "longview.jpeg"; > > private static final String LO_NAME = "CloseLong"; > private static final String CV_PH_NAME = "CloseView"; > private static final String LV_PH_NAME = "LongView"; > private static final String LOC_PH_NAME = "Location"; > > public static void main(int argc, String[] argv) { > String root = System.getenv("PPT_TEST"); > String srcPPTFN = root + SRC_PPT_FN; > String cvPicFN = root + CV_PIC_FN; > String lvPicFN = root + LV_PIC_FN; > String locTxt = "This is where the site is"; > > try { > FileInputStream spptIS = new FileInputStream(srcPPTFN); > FileInputStream cvPicIS = new FileInputStream(cvPicFN); > FileInputStream lvPicIS = new FileInputStream(lvPicFN); > byte[] cvPic = IOUtils.toByteArray(cvPicIS); > cvPicIS.close(); > byte[] lvPic = IOUtils.toByteArray(lvPicIS); > lvPicIS.close(); > createPPT(spptIS, cvPic, lvPic, locTxt, DST_PPT_FN); > } catch (Exception ex) { > ex.printStackTrace(); > } > } > > private static XSLFSlide getSrcSlide(XMLSlideShow ppt) { > XSLFSlide slide = null; > for (XSLFSlide srcSlide : ppt.getSlides()) { > if(LO_NAME.equalsIgnoreCase(srcSlide.getSlideLayout().getName())) { > slide = srcSlide; > break; > } > } > return slide; > } > > > private static byte[] resizeImage(byte[] inPic, double img_width, double img_height) { > try { > ByteArrayInputStream in = new ByteArrayInputStream(inPic); > BufferedImage originalImage = ImageIO.read(in); > int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType(); > > if (img_height == 0) { > img_height = originalImage.getHeight(); > } > > BufferedImage resizedImage = new BufferedImage((int) img_width, (int) img_height, type); > Graphics2D g = resizedImage.createGraphics(); > g.drawImage(originalImage, 0, 0, (int) img_width, (int) img_height, null); > g.dispose(); > > ByteArrayOutputStream buffer = new ByteArrayOutputStream(); > ImageIO.write(resizedImage, "jpeg", buffer); > return buffer.toByteArray(); > } catch (Exception ex) { > ex.printStackTrace(); > } > return null; > } > > private static XSLFShape getShape(XSLFSlide slide, String shapeName) { > XSLFShape shape = null; > Iterator<XSLFShape> sit = slide.getSlideLayout().getShapes().iterator(); > while (sit.hasNext()) { > XSLFShape lshape = sit.next(); > if (lshape != null && lshape.getShapeName().equalsIgnoreCase(shapeName)) { > shape = lshape; > break; > } > } > return shape; > } > > private static XSLFShape getPlaceholder(XSLFSlide slide, String txtShapeName) { > XSLFShape shape = null; > int idx = 0; > try { > for (; idx < 100; idx++) { > String name = slide.getPlaceholder(idx).getShapeName(); > if (name.equalsIgnoreCase(txtShapeName)) { > shape = slide.getPlaceholder(idx); > break; > } > } > } catch (IndexOutOfBoundsException ex) { > ex.printStackTrace(); > } > return shape; > } > > private static void setText(XSLFSlide slide, String txtShapeName, String txtToSet) { > XSLFShape shape = getPlaceholder(slide, txtShapeName); > if (shape != null && shape instanceof XSLFTextShape) { > XSLFTextShape tshape = (XSLFTextShape) shape; > tshape.clearText(); > tshape.setText(txtToSet); > } > } > > private static void setPic(XMLSlideShow ppt, XSLFSlide slide, byte[] pic, String shapeName) { > XSLFShape shape = getShape(slide, shapeName); > > Rectangle2D anchor = shape.getAnchor(); > byte[] ri = resizeImage(pic, anchor.getWidth(), anchor.getHeight()); > XSLFPictureData pd = ppt.addPicture(ri, PictureType.JPEG); > XSLFPictureShape pics = slide.createPicture(pd); > slide.removeShape(shape); > pics.setAnchor(anchor); > } > > public static XSLFSlide createSlide(XMLSlideShow ppt, > XSLFSlide srcSlide, Set<XSLFSlideLayout> visitedLOs) { > XSLFSlide slide = ppt.createSlide(); > XSLFSlideLayout srcLayout = srcSlide.getSlideLayout(); > if (!visitedLOs.contains(srcLayout)) { > visitedLOs.add(srcLayout); > XSLFSlideLayout dstLayout = slide.getSlideLayout(); > dstLayout.importContent(srcLayout); > } > slide.importContent(srcSlide); > return slide; > } > > private static void createPPT(InputStream spptIS, byte[] cvPic, byte[] lvPic, String locTxt, String destPPTFN) { > try { > Set<XSLFSlideLayout> visitedLOs = new HashSet<XSLFSlideLayout>(); > XMLSlideShow srcPPT = new XMLSlideShow(spptIS); > XSLFSlide srcSlide = getSrcSlide(srcPPT); > > XMLSlideShow destPPT = new XMLSlideShow(); > > XSLFSlide slide1 = createSlide(destPPT, srcSlide, visitedLOs); > setPic(destPPT, slide1, cvPic, CV_PH_NAME); > setPic(destPPT, slide1, lvPic, LV_PH_NAME); > setText(slide1, LOC_PH_NAME, locTxt); > > XSLFSlide slide2 = createSlide(destPPT, srcSlide, visitedLOs); > setPic(destPPT, slide2, cvPic, CV_PH_NAME); > setPic(destPPT, slide2, lvPic, LV_PH_NAME); > setText(slide1, LOC_PH_NAME, locTxt); > > FileOutputStream fos = new FileOutputStream(System.getenv("PPT_TEST") + destPPTFN); > destPPT.write(fos); > fos.close(); > } catch (Exception ex) { > ex.printStackTrace(); > } > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 60552
:
34590
| 34591 |
34592
|
36089