2

我正在寻找一个库,它允许我将文本和图形输出呈现到 PDF 文档中。(开罗当然是一个选择。)我想知道 OpenOffice 如何编写 PDF 文件,看看我是否可以使用同一个库。OpenOffice 使用什么库进行 PDF 导出?

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

4

3 回答 3

7

我到处寻找如何使用 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++ 中。某人。

使用 unoidl.com.sun.star.lang;
使用 unoidl.com.sun.star.uno;
使用 unoidl.com.sun.star.bridge;
使用 unoidl.com.sun.star.frame;
使用 unoidl.com.sun.star.beans;

公共静态无效 ConvertToPDF(字符串输入文件,字符串输出文件)
        {
            if (ConvertExtensionToFilterType(Path.GetExtension(inputFile)) == null)
                throw new InvalidProgramException("OpenOffice 的未知文件类型。File = " + inputFile);

            StartOpenOffice();

            //获取一个组件上下文
            unoidl.com.sun.star.uno.XComponentContext xLocalContext =
               uno.util.Bootstrap.bootstrap();
            //获取多服务工厂
            unoidl.com.sun.star.lang.XMultiServiceFactory xRemoteFactory =
               (unoidl.com.sun.star.lang.XMultiServiceFactory)
               xLocalContext.getServiceManager();
            //获取一个组件加载器
            XComponentLoader aLoader =
               (XComponentLoader)xRemoteFactory.createInstance("com.sun.star.frame.Desktop");
            //加载源文件

            XComponent xComponent = null;
            尝试
            {
                xComponent = initDocument(aLoader,
                   路径转换器(输入文件),“_blank”);
                //等待加载
                而(xComponent == null)
                {
                    System.Threading.Thread.Sleep(1000);
                }

                // 保存/导出文档
                saveDocument(xComponent, inputFile, PathConverter(outputFile));

            }
            抓住 { 扔;}
            最后 { xComponent.dispose(); }

        }

        私有静态无效 StartOpenOffice()
        {
            进程[] ps = Process.GetProcessesByName("soffice.exe");
            如果(ps!= null)
            {
                如果(ps.长度> 0)
                    返回;
                别的
                {
                    进程 p = new Process();
                    p.StartInfo.Arguments = "-headless -nofirststartwizard";
                    p.StartInfo.FileName = "soffice.exe";
                    p.StartInfo.CreateNoWindow = true;
                    布尔结果 = p.Start();
                    如果(结果 == 假)
                        throw new InvalidProgramException("OpenOffice 启动失败。");
                }
            }
            别的
            {
                throw new InvalidProgramException("未找到 OpenOffice。是否安装了 OpenOffice?");
            }
        }

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


            XComponent xComponent = aLoader.loadComponentFromURL(
               文件,目标,0,
               开放道具);

            返回 x 组件;
        }


        private static void saveDocument(XComponent xComponent, string sourceFile, string destinationFile)
        {
            属性值 [] 属性值 = 新属性值 [2];
            属性值 = 新属性值 [2];
            // 设置覆盖标志
            属性值[1] = 新属性值();
            propertyValues[1].Name = "覆盖";
            propertyValues[1].Value = new uno.Any(true);
            //// 设置过滤器名称
            属性值[0] = 新属性值();
            propertyValues[0].Name = "过滤器名称";
            propertyValues[0].Value = new uno.Any(ConvertExtensionToFilterType(Path.GetExtension(sourceFile)));
            ((XStorable)xComponent).storeToURL(destinationFile, propertyValues);

        }


        私有静态字符串 PathConverter(字符串文件)
        {
            if (file == null || file.Length == 0)
                throw new NullReferenceException("传递给 OpenOffice 的 Null 或空路径");

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

        }

        公共静态字符串 ConvertExtensionToFilterType(字符串扩展)
        {
            开关(扩展)
            {
                案例“.doc”:
                案例“.docx”:
                案例“.txt”:
                案例“.rtf”:
                案例“.html”:
                案例“.htm”:
                案例“.xml”:
                案例“.odt”:
                案例“.wps”:
                案例“.wpd”:
                    返回“writer_pdf_Export”;
                案例“.xls”:
                案例“.xlsb”:
                案例“.ods”:
                    返回“calc_pdf_Export”;
                案例“.ppt”:
                案例“.pptx”:
                案例“.odp”:
                    返回“impress_pdf_Export”;

                默认值:返回空;
            }
        }


    }
于 2010-01-18T20:44:38.423 回答
1

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

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

于 2009-12-08T18:43:22.953 回答
1

PDFCreator 有一个 API 可以帮助您。

http://www.pdfforge.org/forum/open-discussion/3063-api-sdk-pdfcreator

于 2010-11-19T20:57:25.130 回答