Page 1 of 1

Auto numbering slides in pptx

PostPosted: Thu Nov 24, 2022 12:46 am
by quimperval
I want to add a shape with a text that holds the slide number in a presentation. I've been cheching the structure of a xml that invludes it and I found it has a shape object which contains a textbody "<p:txBody>" and the text body contains the below entity
<a:p>
<a:fld id="{an-uri-here}" type="slidenum">
<a:t> <#> </a:t>
</a:fld>
</a:p>

The class CTTextBody contains a list of CTTextParagraphs and that does not match the CTTextField that contains the information for adding the auto numbering object in the slides.

Does anyone knows the class that could allow to put a CTTextField in a slide?

Re: Auto numbering slides in pptx

PostPosted: Thu Nov 24, 2022 6:57 am
by jason
CTTextField goes inside CTTextParagraph

From the XML shown further below, docx4j webapp generates:

Method 1: via ObjectFactory

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
import java.lang.Long;
import org.docx4j.dml.CTTextCharacterProperties;
import org.docx4j.dml.CTTextField;
import org.docx4j.dml.CTTextParagraph;


public class Foo {
public CTTextParagraph createIt() {

org.docx4j.dml.ObjectFactory dmlObjectFactory = new org.docx4j.dml.ObjectFactory();

CTTextParagraph textparagraph = dmlObjectFactory.createCTTextParagraph();
    // Create object for endParaRPr
    CTTextField textfield = dmlObjectFactory.createCTTextField();
    textparagraph.getEGTextRun().add( textfield);
        // Create object for rPr
        CTTextCharacterProperties textcharacterproperties = dmlObjectFactory.createCTTextCharacterProperties();
        textfield.setRPr(textcharacterproperties);
            textcharacterproperties.setLang( "en-AU");
            textcharacterproperties.setSmtId( new Long(0) );
        textfield.setT( "1");
        textfield.setId( "{54DA7E74-E873-4E88-B918-E260C39D583F}");
        textfield.setType( "slidenum");
    // Create object for endParaRPr
    CTTextCharacterProperties textcharacterproperties2 = dmlObjectFactory.createCTTextCharacterProperties();
    textparagraph.setEndParaRPr(textcharacterproperties2);
        textcharacterproperties2.setLang( "en-US");
        textcharacterproperties2.setSmtId( new Long(0) );

return textparagraph;
}
}
 
Parsed in 0.016 seconds, using GeSHi 1.0.8.4


Method 2


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
String openXML = "<a:p xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
            + "<a:fld id=\"{54DA7E74-E873-4E88-B918-E260C39D583F}\" type=\"slidenum\">"
                + "<a:rPr lang=\"en-AU\"/>"
                + "<a:t>1</a:t>"
            + "</a:fld>"
            + "<a:endParaRPr dirty=\"false\" lang=\"en-US\"/>"
        + "</a:p>";
CTTextParagraph textparagraph = (CTTextParagraph)XmlUtils.unmarshalString(openXML);
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4