libreoffice打开文档-修改表格-修改书签-保存pdf

来源:互联网 发布:腾讯微云网盘for mac 编辑:程序博客网 时间:2024/05/02 00:11
功能:
1.打开文档
2.修改表格内容
3.修改书签内容
4.保存为pdf
5.插入中文字符


环境信息:
OS :win7 x64
IDE:VC3013
LibreOffice_5.2.2_Win_x64
LibreOffice_5.2.2_Win_x64_sdk


#include <Windows.h>
#include <stdio.h>
#include <osl/file.hxx>
#include <cppuhelper/bootstrap.hxx>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/awt/XToolkit2.hpp>
#include <com/sun/star/awt/Rectangle.hpp>
#include <com/sun/star/text/XTextTable.hpp>
#include <com/sun/star/text/XWordCursor.hpp>
#include <com/sun/star/text/XTextTablesSupplier.hpp>
#include <com/sun/star/awt/WindowAttribute.hpp>
#include <osl/process.h>
#include <rtl/process.h>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/registry/XSimpleRegistry.hpp>
#include <cppuhelper/weak.hxx>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/text/XBookmarksSupplier.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/awt/XSystemChildFactory.hpp>
#include <com/sun/star/awt/XSystemDependentWindowPeer.hpp>
#include <com/sun/star/lang/SystemDependent.hpp>
#include <com/sun/star/view/XPrintable.hpp>
#include <com/sun/star/frame/XDesktop.hpp>




using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::bridge;
using namespace com::sun::star::frame;
using namespace com::sun::star::awt;
using namespace rtl;
using namespace cppu;
using namespace com::sun::star::uno;
using namespace com::sun::star::beans;
using namespace com::sun::star::registry;
using ::rtl::OUString;
using ::rtl::OUStringToOString;
using namespace com::sun::star::text;
using namespace com::sun::star::table;
using namespace com::sun::star::container;
using ::rtl::OString;
using namespace com::sun::star::view;


#pragma comment(lib, "icppu.lib")
#pragma comment(lib, "icppuhelper.lib")
#pragma comment(lib, "ipurpenvhelper.lib")
#pragma comment(lib, "isal.lib")
#pragma comment(lib, "isalhelper.lib")


//retrieve an instance of the remote service manager
Reference< XMultiServiceFactory > rOfficeServiceManager;
rOfficeServiceManager = ooConnect();
if (rOfficeServiceManager.is())
{
printf("Connected sucessfully to the office\n");
}
else
{
printf("Connected failure to the office\n");
return 0;
}
//get the desktop service using createInstance returns an XInterface type
Reference< XInterface > Desktop = rOfficeServiceManager->createInstance(
OUString::createFromAscii("com.sun.star.frame.Desktop"));




//query for the XComponentLoader interface
Reference< XComponentLoader > rComponentLoader(Desktop, UNO_QUERY);
if (rComponentLoader.is())
{
printf("XComponentloader successfully instanciated\n");
}


//打开已存在的文档
OUString sOpenFile = OUString::createFromAscii("file:///E|/Works/libre-file/tmp.odt");


//get an instance of the spreadsheet
Reference< XComponent > xWriterComponent = rComponentLoader->loadComponentFromURL(
//OUString::createFromAscii("private:factory/swriter"),
sOpenFile,
OUString::createFromAscii("_blank"),
0,
Sequence < ::com::sun::star::beans::PropertyValue >());
// add code here




Reference < XTextDocument > xTextDocument(xWriterComponent, UNO_QUERY);


///////////////////////////////////////////////////////
printf("开始修改书签内容\r\n");
system("pause");
//文档中必须存在书签, 书签名称为taskitem
Reference< XBookmarksSupplier > xBookmark(xTextDocument, UNO_QUERY);
Reference< XNameAccess > xName = xBookmark->getBookmarks();
try{
Any bookObj = xName->getByName("taskitem");
Reference< XTextContent > xTxtCont(bookObj, UNO_QUERY);
Reference< XTextRange > xRange=xTxtCont->getAnchor();


//字符集问题
char *sAnsiTxt = "中文书签内容";
int nLen = MultiByteToWideChar(CP_ACP, 0, sAnsiTxt, -1, NULL, 0);
wchar_t *sWchTxt = new wchar_t(nLen + 1);
MultiByteToWideChar(CP_ACP, 0, sAnsiTxt, -1, sWchTxt, nLen);
OUString sOuTxt(sWchTxt, nLen);
//此处释放sWchTxt,会导致sOuTxt异常
//delete sWchTxt;
xRange->setString(sOuTxt);
}
catch (Exception &e)
{
printf("没找到书签或书签修改异常**************************");
}




///////////////////////////////////////////////////////
printf("开始修改表格内容\r\n");
system("pause");
//文档中必须存在书签, 书签名称为taskitem
try{
Reference< XTextTablesSupplier > xTxtTabs(xTextDocument, UNO_QUERY);
Reference< XNameAccess > xTabName = xTxtTabs->getTextTables();
Reference< XIndexAccess > xTabIndex(xTabName, UNO_QUERY);
Reference< XTextTable> xTab(xTabIndex->getByIndex(0), UNO_QUERY);

// it's time to add some text now
Reference<XCell> xCell = xTab->getCellByName(OUString::createFromAscii("A1"));
Reference<XText> xText = Reference<XText>(xCell, UNO_QUERY);
Reference< ::css::text::XTextCursor > xTextCursor = xText->createTextCursor();
xTextCursor->setString(OUString::createFromAscii("change txt"));
}
catch (Exception &e)
{
printf("没找到表格或表格内容修改异常**************************");
}


//保存文档
Reference< XStorable > rcomponentStore(xWriterComponent, UNO_QUERY);
if (!rcomponentStore.is())
{
printf("XStorable Not successfully instanciated\n");
return 0;
}
else
{
//保存为PDF
OUString sPath = OUString::createFromAscii("file:///E|/Works/libre-file/111.pdf");
Sequence < ::css::beans::PropertyValue> pro(2);


pro[0].Name = OUString::createFromAscii("Overwrite");
pro[0].Value <<= (sal_Bool)true;
pro[1].Name = OUString::createFromAscii("FilterName");
pro[1].Value <<= OUString::createFromAscii("writer_pdf_Export");


Reference<XStorable> xStorable(xWriterComponent, UNO_QUERY);
xStorable->storeToURL(sPath, pro);
}


system("pause");
xTextDocument->dispose();


return 1;
0 0
原创粉丝点击