How to renames the slide master












0















I want rename the PowerPoint slide master by apache poi. In PowerPoint GUI we do View - Slide Master - then we right click the top most slide on left side and select Rename Master from context menu.



enter image description here










share|improve this question

























  • Please help us to help you. This is a English Question/Answer site. So screen shots of Chinese PowerPoint GUI will not be very helpful for most of the people here. So instead of that screenshot do describing what you are doing in the GUI to rename the slide master.

    – Axel Richter
    Jan 19 at 10:43
















0















I want rename the PowerPoint slide master by apache poi. In PowerPoint GUI we do View - Slide Master - then we right click the top most slide on left side and select Rename Master from context menu.



enter image description here










share|improve this question

























  • Please help us to help you. This is a English Question/Answer site. So screen shots of Chinese PowerPoint GUI will not be very helpful for most of the people here. So instead of that screenshot do describing what you are doing in the GUI to rename the slide master.

    – Axel Richter
    Jan 19 at 10:43














0












0








0








I want rename the PowerPoint slide master by apache poi. In PowerPoint GUI we do View - Slide Master - then we right click the top most slide on left side and select Rename Master from context menu.



enter image description here










share|improve this question
















I want rename the PowerPoint slide master by apache poi. In PowerPoint GUI we do View - Slide Master - then we right click the top most slide on left side and select Rename Master from context menu.



enter image description here







apache-poi powerpoint master






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 11:15









Axel Richter

25.4k21935




25.4k21935










asked Jan 19 at 9:45









javaerjavaer

1




1













  • Please help us to help you. This is a English Question/Answer site. So screen shots of Chinese PowerPoint GUI will not be very helpful for most of the people here. So instead of that screenshot do describing what you are doing in the GUI to rename the slide master.

    – Axel Richter
    Jan 19 at 10:43



















  • Please help us to help you. This is a English Question/Answer site. So screen shots of Chinese PowerPoint GUI will not be very helpful for most of the people here. So instead of that screenshot do describing what you are doing in the GUI to rename the slide master.

    – Axel Richter
    Jan 19 at 10:43

















Please help us to help you. This is a English Question/Answer site. So screen shots of Chinese PowerPoint GUI will not be very helpful for most of the people here. So instead of that screenshot do describing what you are doing in the GUI to rename the slide master.

– Axel Richter
Jan 19 at 10:43





Please help us to help you. This is a English Question/Answer site. So screen shots of Chinese PowerPoint GUI will not be very helpful for most of the people here. So instead of that screenshot do describing what you are doing in the GUI to rename the slide master.

– Axel Richter
Jan 19 at 10:43












1 Answer
1






active

oldest

votes


















1














In a PowerPoint presentation the master is named such as it's theme. We can get all masters using XMLSlideShow.getSlideMasters. XSLFSlideMaster
extends XSLFSheet. So we can get the theme of each master using XSLFSheet.getTheme. Once we have the XSLFTheme there are getters and setters for the name.



Example:



import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.xslf.usermodel.*;

public class XSLFRenameMasterTheme {

public static void main(String args) throws Exception {

XMLSlideShow slideshow = new XMLSlideShow(new FileInputStream("Presentation.pptx"));

for (XSLFSlideMaster master : slideshow.getSlideMasters()) {
XSLFTheme theme = master.getTheme();
String name = theme.getName();
System.out.println(name);
theme.setName(name + " renamed");
System.out.println(theme.getName());
}

FileOutputStream out = new FileOutputStream("PresentationRenamedMaster.pptx");
slideshow.write(out);
out.close();
slideshow.close();
}
}




For HSLFSlideShow is seems there is no access to master names supported. One can get the HSLFSlideMasters but not the names of them.



So if one needs doing that nevertheless, then one must know about the internals of the binary *.ppt file system. This is documented in [MS-PPT]: PowerPoint (.ppt) Binary File Format. The sheet names are in a SlideNameAtom. With knowledge about the internals one can create a class for that kind of record. This can providing methods for get and set the name then.



Example:



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;

import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.RecordAtom;

import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.StringUtil;

public class HSLFRenameMaster {

// method for get SlideNameAtom out of the master
private static SlideNameAtom getSlideNameAtom(HSLFSlideMaster master) throws Exception {
SlideNameAtom slideNameAtomRecord = null;
Record record = master.getSheetContainer().findFirstOfType(0x0FBA);
if (record != null) { // SlideNameAtom exists
// get present data
ByteArrayOutputStream out = new ByteArrayOutputStream();
record.writeOut(out);
out.flush();
byte data = out.toByteArray();
out.close();
// create new SlideNameAtom from data
slideNameAtomRecord = new SlideNameAtom(data);
// replace old record with new SlideNameAtom
master.getSheetContainer().addChildBefore(
slideNameAtomRecord,
record
);
master.getSheetContainer().removeChild(record);
}
return slideNameAtomRecord;
}


public static void main(String args) throws Exception {

HSLFSlideShow slideshow = new HSLFSlideShow(new FileInputStream("Presentation.ppt"));

for (HSLFSlideMaster master : slideshow.getSlideMasters()) {
SlideNameAtom slideNameAtomRecord = getSlideNameAtom(master);
if (slideNameAtomRecord != null) {
String name = slideNameAtomRecord.getName();
System.out.println(name);
slideNameAtomRecord.setName(name + " renamed");
System.out.println(slideNameAtomRecord.getName());
}
}

FileOutputStream out = new FileOutputStream("PresentationRenamedMaster.ppt");
slideshow.write(out);
out.close();
slideshow.close();
}

//class SlideNameAtom
//having methods for manipulating the [SlideNameAtom](https://msdn.microsoft.com/en-us/library/dd906297(v=office.12).aspx)
private static class SlideNameAtom extends RecordAtom {

private byte data;
private String name;

public SlideNameAtom() {
this.name = "Office";
setName(name);
}

public SlideNameAtom(byte data) {
this.data = data;
this.name = getName();
}

public void setName(String name) {
this.name = name;
int length = 8;
length += StringUtil.getToUnicodeLE(name).length;
this.data = new byte[length];
data[0] = (byte)0x20; data[1] = (byte)0x00;
data[2] = (byte)0xBA; data[3] = (byte)0x0F; //MUST be 0x0fba = RT_CString (little endian)
LittleEndian.putInt(data, 4, StringUtil.getToUnicodeLE(name).length);
StringUtil.putUnicodeLE(name, data, 8);
}

public String getName() {
return StringUtil.getFromUnicodeLE(this.data, 8, (this.data.length-8)/2);
}

@Override
public void writeOut(OutputStream out) throws IOException {
out.write(data);
}

@Override
public long getRecordType() { return 0x0FBA; }
}

}


The question is whether renaming the master is worth that effort.






share|improve this answer


























  • thanks for your sample code and suggest about Q&A :)

    – javaer
    Jan 21 at 3:48











  • using xslf , i can get the getTheme method, but hslf , i do not find out

    – javaer
    Jan 22 at 4:34











  • @avaer: See my supplements.

    – Axel Richter
    Jan 22 at 19:36











  • i am very very thanks for your help, your technical so NB, NB is a chinese word. just like “very excellent” :)

    – javaer
    Jan 23 at 2:07











  • @javaer: 別客氣。 You are welcome.

    – Axel Richter
    Jan 23 at 4:39











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265822%2fhow-to-renames-the-slide-master%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














In a PowerPoint presentation the master is named such as it's theme. We can get all masters using XMLSlideShow.getSlideMasters. XSLFSlideMaster
extends XSLFSheet. So we can get the theme of each master using XSLFSheet.getTheme. Once we have the XSLFTheme there are getters and setters for the name.



Example:



import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.xslf.usermodel.*;

public class XSLFRenameMasterTheme {

public static void main(String args) throws Exception {

XMLSlideShow slideshow = new XMLSlideShow(new FileInputStream("Presentation.pptx"));

for (XSLFSlideMaster master : slideshow.getSlideMasters()) {
XSLFTheme theme = master.getTheme();
String name = theme.getName();
System.out.println(name);
theme.setName(name + " renamed");
System.out.println(theme.getName());
}

FileOutputStream out = new FileOutputStream("PresentationRenamedMaster.pptx");
slideshow.write(out);
out.close();
slideshow.close();
}
}




For HSLFSlideShow is seems there is no access to master names supported. One can get the HSLFSlideMasters but not the names of them.



So if one needs doing that nevertheless, then one must know about the internals of the binary *.ppt file system. This is documented in [MS-PPT]: PowerPoint (.ppt) Binary File Format. The sheet names are in a SlideNameAtom. With knowledge about the internals one can create a class for that kind of record. This can providing methods for get and set the name then.



Example:



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;

import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.RecordAtom;

import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.StringUtil;

public class HSLFRenameMaster {

// method for get SlideNameAtom out of the master
private static SlideNameAtom getSlideNameAtom(HSLFSlideMaster master) throws Exception {
SlideNameAtom slideNameAtomRecord = null;
Record record = master.getSheetContainer().findFirstOfType(0x0FBA);
if (record != null) { // SlideNameAtom exists
// get present data
ByteArrayOutputStream out = new ByteArrayOutputStream();
record.writeOut(out);
out.flush();
byte data = out.toByteArray();
out.close();
// create new SlideNameAtom from data
slideNameAtomRecord = new SlideNameAtom(data);
// replace old record with new SlideNameAtom
master.getSheetContainer().addChildBefore(
slideNameAtomRecord,
record
);
master.getSheetContainer().removeChild(record);
}
return slideNameAtomRecord;
}


public static void main(String args) throws Exception {

HSLFSlideShow slideshow = new HSLFSlideShow(new FileInputStream("Presentation.ppt"));

for (HSLFSlideMaster master : slideshow.getSlideMasters()) {
SlideNameAtom slideNameAtomRecord = getSlideNameAtom(master);
if (slideNameAtomRecord != null) {
String name = slideNameAtomRecord.getName();
System.out.println(name);
slideNameAtomRecord.setName(name + " renamed");
System.out.println(slideNameAtomRecord.getName());
}
}

FileOutputStream out = new FileOutputStream("PresentationRenamedMaster.ppt");
slideshow.write(out);
out.close();
slideshow.close();
}

//class SlideNameAtom
//having methods for manipulating the [SlideNameAtom](https://msdn.microsoft.com/en-us/library/dd906297(v=office.12).aspx)
private static class SlideNameAtom extends RecordAtom {

private byte data;
private String name;

public SlideNameAtom() {
this.name = "Office";
setName(name);
}

public SlideNameAtom(byte data) {
this.data = data;
this.name = getName();
}

public void setName(String name) {
this.name = name;
int length = 8;
length += StringUtil.getToUnicodeLE(name).length;
this.data = new byte[length];
data[0] = (byte)0x20; data[1] = (byte)0x00;
data[2] = (byte)0xBA; data[3] = (byte)0x0F; //MUST be 0x0fba = RT_CString (little endian)
LittleEndian.putInt(data, 4, StringUtil.getToUnicodeLE(name).length);
StringUtil.putUnicodeLE(name, data, 8);
}

public String getName() {
return StringUtil.getFromUnicodeLE(this.data, 8, (this.data.length-8)/2);
}

@Override
public void writeOut(OutputStream out) throws IOException {
out.write(data);
}

@Override
public long getRecordType() { return 0x0FBA; }
}

}


The question is whether renaming the master is worth that effort.






share|improve this answer


























  • thanks for your sample code and suggest about Q&A :)

    – javaer
    Jan 21 at 3:48











  • using xslf , i can get the getTheme method, but hslf , i do not find out

    – javaer
    Jan 22 at 4:34











  • @avaer: See my supplements.

    – Axel Richter
    Jan 22 at 19:36











  • i am very very thanks for your help, your technical so NB, NB is a chinese word. just like “very excellent” :)

    – javaer
    Jan 23 at 2:07











  • @javaer: 別客氣。 You are welcome.

    – Axel Richter
    Jan 23 at 4:39
















1














In a PowerPoint presentation the master is named such as it's theme. We can get all masters using XMLSlideShow.getSlideMasters. XSLFSlideMaster
extends XSLFSheet. So we can get the theme of each master using XSLFSheet.getTheme. Once we have the XSLFTheme there are getters and setters for the name.



Example:



import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.xslf.usermodel.*;

public class XSLFRenameMasterTheme {

public static void main(String args) throws Exception {

XMLSlideShow slideshow = new XMLSlideShow(new FileInputStream("Presentation.pptx"));

for (XSLFSlideMaster master : slideshow.getSlideMasters()) {
XSLFTheme theme = master.getTheme();
String name = theme.getName();
System.out.println(name);
theme.setName(name + " renamed");
System.out.println(theme.getName());
}

FileOutputStream out = new FileOutputStream("PresentationRenamedMaster.pptx");
slideshow.write(out);
out.close();
slideshow.close();
}
}




For HSLFSlideShow is seems there is no access to master names supported. One can get the HSLFSlideMasters but not the names of them.



So if one needs doing that nevertheless, then one must know about the internals of the binary *.ppt file system. This is documented in [MS-PPT]: PowerPoint (.ppt) Binary File Format. The sheet names are in a SlideNameAtom. With knowledge about the internals one can create a class for that kind of record. This can providing methods for get and set the name then.



Example:



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;

import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.RecordAtom;

import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.StringUtil;

public class HSLFRenameMaster {

// method for get SlideNameAtom out of the master
private static SlideNameAtom getSlideNameAtom(HSLFSlideMaster master) throws Exception {
SlideNameAtom slideNameAtomRecord = null;
Record record = master.getSheetContainer().findFirstOfType(0x0FBA);
if (record != null) { // SlideNameAtom exists
// get present data
ByteArrayOutputStream out = new ByteArrayOutputStream();
record.writeOut(out);
out.flush();
byte data = out.toByteArray();
out.close();
// create new SlideNameAtom from data
slideNameAtomRecord = new SlideNameAtom(data);
// replace old record with new SlideNameAtom
master.getSheetContainer().addChildBefore(
slideNameAtomRecord,
record
);
master.getSheetContainer().removeChild(record);
}
return slideNameAtomRecord;
}


public static void main(String args) throws Exception {

HSLFSlideShow slideshow = new HSLFSlideShow(new FileInputStream("Presentation.ppt"));

for (HSLFSlideMaster master : slideshow.getSlideMasters()) {
SlideNameAtom slideNameAtomRecord = getSlideNameAtom(master);
if (slideNameAtomRecord != null) {
String name = slideNameAtomRecord.getName();
System.out.println(name);
slideNameAtomRecord.setName(name + " renamed");
System.out.println(slideNameAtomRecord.getName());
}
}

FileOutputStream out = new FileOutputStream("PresentationRenamedMaster.ppt");
slideshow.write(out);
out.close();
slideshow.close();
}

//class SlideNameAtom
//having methods for manipulating the [SlideNameAtom](https://msdn.microsoft.com/en-us/library/dd906297(v=office.12).aspx)
private static class SlideNameAtom extends RecordAtom {

private byte data;
private String name;

public SlideNameAtom() {
this.name = "Office";
setName(name);
}

public SlideNameAtom(byte data) {
this.data = data;
this.name = getName();
}

public void setName(String name) {
this.name = name;
int length = 8;
length += StringUtil.getToUnicodeLE(name).length;
this.data = new byte[length];
data[0] = (byte)0x20; data[1] = (byte)0x00;
data[2] = (byte)0xBA; data[3] = (byte)0x0F; //MUST be 0x0fba = RT_CString (little endian)
LittleEndian.putInt(data, 4, StringUtil.getToUnicodeLE(name).length);
StringUtil.putUnicodeLE(name, data, 8);
}

public String getName() {
return StringUtil.getFromUnicodeLE(this.data, 8, (this.data.length-8)/2);
}

@Override
public void writeOut(OutputStream out) throws IOException {
out.write(data);
}

@Override
public long getRecordType() { return 0x0FBA; }
}

}


The question is whether renaming the master is worth that effort.






share|improve this answer


























  • thanks for your sample code and suggest about Q&A :)

    – javaer
    Jan 21 at 3:48











  • using xslf , i can get the getTheme method, but hslf , i do not find out

    – javaer
    Jan 22 at 4:34











  • @avaer: See my supplements.

    – Axel Richter
    Jan 22 at 19:36











  • i am very very thanks for your help, your technical so NB, NB is a chinese word. just like “very excellent” :)

    – javaer
    Jan 23 at 2:07











  • @javaer: 別客氣。 You are welcome.

    – Axel Richter
    Jan 23 at 4:39














1












1








1







In a PowerPoint presentation the master is named such as it's theme. We can get all masters using XMLSlideShow.getSlideMasters. XSLFSlideMaster
extends XSLFSheet. So we can get the theme of each master using XSLFSheet.getTheme. Once we have the XSLFTheme there are getters and setters for the name.



Example:



import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.xslf.usermodel.*;

public class XSLFRenameMasterTheme {

public static void main(String args) throws Exception {

XMLSlideShow slideshow = new XMLSlideShow(new FileInputStream("Presentation.pptx"));

for (XSLFSlideMaster master : slideshow.getSlideMasters()) {
XSLFTheme theme = master.getTheme();
String name = theme.getName();
System.out.println(name);
theme.setName(name + " renamed");
System.out.println(theme.getName());
}

FileOutputStream out = new FileOutputStream("PresentationRenamedMaster.pptx");
slideshow.write(out);
out.close();
slideshow.close();
}
}




For HSLFSlideShow is seems there is no access to master names supported. One can get the HSLFSlideMasters but not the names of them.



So if one needs doing that nevertheless, then one must know about the internals of the binary *.ppt file system. This is documented in [MS-PPT]: PowerPoint (.ppt) Binary File Format. The sheet names are in a SlideNameAtom. With knowledge about the internals one can create a class for that kind of record. This can providing methods for get and set the name then.



Example:



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;

import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.RecordAtom;

import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.StringUtil;

public class HSLFRenameMaster {

// method for get SlideNameAtom out of the master
private static SlideNameAtom getSlideNameAtom(HSLFSlideMaster master) throws Exception {
SlideNameAtom slideNameAtomRecord = null;
Record record = master.getSheetContainer().findFirstOfType(0x0FBA);
if (record != null) { // SlideNameAtom exists
// get present data
ByteArrayOutputStream out = new ByteArrayOutputStream();
record.writeOut(out);
out.flush();
byte data = out.toByteArray();
out.close();
// create new SlideNameAtom from data
slideNameAtomRecord = new SlideNameAtom(data);
// replace old record with new SlideNameAtom
master.getSheetContainer().addChildBefore(
slideNameAtomRecord,
record
);
master.getSheetContainer().removeChild(record);
}
return slideNameAtomRecord;
}


public static void main(String args) throws Exception {

HSLFSlideShow slideshow = new HSLFSlideShow(new FileInputStream("Presentation.ppt"));

for (HSLFSlideMaster master : slideshow.getSlideMasters()) {
SlideNameAtom slideNameAtomRecord = getSlideNameAtom(master);
if (slideNameAtomRecord != null) {
String name = slideNameAtomRecord.getName();
System.out.println(name);
slideNameAtomRecord.setName(name + " renamed");
System.out.println(slideNameAtomRecord.getName());
}
}

FileOutputStream out = new FileOutputStream("PresentationRenamedMaster.ppt");
slideshow.write(out);
out.close();
slideshow.close();
}

//class SlideNameAtom
//having methods for manipulating the [SlideNameAtom](https://msdn.microsoft.com/en-us/library/dd906297(v=office.12).aspx)
private static class SlideNameAtom extends RecordAtom {

private byte data;
private String name;

public SlideNameAtom() {
this.name = "Office";
setName(name);
}

public SlideNameAtom(byte data) {
this.data = data;
this.name = getName();
}

public void setName(String name) {
this.name = name;
int length = 8;
length += StringUtil.getToUnicodeLE(name).length;
this.data = new byte[length];
data[0] = (byte)0x20; data[1] = (byte)0x00;
data[2] = (byte)0xBA; data[3] = (byte)0x0F; //MUST be 0x0fba = RT_CString (little endian)
LittleEndian.putInt(data, 4, StringUtil.getToUnicodeLE(name).length);
StringUtil.putUnicodeLE(name, data, 8);
}

public String getName() {
return StringUtil.getFromUnicodeLE(this.data, 8, (this.data.length-8)/2);
}

@Override
public void writeOut(OutputStream out) throws IOException {
out.write(data);
}

@Override
public long getRecordType() { return 0x0FBA; }
}

}


The question is whether renaming the master is worth that effort.






share|improve this answer















In a PowerPoint presentation the master is named such as it's theme. We can get all masters using XMLSlideShow.getSlideMasters. XSLFSlideMaster
extends XSLFSheet. So we can get the theme of each master using XSLFSheet.getTheme. Once we have the XSLFTheme there are getters and setters for the name.



Example:



import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.xslf.usermodel.*;

public class XSLFRenameMasterTheme {

public static void main(String args) throws Exception {

XMLSlideShow slideshow = new XMLSlideShow(new FileInputStream("Presentation.pptx"));

for (XSLFSlideMaster master : slideshow.getSlideMasters()) {
XSLFTheme theme = master.getTheme();
String name = theme.getName();
System.out.println(name);
theme.setName(name + " renamed");
System.out.println(theme.getName());
}

FileOutputStream out = new FileOutputStream("PresentationRenamedMaster.pptx");
slideshow.write(out);
out.close();
slideshow.close();
}
}




For HSLFSlideShow is seems there is no access to master names supported. One can get the HSLFSlideMasters but not the names of them.



So if one needs doing that nevertheless, then one must know about the internals of the binary *.ppt file system. This is documented in [MS-PPT]: PowerPoint (.ppt) Binary File Format. The sheet names are in a SlideNameAtom. With knowledge about the internals one can create a class for that kind of record. This can providing methods for get and set the name then.



Example:



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;

import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.RecordAtom;

import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.StringUtil;

public class HSLFRenameMaster {

// method for get SlideNameAtom out of the master
private static SlideNameAtom getSlideNameAtom(HSLFSlideMaster master) throws Exception {
SlideNameAtom slideNameAtomRecord = null;
Record record = master.getSheetContainer().findFirstOfType(0x0FBA);
if (record != null) { // SlideNameAtom exists
// get present data
ByteArrayOutputStream out = new ByteArrayOutputStream();
record.writeOut(out);
out.flush();
byte data = out.toByteArray();
out.close();
// create new SlideNameAtom from data
slideNameAtomRecord = new SlideNameAtom(data);
// replace old record with new SlideNameAtom
master.getSheetContainer().addChildBefore(
slideNameAtomRecord,
record
);
master.getSheetContainer().removeChild(record);
}
return slideNameAtomRecord;
}


public static void main(String args) throws Exception {

HSLFSlideShow slideshow = new HSLFSlideShow(new FileInputStream("Presentation.ppt"));

for (HSLFSlideMaster master : slideshow.getSlideMasters()) {
SlideNameAtom slideNameAtomRecord = getSlideNameAtom(master);
if (slideNameAtomRecord != null) {
String name = slideNameAtomRecord.getName();
System.out.println(name);
slideNameAtomRecord.setName(name + " renamed");
System.out.println(slideNameAtomRecord.getName());
}
}

FileOutputStream out = new FileOutputStream("PresentationRenamedMaster.ppt");
slideshow.write(out);
out.close();
slideshow.close();
}

//class SlideNameAtom
//having methods for manipulating the [SlideNameAtom](https://msdn.microsoft.com/en-us/library/dd906297(v=office.12).aspx)
private static class SlideNameAtom extends RecordAtom {

private byte data;
private String name;

public SlideNameAtom() {
this.name = "Office";
setName(name);
}

public SlideNameAtom(byte data) {
this.data = data;
this.name = getName();
}

public void setName(String name) {
this.name = name;
int length = 8;
length += StringUtil.getToUnicodeLE(name).length;
this.data = new byte[length];
data[0] = (byte)0x20; data[1] = (byte)0x00;
data[2] = (byte)0xBA; data[3] = (byte)0x0F; //MUST be 0x0fba = RT_CString (little endian)
LittleEndian.putInt(data, 4, StringUtil.getToUnicodeLE(name).length);
StringUtil.putUnicodeLE(name, data, 8);
}

public String getName() {
return StringUtil.getFromUnicodeLE(this.data, 8, (this.data.length-8)/2);
}

@Override
public void writeOut(OutputStream out) throws IOException {
out.write(data);
}

@Override
public long getRecordType() { return 0x0FBA; }
}

}


The question is whether renaming the master is worth that effort.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 22 at 19:35

























answered Jan 19 at 10:39









Axel RichterAxel Richter

25.4k21935




25.4k21935













  • thanks for your sample code and suggest about Q&A :)

    – javaer
    Jan 21 at 3:48











  • using xslf , i can get the getTheme method, but hslf , i do not find out

    – javaer
    Jan 22 at 4:34











  • @avaer: See my supplements.

    – Axel Richter
    Jan 22 at 19:36











  • i am very very thanks for your help, your technical so NB, NB is a chinese word. just like “very excellent” :)

    – javaer
    Jan 23 at 2:07











  • @javaer: 別客氣。 You are welcome.

    – Axel Richter
    Jan 23 at 4:39



















  • thanks for your sample code and suggest about Q&A :)

    – javaer
    Jan 21 at 3:48











  • using xslf , i can get the getTheme method, but hslf , i do not find out

    – javaer
    Jan 22 at 4:34











  • @avaer: See my supplements.

    – Axel Richter
    Jan 22 at 19:36











  • i am very very thanks for your help, your technical so NB, NB is a chinese word. just like “very excellent” :)

    – javaer
    Jan 23 at 2:07











  • @javaer: 別客氣。 You are welcome.

    – Axel Richter
    Jan 23 at 4:39

















thanks for your sample code and suggest about Q&A :)

– javaer
Jan 21 at 3:48





thanks for your sample code and suggest about Q&A :)

– javaer
Jan 21 at 3:48













using xslf , i can get the getTheme method, but hslf , i do not find out

– javaer
Jan 22 at 4:34





using xslf , i can get the getTheme method, but hslf , i do not find out

– javaer
Jan 22 at 4:34













@avaer: See my supplements.

– Axel Richter
Jan 22 at 19:36





@avaer: See my supplements.

– Axel Richter
Jan 22 at 19:36













i am very very thanks for your help, your technical so NB, NB is a chinese word. just like “very excellent” :)

– javaer
Jan 23 at 2:07





i am very very thanks for your help, your technical so NB, NB is a chinese word. just like “very excellent” :)

– javaer
Jan 23 at 2:07













@javaer: 別客氣。 You are welcome.

– Axel Richter
Jan 23 at 4:39





@javaer: 別客氣。 You are welcome.

– Axel Richter
Jan 23 at 4:39


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265822%2fhow-to-renames-the-slide-master%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre