在 PowerPoint 文档中添加、修改和移除页脚是非常重要的,因为页脚可以为文档提供额外的信息和组织结构。通过在页脚中添加页码、日期、作者信息或自定义文本,可以帮助观众更好地理解演示内容和跟踪文档版本。页脚还可以增强文档的专业性和整洁度,使其看起来更具吸引力和易读性。修改页脚可以根据需要更新信息或调整格式,确保文档保持最新和一致。移除页脚则可以根据特定需求或设计要求定制文档外观。本文将介绍如何使用 Spire.Presentation for Java 在 Java 项目中添加、修改和移除 Powerpoint 文档中的页脚。
安装 Spire.Presentation for Java
首先,您需要在 Java 程序中添加 Spire.Presentation.jar 文件作为一个依赖项。您可以从这个链接下载 JAR 文件。如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码轻松导入该 JAR 文件。
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>10.10.2</version>
    </dependency>
</dependencies>
Java 在 Powerpoint 文档中添加页脚
使用 Spire.Presentation,您可以轻松实现在 PowerPoint 文档的每个页面底部添加统一的页脚内容。通过添加页脚占位符、页码占位符和时间占位符,您可以确保每个页面上的页脚内容保持一致。以下是详细的步骤:
- 创建一个 Presentation 对象。
- 使用 Presentation.loadFromFile() 方法加载一个 Powerpoint 文档。
- 使用 Presentation.setFooterVisible(true) 设置页脚可见,并设置下页脚文本。
- 使用 Presentation.setSlideNumberVisible(true) 设置幻灯片编号可见,并遍历每张幻灯片,检查是否存在页码占位符,如果是,则修改文本为 "第 X 页" 模式。
- 使用 Presentation.setDateTimeVisible(true) 设置日期时间可见。
- 使用 Presentation.setDateTime() 方法设置日期的格式。
- 通过 Presentation.saveToFile() 方法保存文档到指定路径。
- Java
import com.spire.presentation.*;
import java.util.Date;
public class AddFooter {
    public static void main(String[] args) throws Exception {
        // 创建一个 Presentation 对象
        Presentation presentation = new Presentation();
        // 从文件加载演示文稿
        presentation.loadFromFile("示例1.pptx");
        // 设置页脚可见
        presentation.setFooterVisible(true);
        // 设置页脚文本为 "Spire.Presentation"
        presentation.setFooterText("Spire.Presentation");
        // 设置幻灯片编号可见
        presentation.setSlideNumberVisible(true);
        // 遍历演示文稿中的每一张幻灯片
        for (int i=0;i<presentation.getSlides().getCount();i++) {
            ISlide slide =presentation.getSlides().get(i);
            for(int j=0; j<slide.getShapes().getCount();j++)
            {
                IShape shape =slide.getShapes().get(j);
                if (shape.getPlaceholder() != null)
                {
                    // 如果是页码占位符
                    if (shape.getPlaceholder().getType().equals(PlaceholderType.SLIDE_NUMBER))
                    {
                        IAutoShape shape1 = (IAutoShape)shape;
                        ParagraphEx paragraph = shape1.getTextFrame().getTextRange().getParagraph();
                        String text = paragraph.getText();
                        // 修改幻灯片编号文本为 "第X页"
                        paragraph.setText("第" + text + "页");
                    }
                }
            }
        }
        // 设置日期时间可见
        presentation.setDateTimeVisible(true);
        // 创建一个 Date 对象来表示当前时间
        Date currentDate = new Date();
        // 设置日期时间格式为 "yyyy年MM月dd日"
        presentation.setDateTime(currentDate, "yyyy年MM月dd日");
        // 将修改后的演示文稿保存到文件
        presentation.saveToFile("添加页脚.pptx", FileFormat.PPTX_2016);
        // 释放 Presentation 对象资源
        presentation.dispose();
    }
}

Java 修改 Powerpoint 文档中的页脚
要修改 PowerPoint 文档中的页脚,您需要逐一检查每张幻灯片中的形状,以识别页脚占位符、页码占位符等。通过识别这些占位符,您可以针对每种类型设定特定的内容和格式。以下是详细的步骤:
- 创建一个 Presentation 对象。
- 使用 Presentation.loadFromFile() 方法加载一个 Powerpoint 文档。
- 使用 Presentation.getSlides().get(index) 方法获取一张幻灯片。
- 使用 for 循环遍历幻灯片中的形状,逐一检查每个形状是否为占位符,以确定是否为页脚、页码占位符等,然后修改下它的内容或格式。
- 通过 Presentation.saveToFile() 方法保存文档到指定路径。
- Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
public class ModifyFooter {
    public static void main(String[] args) throws Exception {
        // 创建一个Presentation对象
        Presentation presentation = new Presentation();
        // 从文件加载演示文稿
        presentation.loadFromFile("示例2.pptx");
        // 获取第一页幻灯片
        ISlide slide = presentation.getSlides().get(0);
        // 遍历幻灯片中的形状
        for (int i = 0; i < slide.getShapes().getCount(); i++) {
            // 检查形状是否为占位符
            if (slide.getShapes().get(i).getPlaceholder() != null)
            {
                // 获取占位符类型
                PlaceholderType type = slide.getShapes().get(i).getPlaceholder().getType();
                // 如果是页脚占位符
                if (type == PlaceholderType.FOOTER)
                {
                    // 将形状转换为IAutoShape类型
                    IAutoShape autoShape = (IAutoShape)slide.getShapes().get(i);
                    // 设置文本内容为"E-ICEBLUE"
                    autoShape.getTextFrame().setText("E-ICEBLUE");
                    // 修改文本的字体
                    ChangeFont(autoShape.getTextFrame().getParagraphs().get(0));
                }
                // 如果是页码占位符
                if (type == PlaceholderType.SLIDE_NUMBER)
                {
                    // 将形状转换为IAutoShape类型
                    IAutoShape autoShape = (IAutoShape)slide.getShapes().get(i);
                    // 修改文本的字体
                    ChangeFont(autoShape.getTextFrame().getParagraphs().get(0));
                }
            }
        }
        // 将修改后的演示文稿保存到文件
        presentation.saveToFile("修改页脚.pptx", FileFormat.PPTX_2016);
        // 释放Presentation对象资源
        presentation.dispose();
    }
    static void ChangeFont(ParagraphEx paragraph)
    {
        // 循环遍历段落中的每个文本域
        for(int i=0; i< paragraph.getTextRanges().getCount();i++) {
            // 设置文本样式为斜体
            paragraph.getTextRanges().get(i).isItalic(TriState.TRUE);
            // 设置文本字体
            paragraph.getTextRanges().get(i).setEastAsianFont(new TextFont("宋体"));
            // 设置文本字号为12
            paragraph.getTextRanges().get(i).setFontHeight(34);
            // 设置文本的颜色
            paragraph.getTextRanges().get(i).getFill().setFillType(FillFormatType.SOLID);
            paragraph.getTextRanges().get(i).getFill().getSolidColor().setColor(Color.BLUE);
        }
    }
}
Java 从 Powerpoint 文档中移除页脚
要删除 PowerPoint 文档中的页脚,您需要首先检索幻灯片中的页脚占位符、页码占位符、时间占位符等内容。一旦识别到这些占位符,您可以从幻灯片的形状集合中定位并移除它们。以下是详细的步骤:
- 创建一个 Presentation 对象。
- 使用 Presentation.loadFromFile() 方法加载一个 Powerpoint 文档。
- 使用 Presentation.getSlides().get(index) 方法获取一张幻灯片。
- 使用 for 循环遍历幻灯片中的形状,检查是否为占位符,如果是页脚占位符、页码占位符、时间占位符,则从幻灯片中移除它。
- 通过 Presentation.saveToFile() 方法保存文档到指定路径。
- Java
import com.spire.presentation.*;
public class RemoveFooter {
    public static void main(String[] args) throws Exception{
        // 创建一个Presentation对象
        Presentation presentation = new Presentation();
        // 从文件加载演示文稿
        presentation.loadFromFile("示例2.pptx");
        // 获取第一页幻灯片
        ISlide slide = presentation.getSlides().get(0);
        // 遍历幻灯片中的形状
        for (int i = slide.getShapes().getCount()-1; i>=0; i--) {
            // 检查形状是否为占位符
            if (slide.getShapes().get(i).getPlaceholder() != null)
            {
                // 获取占位符类型
                PlaceholderType type = slide.getShapes().get(i).getPlaceholder().getType();
                // 如果是页脚占位符
                if (type == PlaceholderType.FOOTER)
                {
                    // 从幻灯片中移除它
                    slide.getShapes().removeAt(i);
                }
                // 如果是页码占位符
                if (type == PlaceholderType.SLIDE_NUMBER)
                {
                    // 从幻灯片中移除它
                    slide.getShapes().removeAt(i);
                }
                // 如果是时间占位符
                if (type == PlaceholderType.DATE_AND_TIME)
                {
                    // 从幻灯片中移除它
                    slide.getShapes().removeAt(i);
                }
            }
        }
        // 将修改后的演示文稿保存到文件
        presentation.saveToFile("移除页脚.pptx", FileFormat.PPTX_2016);
        // 释放Presentation对象资源
        presentation.dispose();
    }
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



