简体   繁体   English

以编程方式将Word(docx)转换为PDF

[英]Programmatically convert Word (docx) to PDF

Ok before you think "Not another question like this" please read this first. 好吧,在你想到“不是这样的另一个问题”之前,请先阅读此内容。

I have an application (web application in ASP.NET MVC 3) the generates Word files in DocX using the DocX library . 我有一个应用程序(ASP.NET MVC 3中的Web应用程序)使用DocX库在DocX中生成Word文件。

The application takes a template and fills it in with all the data from a database. 应用程序采用模板并使用数据库中的所有数据填充它。 Now I want to create a PDF version of that created docx-file. 现在我想创建一个创建的docx文件的PDF版本。

I know apose.word is an option, but not for me since I have little budget. 我知道箴言是一种选择,但不适合我,因为我的预算很少。 Other libs where I have to spend some money on are also out of the question. 其他我需要花钱的库也是不可能的。

I don't have a sharepoint server so Word Automation Services isn't an option either. 我没有sharepoint服务器,因此Word Automation Services也不是一个选项。

So I have 2 options (that I know) left and they both use iTextSharp . 所以我有两个选项(我知道),他们都使用了iTextSharp Don't know which is better. 不知道哪个更好。

  1. I could use the generated XML from the docx file and transform it to a version that is usable by iTextSharp. 我可以使用docx文件中生成的XML并将其转换为iTextSharp可用的版本。
  2. I could create the PDF like I create the docx with a template. 我可以创建PDF,就像我用模板创建docx一样。

Anybody has any idea on how much work it is, which of those 2 has better performance and if it is even possible to do. 任何人都知道它有多少工作,哪两个有更好的性能,如果有可能的话。

I know that the second option has the downside that when I change a template I have to change it for both versions. 我知道第二个选项的缺点是,当我更改模板时,我必须为两个版本更改它。

If you have a better solution (free that is), you are welcome to share it. 如果您有更好的解决方案(免费),欢迎您分享。

Another option, even if it needs some work: install OpenOffice on server and, using UNO libraries (including them as assemblies in your app), you can open docx document and save it in PDF directly. 另一个选项,即使它需要一些工作:在服务器上安装OpenOffice,并使用UNO库(包括它们作为应用程序中的程序集),您可以打开docx文档并直接将其保存为PDF。
In a few minutes I post an example... 几分钟后我发布了一个例子......

PARTIAL EXAMPLE: 部分示例:
This is a class I created a long time ago and used to convert files to 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, hidden, filters[filter_index], filter_options);
        }
        public bool Load(string filename, bool hidden, string filter_name, string filter_options)
        {
            List<PropertyValue> pv = new List<PropertyValue>();
            pv.Add(new PropertyValue("Hidden", 0, new uno.Any(hidden), PropertyState.DIRECT_VALUE));
            if (filter_name != "")
            {
                pv.Add(new PropertyValue("FilterName", 0, new uno.Any(filter_name), PropertyState.DIRECT_VALUE));
                pv.Add(new PropertyValue("FilterOptions", 0, new uno.Any(filter_options), PropertyState.DIRECT_VALUE));
            }

            try
            {
                doc = component.loadComponentFromURL(
                    "file:///" + filename.Replace('\\', '/'), "_blank",
                    0, 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", 0, new uno.Any(copies), PropertyState.DIRECT_VALUE));
            if (pages != "")
                pv.Add(new PropertyValue("Pages", 0, 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", 0, new uno.Any(filter), PropertyState.DIRECT_VALUE));
            pv.Add(new PropertyValue("Overwrite", 0, 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, "impress_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", 0, new uno.Any(hidden), PropertyState.DIRECT_VALUE);
                doc = component.loadComponentFromURL(sapp, "_blank", 0, new PropertyValue[1] { pv });
                return true;
            }
            catch
            {
                doc = null;
                return false;
            }
        }
        #endregion


        #region Properties
        public List<string> Filters
        {
            get { return filters; }
        }
        #endregion
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM