Tab 1
此演示向您展示如何在 PowerPoint 文档中搜索特定文本并替换匹配的文本。
Upload
Maximum file size: 1 MB. Files accepted: ppt, pptx.
Click here to browse files.
fileerrors
Convert to
Source file:
filename
Search Text:
Replace Text:
downloads
如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo。
如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。。
Tab 2
package ppt;
import com.spire.presentation.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
public class FindAndReplaceDemo {
public void findOrReplace(String filePath,String changeType, String text, String newText, String resultFilePath) throws Exception {
Presentation presentation = new Presentation();
presentation.loadFromFile(filePath);
switch (changeType){
case "FIND":
findText(presentation,text);
break;
case "REPLACE":
replaceText(presentation,text,newText);
break;
}
presentation.saveToFile(resultFilePath,FileFormat.PPTX_2013);
}
private void findText(Presentation presentation, String text){
for(int i = 0; i < presentation.getSlides().getCount();i++){
ISlide slide = presentation.getSlides().get(i);
for (int j = 0; j < slide.getShapes().getCount();j++){
IAutoShape shape = (IAutoShape)slide.getShapes().get(j);
TextHighLightingOptions options = new TextHighLightingOptions();
options.setWholeWordsOnly(true);
options.setCaseSensitive(true);
shape.getTextFrame().highLightText(text, Color.yellow, options);
}
}
}
private void replaceText(Presentation presentation, String oldText, String newText){
for(int i = 0; i < presentation.getSlides().getCount();i++){
ISlide slide = presentation.getSlides().get(i);
Map map = new HashMap<>();
map.put(oldText,newText);
replaceTags(slide,map);
}
}
private static void replaceTags(ISlide pSlide, Map tagValues) {
for (int i = 0; i < pSlide.getShapes().getCount(); i++) {
IShape curShape = pSlide.getShapes().get(i);
if (curShape instanceof IAutoShape) {
for (int j = 0; j < ((IAutoShape) curShape).getTextFrame().getParagraphs().getCount(); j++) {
ParagraphEx tp = ((IAutoShape) curShape).getTextFrame().getParagraphs().get(j);
for (Map.Entry entry : tagValues.entrySet()) {
String mapKey = entry.getKey();
String mapValue = entry.getValue();
if (tp.getText().contains(mapKey)) {
tp.setText(tp.getText().replace(mapKey, mapValue));
}
}
}
}
}
}
}
Tab 3
using Spire.Presentation;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoOnlineCode
{
class HighlightOrReplace
{
public void HighlightOrReplaceDemo(string filePath, string format, string text, string newText, string resultFileName)
{
Presentation presentation = new Presentation();
presentation.LoadFromFile(filePath);
switch (format)
{
case "HIGHLIGHT":
HighlightText(presentation,text,resultFileName);
break;
case "REPLACE":
ReplaceText(presentation, text, newText, resultFileName);
break;
}
}
private static void HighlightText(Presentation presentation, string text, string resultFileName)
{
foreach (ISlide slide in presentation.Slides)
{
foreach (IShape shape in slide.Shapes)
{
if (shape is IAutoShape)
{
IAutoShape autoShape = shape as IAutoShape;
TextHighLightingOptions options = new TextHighLightingOptions();
options.WholeWordsOnly = true;
options.CaseSensitive = true;
autoShape.TextFrame.HighLightText(text, Color.Yellow, options);
}
}
}
presentation.SaveToFile(resultFileName+".pptx", FileFormat.Pptx2013);
}
private static void ReplaceText(Presentation presentation, string oldText, string newText, string resultFileName)
{
Dictionary tagValues = new Dictionary();
tagValues.Add(oldText,newText);
foreach (ISlide slide in presentation.Slides)
{
ReplaceTags(slide, tagValues);
}
presentation.SaveToFile(resultFileName + ".pptx", FileFormat.Pptx2013);
}
private static void ReplaceTags(ISlide pSlide, Dictionary TagValues)
{
foreach (IShape curShape in pSlide.Shapes)
{
if (curShape is IAutoShape)
{
foreach (TextParagraph tp in (curShape as IAutoShape).TextFrame.Paragraphs)
{
foreach (var curKey in TagValues.Keys)
{
if (tp.Text.Contains(curKey))
{
tp.Text = tp.Text.Replace(curKey, TagValues[curKey]);
}
}
}
}
}
}
}
}