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

推荐订阅源

G
Google Developers Blog
宝玉的分享
宝玉的分享
月光博客
月光博客
B
Blog
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
N
Netflix TechBlog - Medium
博客园_首页
GbyAI
GbyAI
人人都是产品经理
人人都是产品经理
A
About on SuperTechFans
Y
Y Combinator Blog
L
LangChain Blog
有赞技术团队
有赞技术团队
D
Docker
爱范儿
爱范儿
博客园 - 司徒正美
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net

博客园 - 蒜头

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是一样的