使用OpenOffice提供的接口函数将WORD转换成PDF

1. 安装openoffice的SDK

2.  将SDK的安装目录 /sdk/cli/位置下的5个dll引入项目

3. 调用以下代码完成

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Botwave.McdTis.Domain;
using System.IO;
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.sheet;
using unoidl.com.sun.star;
using unoidl.com.sun.star.system;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.text;
using unoidl.com.sun.star.drawing;

namespace xxx

{
    public class PublicMethod
    {
        private string myFileName; //文件路径(包含文件名)
        public string MyFileName
        {
            get { return myFileName; }
            set { myFileName = value; }
        }

        public PublicMethod()
        {
 
        }

        /// <summary>
        /// 读取模版
        /// </summary>
        /// <returns></returns>
        public  XSpreadsheetDocument InitDocument(string ModelPath)
        {
            unoidl.com.sun.star.uno.XComponentContext localContext =
            uno.util.Bootstrap.bootstrap();

            unoidl.com.sun.star.lang.XMultiServiceFactory multiServiceFactory =
            (unoidl.com.sun.star.lang.XMultiServiceFactory)
            localContext.getServiceManager();

            string sUrl = PathConverter(ModelPath);
            //然后使用bootstrap获得一个OOo组件,如果OOo没有打开就会新建一个
            XComponentContext xContext = uno.util.Bootstrap.bootstrap();
            //然后使用该组件获得一个服务管理器:
            XMultiServiceFactory xMCF = (unoidl.com.sun.star.lang.XMultiServiceFactory)xContext.getServiceManager();
            //这样就实现了OOo的连接,使用该服务管理器,我们可以创建一个桌面实例,我们将该桌面实例强制转换
            XComponentLoader xCompLoader = (XComponentLoader)xMCF.createInstance("com.sun.star.frame.Desktop");
            PropertyValue[] propertyValues = new PropertyValue[1];
            propertyValues[0] = new unoidl.com.sun.star.beans.PropertyValue();
            propertyValues[0].Name = "Hidden";
            propertyValues[0].Value = new uno.Any(true);
            XComponent xComponent = xCompLoader.loadComponentFromURL(sUrl, "_blank", 0, propertyValues);

            return (XSpreadsheetDocument)xComponent;
        }


        /// <summary>
        /// 按序号获取工作簿内的sheet
        /// </summary>
        /// <param name="mxDocument"></param>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        public unoidl.com.sun.star.sheet.XSpreadsheet getSpreadsheet(XSpreadsheetDocument mxDocument, int nIndex)
        {
            // Collection of sheets
            unoidl.com.sun.star.sheet.XSpreadsheets xSheets =
                mxDocument.getSheets();

            unoidl.com.sun.star.container.XIndexAccess xSheetsIA =
                (unoidl.com.sun.star.container.XIndexAccess)xSheets;

            unoidl.com.sun.star.sheet.XSpreadsheet xSheet =
                (unoidl.com.sun.star.sheet.XSpreadsheet)
                  xSheetsIA.getByIndex(nIndex).Value;

            return xSheet;
        }

        /// <summary>
        /// 获取单元格
        /// </summary>
        /// <param name="xSpreadsheet"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public XCell getXCellByPosition(XSpreadsheet xSpreadsheet, int x, int y)
        {
            return xSpreadsheet.getCellByPosition(x, y);
        }

        /// <summary>
        /// 设置单元格text值
        /// </summary>
        /// <param name="xSpreadsheet"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="value"></param>
        public void setTextValueOfXCellAtPosition(XSpreadsheet xSpreadsheet, int x, int y, String value)
        {
            XCell xCell = getXCellByPosition(xSpreadsheet, x, y);
            unoidl.com.sun.star.text.XText xText = (unoidl.com.sun.star.text.XText)xCell;

            //XPropertySet xlprop = (XPropertySet)xCell;
            //xlprop.setPropertyValue("IsTextWrapped", value);


            // xCell.setFormula(value);
            //xText.setString(value);
            // XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, xCell); 

            unoidl.com.sun.star.text.XTextCursor xTextCursor =
                xText.createTextCursor();

            xText.insertString(xTextCursor, value, true);
        }

 

        public void setDoubleValueOfXCellAtPosition(XSpreadsheet xSpreadsheet, int x, int y, String value)
        {
            XCell xCell = getXCellByPosition(xSpreadsheet, x, y);
            unoidl.com.sun.star.text.XText xText = (unoidl.com.sun.star.text.XText)xCell;

            //XPropertySet xlprop = (XPropertySet)xCell;
            //xlprop.setPropertyValue("IsTextWrapped", value);
            xCell.setValue(Convert.ToDouble(value));

        }

        /// <summary>
        /// 转换路径分隔符
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private  string PathConverter(string file)
        {
            try
            {
                file = file.Replace(@"", "/");
                return "file:///" + file;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取表内行集合
        /// </summary>
        /// <param name="xComponent"></param>
        /// <returns></returns>
        public XTableRows GetRows(XSpreadsheetDocument xComponent)
        {
            unoidl.com.sun.star.sheet.XSpreadsheets xSheets = xComponent.getSheets();
            unoidl.com.sun.star.container.XIndexAccess xSheetsIA = (unoidl.com.sun.star.container.XIndexAccess)xSheets;
            unoidl.com.sun.star.sheet.XSpreadsheet xSheet = (unoidl.com.sun.star.sheet.XSpreadsheet)xSheetsIA.getByIndex(0).Value;


            unoidl.com.sun.star.table.XColumnRowRange xCRRange =
            (unoidl.com.sun.star.table.XColumnRowRange)xSheet;
            unoidl.com.sun.star.table.XTableColumns xColumns =
                xCRRange.getColumns();
            unoidl.com.sun.star.table.XTableRows xRows = xCRRange.getRows();

            return xRows;
        }


        /// <summary>
        /// 合并单元格
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="Rowindex"></param>
        public void MergeCell(XSpreadsheet sheet,int left,int top,int right ,int bottom)
        {
            //合并单元格
            unoidl.com.sun.star.table.XCellRange xCellRange = null;
            xCellRange = sheet.getCellRangeByPosition(left, top, right, bottom);

            unoidl.com.sun.star.util.XMergeable xMerge =
                (unoidl.com.sun.star.util.XMergeable)xCellRange;
            xMerge.merge(true);
        }

        /// <summary>
        ///  //拆分单元格
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="Rowindex"></param>
        public void UnMergeCell(XSpreadsheet sheet, int left, int top, int right, int bottom)
        {
            //拆分单元格
            unoidl.com.sun.star.table.XCellRange xCellRange = null;
            xCellRange = sheet.getCellRangeByPosition(left, top, right, bottom);

            unoidl.com.sun.star.util.XMergeable xMerge =
                (unoidl.com.sun.star.util.XMergeable)xCellRange;
            xMerge.merge(false);
        }


        /// <summary>
        /// 保存XXX路径下
        /// </summary>
        /// <param name="xComponent"></param>
        /// <param name="FileName"></param>
        public void SaveToPath(XSpreadsheetDocument xComponent, string FileName)
        {
            PropertyValue[] propertyValue =
   new PropertyValue[]{
                   new PropertyValue{Name="FilterName",Value=new uno.Any("writer_pdf_Export")}
               };

            ((XStorable)xComponent).storeToURL(
                //Convert the file path into a OpenOffice path
            PathConverter(FileName),
                //no additional arguments
            propertyValue);

           
            //PropertyValue[] propertyValue =
            //     new PropertyValue[]{
            //       new PropertyValue{Name="FilterName",Value=new uno.Any("writer_pdf_Export")}
            //   };

            //(xComponent as XStorable).storeToURL(FileName, propertyValue);
        }

        /// <summary>
        /// 设置公式
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="formulastr"></param>
        public void SetFormula(XSpreadsheet sheet,int x,int y,string formulastr)
        {
            sheet.getCellByPosition(x, y).setFormula(formulastr);
        }
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值