CSharp自定义文件的属性
来源:互联网 发布:java分层数据的储存 编辑:程序博客网 时间:2024/06/05 05:19
How to set custom attributes / file properties on a file in C# .Net
In the office here we have developed a document management system, which uses Custom Properties on PDF and DWG files to keep a track of what revision etc they are. In my Revit API development, we required an easy way of settings these from inside of Revit, so I set about creating a plotting external command using the new Print methods in the 2009 API and PDF995's ini file.
Once I'd printed the documents, I wanted to rename them and set the custom properties such as Revision, drawer, designer etc. After much searching, I discovered what you need to get is "DSOFile" from Microsoft, it is intended for editing Microsoft Word document properties, but works on anything. There is an easy to understand VB.Net example included in the download.
DSO File works by adding a reference to the DLL and creating a new DSOFile.OleDocumentPropertiesClass, and then performing actions on that. So, I created a new: private DSOFile.OleDocumentPropertiesClass m_file; Then in my constructor for my ParamaterSetter class I created an instance of OleDocumentPropertiesClass and then called the Open() Method with my filename:
//create new isntance of OleDocumentPropertiesClass
m_file = new DSOFile.OleDocumentPropertiesClass();
// select the file you would like to open
m_file.Open(path, false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);
From there you can perform actions such as : m_file.CustomProperties.Add(key, ref valueForKey); where key is the name of your property, and valueForKey is the value. For more standard properties, such as the "Summary" properties (in summary tab when you go properties on the file), you can access them directly, such as: m_file.SummaryProperties.Comments = value;
For reference, here is the utility class I use:
class FileParameterSetter { //DSOFile document private DSOFile.OleDocumentPropertiesClass m_file; public FileParameterSetter(string path) { //create new isntance of OleDocumentPropertiesClass m_file = new DSOFile.OleDocumentPropertiesClass(); // select the file you would like to open m_file.Open(path, false, DSOFile.dsoFileOpenOptions.dsoOptionDefault); } /// <summary> /// Sets a custom property in the file. /// </summary> /// <param name="key">key to set</param> /// <param name="value">value to set to key</param> public void SetCustomproperty(string key, string value) { try { object valueForKey; //check for valid inputs if ((key == null) || (value == null) || (key.Length == 0) || (value.Length == 0)) { //Error, no key or value. return; } valueForKey = value; //write to file m_file.CustomProperties.Add(key, ref valueForKey); //save change m_file.Save(); } catch(Exception ex) { System.Windows.Forms.MessageBox.Show("There was an error adding properties to PDF file: " + ex); } } /// <summary> /// Saves and closes document /// </summary> public void Close() { m_file.Close(true); } /// <summary> /// Sets a standard property, such as Comments, Subject, Title etc /// </summary> /// <param name="key"></param> /// <param name="value"></param> public void SetStandardProperty(string key, string value) { if (key.Equals("Comments")) { m_file.SummaryProperties.Comments = value; } if (key.Equals("Subject")) { m_file.SummaryProperties.Subject = value; } if (key.Equals("Title")) { m_file.SummaryProperties.Title = value; } } }
- CSharp自定义文件的属性
- 去掉自定义文件的自定义属性
- 自定义属性之xml文件中自定义的属性
- 自定义layout中布局文件的属性
- 自定义属性,style文件的配置
- 【km笔记】CSharp--类的属性和字段
- 自定义获取属性文件
- csharp 读写xml文件
- CSharp学习笔记之八 文件的读写
- 【CSharp 学习笔记】关于ini文件的操作
- 一个自定义的读取属性文件的类
- 自定义标签tag文件中,rtexprvalue属性的作用
- pulltorefresh(二)的自定义属性介绍、attrs文件介绍
- JSLink文件中使用field的自定义属性
- pulltorefresh(二)的自定义属性介绍、attrs文件介绍
- pulltorefresh(二)的自定义属性介绍、attrs文件介绍
- pulltorefresh(二)的自定义属性介绍、attrs文件介绍
- 使用css3的content属性自定义上传文件样式
- jstl中c标签if-else
- java线程继承Thread方法和实现Runable方法区别
- 2. Add Two Numbers
- Python进阶之“属性(property)”详解
- jquery获取和设置select选中的值/文本/索引及选中事件
- CSharp自定义文件的属性
- nginx配置Laravel
- Android设计模式之工厂模式 Factory
- 密码学
- Windows版OpenVPN安装、配置教程
- 成都链家数据爬取分析<初稿>
- scrum中的四个会议
- 解决BurpSuite不能抓本地包的问题
- HP pavilion 15-cc笔记本电脑安装ubuntu的一些折腾事儿