Docx4j:插入椭圆对象时PPTX被破坏了

时间:2014-03-06 10:43:38

标签: java xml powerpoint ellipse docx4j

我一整天都在寻找解决方案,但我找不到一个。这就是我决定问一个问题的原因。

我的问题是我正在向PPTX文件中插入一个椭圆对象。当我试图打开演示文稿MS OFfice说它已损坏并试图修复。它最终会删除幻灯片并显示主幻灯片。

见下面我的代码:
函数officeXReport.getTemplatename()包含PPTX的文件名(例如* Status_Report_template.pptx *)
模板pptx文件只包含1张幻灯片 函数officeXReport.getNewfilename()包含PPTX的新文件名(例如* Status_Report_2014-03-16.pptx *)

try {
    PresentationMLPackage presentationMLPackage = (PresentationMLPackage)OpcPackage.load(new java.io.File(officeXReport.getTemplatename()));
    MainPresentationPart pp = presentationMLPackage.getMainPresentationPart();                  
    boolean noLine = false;                
    STShapeType st = STShapeType.ELLIPSE;
    int i = 1; 

    SlidePart slidePart = (SlidePart)presentationMLPackage.getParts().getParts().get(pp.getSlide(0).getPartName());

    Shape sample = ((Shape)XmlUtils.unmarshalString(
                    getPresetShape(st.value(), noLine, Long.toString(1839580), Long.toString(1314971), Long.toString(184337), Long.toString(184338), "92D050"), Context.jcPML) );
    slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample);

    presentationMLPackage.save(new java.io.File(officeXReport.getNewfilename()));
} catch (Exception ex) {
    System.out.println("Exception thrown = " + ex.getMessage());
    return null;
}

相关函数getPresetShape()

private static String getPresetShape(String preset, boolean noLine, String x, String y, String cx, String cy, String colorcode) {
String txBody = "";
String ln = "";
String style = "";

if (!noLine) {
    ln = "<a:ln w=\"3175\">"
            +"<a:solidFill>"
            +"<a:srgbClr val=\"000000\"/>"
            +"</a:solidFill>"
        +"</a:ln>";

    style = "<p:style>\n" +
        "<a:lnRef idx=\"1\">\n" +
        "<a:schemeClr val=\"accent1\"/>\n" +
        "</a:lnRef>\n" +
        "<a:fillRef idx=\"3\">\n" +
        "<a:schemeClr val=\"accent1\"/>\n" +
        "</a:fillRef>\n" +
        "<a:effectRef idx=\"2\">\n" +
        "<a:schemeClr val=\"accent1\"/>\n" +
        "</a:effectRef>\n" +
        "<a:fontRef idx=\"minor\">\n" +
        "<a:schemeClr val=\"lt1\"/>\n" +
        "</a:fontRef>\n" +
        "</p:style>";

    txBody = "<p:txBody>\n" +
        "<a:bodyPr rtlCol=\"false\" anchor=\"ctr\"/>\n" +
        "<a:lstStyle/>\n" +
        "<a:p>\n" +
        "<a:pPr algn=\"ctr\"/>\n" +
        "<a:endParaRPr lang=\"en-US\" sz=\"1100\" u=\"sng\" dirty=\"false\">\n" +
        "<a:latin typeface=\"Futura Com Light\" panose=\"020B0402020204020303\" pitchFamily=\"34\" charset=\"0\"/>\n" +
        "</a:endParaRPr>\n" +
        "</a:p>\n" +
        "</p:txBody>";
}

return 
       "<p:sp   xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\">"
           + "<p:nvSpPr>"
                   + "<p:cNvPr id=\"32\" name=\"Ellipse 2\" />"
                   + "<p:cNvSpPr/>" 

                   + "<p:nvPr/>"

           + "</p:nvSpPr>"
           + "<p:spPr>"
                   + "<a:xfrm>"
                           + "<a:off x=\"" + x + "\" y=\"" + y + "\"/>"
                           + "<a:ext cx=\"" + cx + "\" cy=\""+ cy + "\"/>"
                   + "</a:xfrm>"
                   + "<a:prstGeom prst=\"" + preset + "\">"
                   + "<a:avLst/>"
                   + "</a:prstGeom>"
                   + "<a:solidFill>"
                       + "<a:srgbClr val=\""+ colorcode + "\"/>"
                   + "</a:solidFill>"
                   + ln
           + "</p:spPr>"
           + style
           + txBody
        + "</p:sp>";
}

我查看了与正确的slide1.xml文件相比缺少XML标头的slide1.xml文件:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>

如何将xml标头定义放入slide1.xml?

谢谢,

阿萨德

0 个答案:

没有答案