OpenOffice PDF 导出库

发布于 2024-08-14 04:26:18 字数 239 浏览 3 评论 0原文

我正在寻找一个库,它允许我将文本和图形输出渲染到 PDF 文档上。 (Cairo 肯定是一个选择。)我想知道 OpenOffice 如何编写 PDF 文件,看看我是否可以使用同一个图书馆。 OpenOffice 使用哪个库进行 PDF 导出?

编辑:我正在寻找 C 或 C++ 库。

I am looking for a library which will allow me to render text and graphics output onto a PDF document. (Cairo is certainly an option.) I would like to know how OpenOffice writes PDF files to see if I could use the same library. What library is being used by OpenOffice for PDF export?

Edit: I am looking for a C or C++ library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魄砕の薆 2024-08-21 04:26:18

我四处寻找如何使用 OpenOffice 将任何文档导出为 PDF。我终于在 OpenOffice 论坛中找到了一个隐藏的帖子,这让我成功了 90%。这是我的 100% 解决方案。适用于 OpenOffice 3.1。您必须安装 OpenOffice 才能使用此代码。您必须包含对 cli_basetypes、cli_cppuhelper、cli_oootypes、cli_ure、cli_uretypes 的引用。这些 dll 引用可在 OpenOffice SDK 中找到。抱歉,这是 C# 语言,而不是 C/C++ 语言。 HTH某人。

using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.beans;

public static void ConvertToPDF(string inputFile, string outputFile)
        {
            if (ConvertExtensionToFilterType(Path.GetExtension(inputFile)) == null)
                throw new InvalidProgramException("Unknown file type for OpenOffice. File = " + inputFile);

            StartOpenOffice();

            //Get a ComponentContext
            unoidl.com.sun.star.uno.XComponentContext xLocalContext =
               uno.util.Bootstrap.bootstrap();
            //Get MultiServiceFactory
            unoidl.com.sun.star.lang.XMultiServiceFactory xRemoteFactory =
               (unoidl.com.sun.star.lang.XMultiServiceFactory)
               xLocalContext.getServiceManager();
            //Get a CompontLoader
            XComponentLoader aLoader =
               (XComponentLoader)xRemoteFactory.createInstance("com.sun.star.frame.Desktop");
            //Load the sourcefile

            XComponent xComponent = null;
            try
            {
                xComponent = initDocument(aLoader,
                   PathConverter(inputFile), "_blank");
                //Wait for loading
                while (xComponent == null)
                {
                    System.Threading.Thread.Sleep(1000);
                }

                // save/export the document
                saveDocument(xComponent, inputFile, PathConverter(outputFile));

            }
            catch { throw; }
            finally { xComponent.dispose(); }

        }

        private static void StartOpenOffice()
        {
            Process[] ps = Process.GetProcessesByName("soffice.exe");
            if (ps != null)
            {
                if (ps.Length > 0)
                    return;
                else
                {
                    Process p = new Process();
                    p.StartInfo.Arguments = "-headless -nofirststartwizard";
                    p.StartInfo.FileName = "soffice.exe";
                    p.StartInfo.CreateNoWindow = true;
                    bool result = p.Start();
                    if (result == false)
                        throw new InvalidProgramException("OpenOffice failed to start.");
                }
            }
            else
            {
                throw new InvalidProgramException("OpenOffice not found.  Is OpenOffice installed?");
            }
        }

        private static XComponent initDocument(XComponentLoader aLoader, string file, string target)
        {
            PropertyValue[] openProps = new PropertyValue[1];
            openProps[0] = new PropertyValue();
            openProps[0].Name = "Hidden";
            openProps[0].Value = new uno.Any(true);


            XComponent xComponent = aLoader.loadComponentFromURL(
               file, target, 0,
               openProps);

            return xComponent;
        }


        private static void saveDocument(XComponent xComponent, string sourceFile, string destinationFile)
        {
            PropertyValue[] propertyValues = new PropertyValue[2];
            propertyValues = new PropertyValue[2];
            // Setting the flag for overwriting 
            propertyValues[1] = new PropertyValue();
            propertyValues[1].Name = "Overwrite";
            propertyValues[1].Value = new uno.Any(true);
            //// Setting the filter name 
            propertyValues[0] = new PropertyValue();
            propertyValues[0].Name = "FilterName";
            propertyValues[0].Value = new uno.Any(ConvertExtensionToFilterType(Path.GetExtension(sourceFile)));
            ((XStorable)xComponent).storeToURL(destinationFile, propertyValues);

        }


        private static string PathConverter(string file)
        {
            if (file == null || file.Length == 0)
                throw new NullReferenceException("Null or empty path passed to OpenOffice");

            return String.Format("file:///{0}", file.Replace(@"\", "/"));

        }

        public static string ConvertExtensionToFilterType(string extension)
        {
            switch (extension)
            {
                case ".doc":
                case ".docx":
                case ".txt":
                case ".rtf":
                case ".html":
                case ".htm":
                case ".xml":
                case ".odt":
                case ".wps":
                case ".wpd":
                    return "writer_pdf_Export";
                case ".xls":
                case ".xlsb":
                case ".ods":
                    return "calc_pdf_Export";
                case ".ppt":
                case ".pptx":
                case ".odp":
                    return "impress_pdf_Export";

                default: return null;
            }
        }


    }

I looked everywhere to find out how to export any document to PDF using OpenOffice. I finally found a buried post in the OpenOffice forums that got me 90% there. Here is my 100% solution. Works with OpenOffice 3.1. You must install OpenOffice to use this code. You must include references to cli_basetypes, cli_cppuhelper, cli_oootypes, cli_ure, cli_uretypes. These dll references are found in the OpenOffice SDK. Sorry, but this is in C#.. not C/C++. HTH someone.

using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.beans;

public static void ConvertToPDF(string inputFile, string outputFile)
        {
            if (ConvertExtensionToFilterType(Path.GetExtension(inputFile)) == null)
                throw new InvalidProgramException("Unknown file type for OpenOffice. File = " + inputFile);

            StartOpenOffice();

            //Get a ComponentContext
            unoidl.com.sun.star.uno.XComponentContext xLocalContext =
               uno.util.Bootstrap.bootstrap();
            //Get MultiServiceFactory
            unoidl.com.sun.star.lang.XMultiServiceFactory xRemoteFactory =
               (unoidl.com.sun.star.lang.XMultiServiceFactory)
               xLocalContext.getServiceManager();
            //Get a CompontLoader
            XComponentLoader aLoader =
               (XComponentLoader)xRemoteFactory.createInstance("com.sun.star.frame.Desktop");
            //Load the sourcefile

            XComponent xComponent = null;
            try
            {
                xComponent = initDocument(aLoader,
                   PathConverter(inputFile), "_blank");
                //Wait for loading
                while (xComponent == null)
                {
                    System.Threading.Thread.Sleep(1000);
                }

                // save/export the document
                saveDocument(xComponent, inputFile, PathConverter(outputFile));

            }
            catch { throw; }
            finally { xComponent.dispose(); }

        }

        private static void StartOpenOffice()
        {
            Process[] ps = Process.GetProcessesByName("soffice.exe");
            if (ps != null)
            {
                if (ps.Length > 0)
                    return;
                else
                {
                    Process p = new Process();
                    p.StartInfo.Arguments = "-headless -nofirststartwizard";
                    p.StartInfo.FileName = "soffice.exe";
                    p.StartInfo.CreateNoWindow = true;
                    bool result = p.Start();
                    if (result == false)
                        throw new InvalidProgramException("OpenOffice failed to start.");
                }
            }
            else
            {
                throw new InvalidProgramException("OpenOffice not found.  Is OpenOffice installed?");
            }
        }

        private static XComponent initDocument(XComponentLoader aLoader, string file, string target)
        {
            PropertyValue[] openProps = new PropertyValue[1];
            openProps[0] = new PropertyValue();
            openProps[0].Name = "Hidden";
            openProps[0].Value = new uno.Any(true);


            XComponent xComponent = aLoader.loadComponentFromURL(
               file, target, 0,
               openProps);

            return xComponent;
        }


        private static void saveDocument(XComponent xComponent, string sourceFile, string destinationFile)
        {
            PropertyValue[] propertyValues = new PropertyValue[2];
            propertyValues = new PropertyValue[2];
            // Setting the flag for overwriting 
            propertyValues[1] = new PropertyValue();
            propertyValues[1].Name = "Overwrite";
            propertyValues[1].Value = new uno.Any(true);
            //// Setting the filter name 
            propertyValues[0] = new PropertyValue();
            propertyValues[0].Name = "FilterName";
            propertyValues[0].Value = new uno.Any(ConvertExtensionToFilterType(Path.GetExtension(sourceFile)));
            ((XStorable)xComponent).storeToURL(destinationFile, propertyValues);

        }


        private static string PathConverter(string file)
        {
            if (file == null || file.Length == 0)
                throw new NullReferenceException("Null or empty path passed to OpenOffice");

            return String.Format("file:///{0}", file.Replace(@"\", "/"));

        }

        public static string ConvertExtensionToFilterType(string extension)
        {
            switch (extension)
            {
                case ".doc":
                case ".docx":
                case ".txt":
                case ".rtf":
                case ".html":
                case ".htm":
                case ".xml":
                case ".odt":
                case ".wps":
                case ".wpd":
                    return "writer_pdf_Export";
                case ".xls":
                case ".xlsb":
                case ".ods":
                    return "calc_pdf_Export";
                case ".ppt":
                case ".pptx":
                case ".odp":
                    return "impress_pdf_Export";

                default: return null;
            }
        }


    }
锦上情书 2024-08-21 04:26:18

您使用什么语言工作?有很多 PDF 库。在 Stack Overflow 中搜索“pdf 库 [编程语言]”。已经有很多建议了。

OpenOffice 使用 Sun PDF 库 作为导入 PDF 的扩展,但我不这样做确定它用什么来导出它们。

What language are you working in? There are many PDF libraries out there. Search Stack Overflow for "pdf library [programming language]". There's tons of recommendations already.

OpenOffice uses the Sun PDF library as an extension to import PDFs, but I'm not sure what it uses to export them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文