使用OpenXML操作Office文档
使用OpenXML类库, 以编程的方式来访问PowerPoint, Word, Excel等文档, 有时能够为我们批量编辑文档提供方便。
最近项目中遇到的两个任务是: 1. 替换文档中的图片的Alt Text信息。2. 替换文档中超级链接的ScreenTip信息。 这里的文档是PPT和Word。如果要手动打开Word文档, 然后一个一个图片进行替换, 挺浪费时间的, 其次, Word也没有提供图片Alt Text的查找替换功能, 所以, 就想编程实现批量查找和替换。
首先, 安装OpenXML SDK, 通过Nuget Manager.
Install-Package DocumentFormat.OpenXml
然后加入对命名空间的引用。
using DocumentFormat.OpenXml.Office;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Drawing.Wordprocessing;
在Word中, Image图片的存储XML文档格式如下:
<w:drawing>
<wp:inline distT="0" distB="0" distL="0" distR="0">
<wp:extent cx="2743438" cy="2792210"/>
<wp:effectExtent l="0" t="0" r="0" b="8255"/>
<wp:docPr id="1" name="Picture 1" descr="this is alt text description of horse icon." title="title of horse Icon"/>
<wp:cNvGraphicFramePr>
<a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
</wp:cNvGraphicFramePr>
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPr id="1" name="1.png"/>
<pic:cNvPicPr/>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="rId6">
<a:extLst>
<a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
<a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>
</a:ext>
</a:extLst>
</a:blip>
<a:stretch>
<a:fillRect/>
</a:stretch>
</pic:blipFill>
<pic:spPr>
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="2743438" cy="2792210"/>
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst/>
</a:prstGeom>
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
Word实现查找图片Alt Text的代码:
using (WordprocessingDocument doc = WordprocessingDocument.Open(file, false))
{
MainDocumentPart mainPart = doc.MainDocumentPart; StringBuilder sb = new StringBuilder(); var imageParts = mainPart.ImageParts;
int imageIndex = ;
foreach (var image in imageParts)
{
imageIndex++;
string id = mainPart.GetIdOfPart(image);
var drawings = mainPart.Document.Body.Descendants<Drawing>();
string title = string.Empty;
string description = string.Empty;
foreach (var drawing in drawings)
{
if (drawing.Descendants<DocumentFormat.OpenXml.Drawing.Blip>().FirstOrDefault() != null &&
drawing.Descendants<DocumentFormat.OpenXml.Drawing.Blip>().FirstOrDefault().Embed == id)
{
title = drawing.Descendants<Inline>().First().DocProperties.Title != null ? drawing.Descendants<Inline>().First().DocProperties.Title.ToString() : string.Empty;
description = drawing.Descendants<Inline>().First().DocProperties.Description != null ? drawing.Descendants<Inline>().First().DocProperties.Description.ToString() : string.Empty;
}
} sb.AppendLine(string.Format("{0}: {1}={2}", imageIndex, title, description));
}
PPT实现图片Alt Text的查找代码如下:
using (PresentationDocument doc = PresentationDocument.Open(file, false))
{
StringBuilder sb = new StringBuilder(); int sdIndex = ;
foreach (var part in doc.PresentationPart.SlideParts)
{
sdIndex++;
int picIndex = ;
var imageParts = part.GetPartsOfType<ImagePart>();
foreach (var imagePart in imageParts)
{
picIndex++;
var picture = part.Slide.Descendants<DocumentFormat.OpenXml.Presentation.Picture>().Where(p =>
p.BlipFill.Blip.Embed == part.GetIdOfPart(imagePart)).FirstOrDefault();
var title = picture.NonVisualPictureProperties.NonVisualDrawingProperties.Title;
var description = picture.NonVisualPictureProperties.NonVisualDrawingProperties.Description;
sb.AppendLine(string.Format("{0}-{1}: {2}={3}", sdIndex, picIndex, title, description));
}
} textToReturn = sb.ToString();
}
在Word中, 超级链接的XML代码是:
<w:hyperlink r:id="rId7" w:tooltip="Baidu Company" w:history="1">
<w:r w:rsidR="00007220" w:rsidRPr="00FC5FDE">
<w:rPr>
<w:rStyle w:val="Hyperlink"/>
</w:rPr>
<w:t>www.baidu.com</w:t>
</w:r>
</w:hyperlink>
word实现超链接ScreenTip的查找:
using (WordprocessingDocument doc = WordprocessingDocument.Open(file, false))
{
MainDocumentPart mainPart = doc.MainDocumentPart;
var hyperlinks = mainPart.Document.Body.Descendants<Hyperlink>(); StringBuilder sb = new StringBuilder(); int hlIndex = ;
foreach (var hyperlink in hyperlinks)
{
hlIndex++;
string url = string.Empty; var hyperlinkRelationships = mainPart.HyperlinkRelationships;
foreach (var item in hyperlinkRelationships)
{
if (item.Id == hyperlink.Id)
{
url = item.Uri.OriginalString;
break;
}
} string toolTip = hyperlink.Tooltip;
sb.AppendLine(string.Format("{0}: {1}={2}", hlIndex, url, toolTip));
} textToReturn = sb.ToString(); }
PPT实现超链接ScreenTip的查找:
using (PresentationDocument doc = PresentationDocument.Open(file, false))
{
StringBuilder sb = new StringBuilder(); int sdIndex = ;
foreach (var part in doc.PresentationPart.SlideParts)
{
sdIndex++;
var hyperLinks = part.Slide.Descendants<DocumentFormat.OpenXml.Drawing.HyperlinkType>();
int hlIndex = ;
foreach (var hyperLink in hyperLinks)
{
hlIndex++;
string url = string.Empty;
foreach (var item in part.HyperlinkRelationships)
{
if (item.Id == hyperLink.Id)
{
url = item.Uri.Authority;
break;
}
}
var tooTip = hyperLink.Tooltip; sb.AppendLine(string.Format("{0}-{1}: {2}={3}", sdIndex, hlIndex, url, tooTip));
} } textToReturn = sb.ToString();
}
使用OpenXML操作Office文档的更多相关文章
- apache poi操作office文档----java在线预览txt、word、ppt、execel,pdf代码
在页面上显示各种文档中的内容.在servlet中的逻辑 word: BufferedInputStream bis = null; URL url = null; HttpURLConnectio ...
- 黄聪:利用OpenXml生成Word2007文档(转)
原文:http://blog.csdn.net/francislaw/article/details/7568317 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 一Op ...
- 利用OpenXml生成Word2007文档
一.OpenXml简介 利用C#生成Word文档并非一定要利用OpenXml技术,至少可以使用微软提供的Office相关组件来编程,不过对于Office2007(确切的说是Word.Excel和Pow ...
- Java实现office文档与pdf文档的在线预览功能
最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...
- 基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览
在博客园很多文章里面,曾经有一些介绍Office文档预览查看操作的,有些通过转为PDF进行查看,有些通过把它转换为Flash进行查看,但是过程都是曲线救国,真正能够简洁方便的实现Office文档的预览 ...
- 怎么给我的Office文档加密
很多的用户朋友都可以熟练的使用office中的Word.Excel和PowerPoint文档,但大家对Office文档加密方式了解的并不多.Advanced Office Password Recov ...
- [转载]基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览
在博客园很多文章里面,曾经有一些介绍Office文档预览查看操作的,有些通过转为PDF进行查看,有些通过把它转换为Flash进行查看,但是过程都是曲线救国,真正能够简洁方便的实现Office文档的预览 ...
- java将office文档pdf文档转换成swf文件在线预览
第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux,solaris等操作系统上执行. 主要模块有writer(文 ...
- 海量Office文档搜索
知识管理系统Data Solution研发日记之十 海量Office文档搜索 经过前面两篇文章的介绍,<分享制作精良的知识管理系统 博客备份程序 Site Rebuild>和<分 ...
随机推荐
- 。【自学总结 3】------3ds Max 主工具栏
1.选择并链接:选择对象,使其和其他对象建立父子关系. 2.断开选择链接:撤销链接关系. 3.选择过滤器列表:可以限制要选择的对象的特定类型和组合.例如,我们选择几何体后,其他对象就无法被选择. 4. ...
- 405 Method Not Allowed
今天在发布一个网站的时候遇到 标题上的问题,一直不明白是为何,刚开始以为是我的程序写的有问题,随即将项目发给同事来发布试试,在他的IIS上发布却没出现问题,一切正常,这可就怪了,于是想到了应该是IIS ...
- Dialog , ProgressDialog , PopWindow 区别
本质区别: Dialog:非阻塞对话框,弹出对话框时时,后台还可以做事情,点击背景时,对话框消失 ProgressDialog:带有圆形进度或者条形进度的对话框,一般结合handler使用.任务完成后 ...
- 安装openJDK 8
1.JDK 8 示例 (1.1)Debian, Ubuntu等使用下述安装命令: $ -jre ps : openjdk-8-jre 仅包含JRE,如果需要开发java程序,需要下载openjdk-8 ...
- 深入浅出设计模式——访问者模式(Visitor Pattern)
模式动机 对于系统中的某些对象,它们存储在同一个集合中,且具有不同的类型,而且对于该集合中的对象,可以接受一类称为访问者的对象来访问,而且不同的访问者其访问方式有所不同,访问者模式为解决这类问题而诞生 ...
- HDU-4528 小明系列故事——捉迷藏 BFS模拟
题意:链接 分析:每一个D或者是E点往四面延伸,并且赋一个特殊的值,能看到D点的点赋值为1,能看到E点的点赋值为1000,这是因为最多100步,因此最后可以根据除以1000和对1000取模来得出某个状 ...
- 【hdu5973】高精度威佐夫博弈
题意:输入a, b表示两堆石头数目,威佐夫博弈,问:先手胜负? a, b <= 1e100. 高精度.当a > b时, a = (a-b)*黄金分割比 时是先手败状态.因为a, b < ...
- angularJS学习笔记之——搭建学习环境
学习AngularJS已经好几天了,从今天开始学习AngularJS环境搭建. 无论是Mac.Linux或Windows环境中,您均可遵循本教程学习编程. 第一步:安装Git Git是什么呢? Git ...
- 解决:“java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut myMethod”问题!
Spring版本:2.5.6 AspectJ是Spring自带的lib. Jdk版本:1.7.0_17 在配置没问题的情况下,报:java.lang.IllegalArgumentException: ...
- page,client,offset区别
offset:相对于当前“盒子”的距离 ,与滚动条无关 client:相对于可视区域的距离,与滚动条无关 page:相对于整个页面的距离,与滚动条有关 示例代码: <!DOCTYPE html& ...