惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
A
About on SuperTechFans
IT之家
IT之家
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Vercel News
Vercel News
G
Google Developers Blog
J
Java Code Geeks
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
Cloudbric
Cloudbric
L
LINUX DO - 最新话题
MyScale Blog
MyScale Blog
H
Heimdal Security Blog
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
Latest news
Latest news
I
Intezer
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
月光博客
月光博客
T
Threatpost
博客园 - 【当耐特】
S
Schneier on Security
P
Privacy International News Feed
G
GRAHAM CLULEY
T
Tenable Blog
AWS News Blog
AWS News Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
博客园 - Franky
Engineering at Meta
Engineering at Meta
美团技术团队
S
Secure Thoughts
T
Troy Hunt's Blog
Microsoft Security Blog
Microsoft Security Blog
SecWiki News
SecWiki News
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Martin Fowler
Martin Fowler
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 蒜头

Mailbox unavailable. The server response was: 5.7.1 Unable to relay for (email address). SQL Server 2005中xml类型和函数的简单应用 部分Office 2007文件格式转换为xps和pdf代码整理 Image.FromFile gives "Out of Memory" Exception for icon - 蒜头 简单介绍PDF,XPS,Images,Office 2007之间的转换方法 - 蒜头 - 博客园 SQL 2005 全文检索(续) 简单实现C#生成Excel 2007文件并下载 简单应用ReportViewer控件 配置SQL Server Session方法 采用负载均衡,部署了两个ASP.NET 2.0的站点服务器碰到的问题 初试VSTS 2008(TFS安装) Windows Server 2003分区修改方法[转载] 制作VSTO 2005 SE开发的Office 2007 AddIn的安装包 一个简单的document library event handler VS 2005 SP1 安装错误 [续] 一个简单的Checkbox Custom Field Type ASP.NET 2.0 SQL Cache 配置方法 解读Document Library关于权限的对象模型 SharePoint应用AJAX.NET和AJAX Control Toolkit
再谈Images到xps,pdf的转换
蒜头 · 2008-05-30 · via 博客园 - 蒜头

转换,实际上是将图片加入到xps,pdf文件中,不是真正意义的格式转换,在这篇blog中已经介绍了如何将图片转换为pdf。下面介绍图片到xps的转换,这里使用了.NET Framework3.0中的新功能,.NET 3.0中有对xps文件支持的类,xps也是OpenXML格式,
首先添加对.NET 3.0中的两个程集的引用ReachFramework,Windowbase,
转换代码代码如下:

        protected bool Convert(string sourcePath, string targetPath)
        
{
            Image img 
= Image.FromFile(sourcePath);

            
string resFile = sourcePath;
            XpsDocument xpsDoc 
= new XpsDocument(targetPath, FileAccess.ReadWrite);
            IXpsFixedDocumentSequenceWriter fds 
= xpsDoc.AddFixedDocumentSequence();
            IXpsFixedDocumentWriter fd 
= fds.AddFixedDocument();
            IXpsFixedPageWriter fp 
= fd.AddFixedPage();
            XpsResource res 
= null;
            XpsResource thumb 
= null;

            res 
= fp.AddImage(XpsImageType.JpegImageType);
            thumb 
= xpsDoc.AddThumbnail(XpsImageType.JpegImageType);

            WriteStream(res.GetStream(), resFile);
            WritePageContent(fp.XmlWriter, res, img.Width, img.Height);
            res.Commit();

            WriteStream(thumb.GetStream(), resFile);
            thumb.Commit();

            fp.Commit();
            fd.Commit();
            fds.Commit();
            xpsDoc.Close();
            
return true;
        }


        
private static void WritePageContent(System.Xml.XmlWriter xmlWriter, XpsResource res, int width, int height)
        
{
            xmlWriter.WriteStartElement(
"FixedPage");
            xmlWriter.WriteAttributeString(
"xmlns"@"http://schemas.microsoft.com/xps/2005/06");
            xmlWriter.WriteAttributeString(
"Width", width.ToString());
            xmlWriter.WriteAttributeString(
"Height", height.ToString());
            xmlWriter.WriteAttributeString(
"xml:lang""en-US");
            xmlWriter.WriteStartElement(
"Canvas");

            
if (res is XpsImage)
            
{
                xmlWriter.WriteStartElement(
"Path");
                xmlWriter.WriteAttributeString(
"Data""M 20,20 L 770,20 770,770 20,770 z");
                xmlWriter.WriteStartElement(
"Path.Fill");
                xmlWriter.WriteStartElement(
"ImageBrush");
                xmlWriter.WriteAttributeString(
"ImageSource", res.Uri.ToString());
                xmlWriter.WriteAttributeString(
"Viewbox""0,0,750,750");
                xmlWriter.WriteAttributeString(
"ViewboxUnits""Absolute");
                xmlWriter.WriteAttributeString(
"Viewport""20,20,750,750");
                xmlWriter.WriteAttributeString(
"ViewportUnits""Absolute");
                xmlWriter.WriteEndElement();
                xmlWriter.WriteEndElement();
                xmlWriter.WriteEndElement();
            }

            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndElement();
        }


        
private static void WriteStream(Stream stream, string resFile)
        
{
            
using (FileStream sourceStream = new FileStream(resFile, FileMode.Open, FileAccess.Read))
            
{
                
byte[] buf = new byte[1024];
                
int read = 0;
                
while ((read = sourceStream.Read(buf, 0, buf.Length)) > 0)
                
{
                    stream.Write(buf, 
0, read);
                }

            }

        }


这样同一个图片得到的xps和pdf差异比较大,

当然,我们通过调整代码,也可以得到相近的效果,但需要时间,这里想到个折中的办法,Office 2007可以通过AddIn将文档转换为xps和pdf,通过它得到xps和pdf应该是一样的。所以,这里我们先将图片添加到一个word文档中,再通过office 2007得到xps和pdf,在上篇blog中的代码再添加将图片添加到word文档的代码:

                wordDocument = wordApplication.Documents.Add(ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing);

                wordDocument.InlineShapes.AddPicture(sourcePath, 
ref paramMissing, ref paramMissing, ref paramMissing);

                
object a = Path.GetTempPath() + Path.GetTempFileName() + ".docx";
                
object format = Word.WdSaveFormat.wdFormatDocumentDefault;
                wordDocument.SaveAs(
ref a, ref format,
                    
ref paramMissing, ref paramMissing, ref paramMissing,
                    
ref paramMissing, ref paramMissing, ref paramMissing,
                    
ref paramMissing, ref paramMissing, ref paramMissing,
                    
ref paramMissing, ref paramMissing, ref paramMissing,
                    
ref paramMissing, ref paramMissing);


添加了图片后的word文档先要保存到磁盘,然后,再转换为xps和pdf,否则会打开一个word窗口。这样我得到xps和pdf是一样的