在你认为“不是另一个这样的问题”之前,请先阅读这个.
我有一个应用程序(ASP.NET MVC 3中的Web应用程序)使用DocX library在DocX中生成Word文件.
应用程序需要一个模板,并使用数据库中的所有数据填充它.
现在我想创建一个创建的docx文件的PDF版本.
我知道apose.word是一个选择,但不是因为我没有预算.其他我必须花一些钱的库也是不成问题的.
我没有一个共享点服务器,所以Word Automation Services也不是一个选项.
所以我有2个选项(我知道)离开,他们都使用iTextSharp.不知道哪个更好.
>我可以使用docx文件生成的XML,并将其转换为iTextSharp可用的版本.
>我可以创建PDF,就像我用一个模板创建一个docx.
任何人都有任何想法,它是多少工作,哪些2有更好的表现,如果甚至可以做.
我知道第二个选项有缺点,当我更改模板时,我必须为这两个版本更改它.
解决方法
另一个选项,即使它需要一些工作:在服务器上安装OpenOffice,并使用UNO库(包括它们作为应用程序中的程序集),您可以打开docx文档并将其直接保存在PDF中.
几分钟后我发了一个例子…
几分钟后我发了一个例子…
部分示例:
这是我很久以前创建的一个类,用于将文件转换为pdf
using unoidl.com.sun.star.lang; using unoidl.com.sun.star.uno; using unoidl.com.sun.star.container; using unoidl.com.sun.star.frame; using unoidl.com.sun.star.beans; using unoidl.com.sun.star.view; using System.Collections.Generic; using System.IO; namespace QOpenOffice { public enum AppType { Writer,Calc,Impress,Draw,Math } public enum ExportFilter{ Word97,WriterPDF,CalcPDF,DrawPDF,ImpressPDF,MathPDF } class OpenOffice { private XComponentContext context; private XMultiServiceFactory service; private XComponentLoader component; private XComponent doc; private List<string> filters = new List<string>(); #region Constructors public OpenOffice() { /// This will start a new instance of OpenOffice.org if it is not running,/// or it will obtain an existing instance if it is already open. context = uno.util.Bootstrap.bootstrap(); /// The next step is to create a new OpenOffice.org service manager service = (XMultiServiceFactory)context.getServiceManager(); /// Create a new Desktop instance using our service manager component = (XComponentLoader)service.createInstance("com.sun.star.frame.Desktop"); // Getting filters XNameContainer filters = (XNameContainer)service.createInstance("com.sun.star.document.FilterFactory"); foreach (string filter in filters.getElementNames()) this.filters.Add(filter); } ~OpenOffice() { if (doc != null) doc.dispose(); doc = null; } #endregion #region Private methods private string FilterToString(ExportFilter filter) { switch (filter) { case ExportFilter.Word97: return "MS Word 97"; case ExportFilter.WriterPDF: return "writer_pdf_Export"; case ExportFilter.CalcPDF: return "calc_pdf_Export"; case ExportFilter.DrawPDF: return "draw_pdf_Export"; case ExportFilter.ImpressPDF: return "impress_pdf_Export"; case ExportFilter.MathPDF: return "math_pdf_Export"; } return ""; } #endregion #region Public methods public bool Load(string filename,bool hidden) { return Load(filename,hidden,"",""); } public bool Load(string filename,bool hidden,int filter_index,string filter_options) { return Load(filename,filters[filter_index],filter_options); } public bool Load(string filename,string filter_name,string filter_options) { List<PropertyValue> pv = new List<PropertyValue>(); pv.Add(new PropertyValue("Hidden",new uno.Any(hidden),PropertyState.DIRECT_VALUE)); if (filter_name != "") { pv.Add(new PropertyValue("FilterName",new uno.Any(filter_name),PropertyState.DIRECT_VALUE)); pv.Add(new PropertyValue("FilterOptions",new uno.Any(filter_options),PropertyState.DIRECT_VALUE)); } try { doc = component.loadComponentFromURL( "file:///" + filename.Replace('\\','/'),"_blank",pv.ToArray()); return true; } catch { doc = null; return false; } } public bool Print() { return Print(1,""); } public bool Print(int copies,string pages) { List<PropertyValue> pv = new List<PropertyValue>(); pv.Add(new PropertyValue("CopyCount",new uno.Any(copies),PropertyState.DIRECT_VALUE)); if (pages != "") pv.Add(new PropertyValue("Pages",new uno.Any(pages),PropertyState.DIRECT_VALUE)); //if (doc is XPrintable) try { ((XPrintable)doc).print(pv.ToArray()); return true; } catch { return false; } } public bool Save(string filename,ExportFilter filter) { return Save(filename,FilterToString(filter)); } public bool Save(string filename,string filter) { List<PropertyValue> pv = new List<PropertyValue>(); pv.Add(new PropertyValue("FilterName",new uno.Any(filter),PropertyState.DIRECT_VALUE)); pv.Add(new PropertyValue("Overwrite",new uno.Any(true),PropertyState.DIRECT_VALUE)); try { filename = filename.Replace("\\","/"); ((XStorable)doc).storeToURL("file:///" + filename,pv.ToArray()); return true; } catch { return false; } } public bool ExportToPdf(string filename) { filename = Path.ChangeExtension(filename,".pdf"); bool ret = Save(filename,"writer_pdf_Export"); if (!ret) ret = Save(filename,"impress_pdf_Export"); if (!ret) ret = Save(filename,"calc_pdf_Export"); if (!ret) ret = Save(filename,"draw_pdf_Export"); if (!ret) ret = Save(filename,"math_pdf_Export"); return ret; } public void Close() { doc.dispose(); doc = null; } public bool New(AppType app,bool hidden) { try { string sapp = "private:factory/"; switch (app) { case AppType.Writer: sapp += "swriter"; break; case AppType.Calc: sapp += "scalc"; break; case AppType.Impress: sapp += "simpress"; break; case AppType.Draw: sapp += "sdraw"; break; case AppType.Math: sapp += "smath"; break; } PropertyValue pv = new PropertyValue("Hidden",PropertyState.DIRECT_VALUE); doc = component.loadComponentFromURL(sapp,new PropertyValue[1] { pv }); return true; } catch { doc = null; return false; } } #endregion #region Properties public List<string> Filters { get { return filters; } } #endregion } }