使用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>和<分 ...
随机推荐
- python之redis
Redis简单介绍 如果简单地比较Redis与Memcached的区别,大多数都会得到以下观点:1 Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,zset,hash等数据结构 ...
- JS部分代码分享
2015-10-12 : 1.search()表达式:搜索索引 <!DOCTYPE html><html><body> <p>搜索字符串 "w ...
- (淘宝无限适配)手机端rem布局详解(转载非原创)
从网易与淘宝的font-size思考前端设计稿与工作流 本文结合自己对网易与淘宝移动端首页html元素上的font-size这个属性的思考与学习,讨论html5设计稿尺寸以及前端与设计之间协作流程的问 ...
- C#中的多线程 - 基础知识
原文:http://www.albahari.com/threading/ 文章来源:http://blog.gkarch.com/threading/part1.html 1简介及概念 C# 支持通 ...
- android中的HttpURLConnection和HttpClient实现app与pc数据交互
自学android的这几天很辛苦,但是很满足,因为每当学到一点点知识点都会觉得很开心,觉得今天是特别有意义的,可能这个就是一种莫名的热爱吧. 下面来说说今天学习的HttpURLConnection和H ...
- 《K&R》中引用的几个排序算法(shellsort、)以及一个自己乱写的排序
留待期末考后更新... void shellsort(int v[], int n) { int gap, i, j, temp; ; gap > ; gap /= ) for(i = gap; ...
- Shell 语法之结构化命令(流程控制)
许多程序在脚本命令之间需要某种逻辑流控制,允许脚本根据变量值的条件或者其他命令的结果路过一些命令或者循环执行这些命令.这些命令通常被称为结构化命令.和其他高级程序设计语言一样,shell提供了用来控制 ...
- 通过一行代码学习javascript
[].forEach.call($$("*"), function (a){ a.style.outline = "1px solid #"+(~~(Math. ...
- ABAP认识
ABAP是一种高级企业应用编程语言(Advanced Business Application Programming),起源于20世纪80年代.经过不断的发展,现在的版本为ABAP/4,SAP R/ ...
- javascript 技巧
1.将arguments转化为数组函数中的预定义变量arguments并非一个真正的数组,而是一个类似数组的对象. 它具有length属性,但是没有slice, push, sort等函数,那么如何使 ...