Read and Replace Header and Footer Text in a Presentation in Java

Hi, I am trying to extract Header and Footer text from a pptx.

I see, that I can write text via the HeaderFooterManager, like so:

IMasterNotesSlide masterNotesSlide = presentation.getMasterNotesSlideManager().getMasterNotesSlide();
masterNotesSlide.getHeaderFooterManager().setHeaderText("HI there new header");

But I cannot see how to read it.

Here is my code:

@Test
void test() throws IOException {
    Path input1 = Path.of("src","test","resources", "Title.pptx");
    Path input2 = Path.of("src","test","resources", "Content.pptx");
    Path input3 = Path.of("src","test","resources", "Content2.pptx");
    Path input4 = Path.of("src","test","resources", "Schluss.pptx");
    List<Path> inputs = Arrays.asList(input2, input3, input4);
    try (InputStream stream = new ByteArrayInputStream(Files.readAllBytes(input1))) {
        Presentation presentation = new Presentation(stream);

        IMasterNotesSlide masterNotesSlide = presentation.getMasterNotesSlideManager().getMasterNotesSlide();
        masterNotesSlide.getHeaderFooterManager().setHeaderText("HI there new header");
       
        try {
            for (Path path : inputs) {
                try (InputStream stream2 = new ByteArrayInputStream(Files.readAllBytes(path))) {
                    Presentation presentation2 = new Presentation(stream2);
                    try {
                        for (ISlide slide : presentation2.getSlides()) {
                            presentation.getSlides().addClone(slide);
                        }
                    } finally {
                        presentation2.dispose();
                    }
                }
            }
        }finally {
            presentation.save("combined.pptx", SaveFormat.Pptx);
            presentation.dispose();
        }
    }
}

To be precise, I want to replace a placeholder string in Header and Footer with a specific string.

What is the best way to do so? Is there a concept like “MergeField” or similar in pptx that I could use?

@kerner

To extract the header and footer text from a PowerPoint presentation using Aspose.Slides, you can utilize the HeaderFooterManager associated with the IMasterNotesSlide and INotesSlide. Below is an example of how to read the header and footer text from the notes slide.

Here’s a code snippet that demonstrates how to achieve this:

import com.aspose.slides.*;

public class ReadHeaderFooter {
    public static void main(String[] args) {
        // Load the presentation
        Presentation presentation = new Presentation("path/to/your/presentation.pptx");

        // Access the master notes slide
        IMasterNotesSlide masterNotesSlide = presentation.getMasterNotesSlideManager().getMasterNotesSlide();
        if (masterNotesSlide != null) {
            // Read header and footer text from the master notes slide
            String masterHeaderText = masterNotesSlide.getHeaderFooterManager().getHeaderText();
            String masterFooterText = masterNotesSlide.getHeaderFooterManager().getFooterText();
            System.out.println("Master Header: " + masterHeaderText);
            System.out.println("Master Footer: " + masterFooterText);
        }

        // Access the first notes slide
        INotesSlide notesSlide = presentation.getSlides().get_Item(0).getNotesSlideManager().getNotesSlide();
        if (notesSlide != null) {
            // Read header and footer text from the notes slide
            String notesHeaderText = notesSlide.getHeaderFooterManager().getHeaderText();
            String notesFooterText = notesSlide.getHeaderFooterManager().getFooterText();
            System.out.println("Notes Header: " + notesHeaderText);
            System.out.println("Notes Footer: " + notesFooterText);
        }

        // Dispose of the presentation
        presentation.dispose();
    }
}

In this example:

  • The presentation is loaded from a specified path.
  • The master notes slide is accessed to retrieve the header and footer text using the HeaderFooterManager.
  • The first notes slide is also accessed to get its header and footer text.
  • Finally, the retrieved texts are printed to the console.

Make sure to replace "path/to/your/presentation.pptx" with the actual path to your PowerPoint file.

This approach allows you to effectively read the header and footer text from both the master notes slide and individual notes slides in your presentation.

Sorry, but there is no such methods as

.getHeaderText() and .getFooterText() in IMasterNotesSlide.

@kerner,
I need some time to check the issue. I will get back to you as soon as possible.

1 Like

@kerner,
Thank you for your patience. Unfortunately, the HeaderFooterManager of master notes slides does not yet provide methods to read header and footer text.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): SLIDESJAVA-39665

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@kerner,
Our developers have investigated the case. Please try using the following code example:

Presentation presentation = new Presentation("sample.pptx");

IMasterNotesSlide notesSlide = presentation.getMasterNotesSlideManager().getMasterNotesSlide();
for (IShape shape :  notesSlide.getShapes()) {
    if (shape instanceof AutoShape) {
        AutoShape autoShape = (AutoShape) shape;
        if (shape.getPlaceholder() != null && shape.getPlaceholder().getType() == PlaceholderType.Footer) {
            System.out.println(autoShape.getTextFrame().getText());
        }
        if (shape.getPlaceholder() != null && shape.getPlaceholder().getType() == PlaceholderType.Header) {
            System.out.println(autoShape.getTextFrame().getText());
        }
    }
}

presentation.dispose();