1. 程式人生 > >libreoffice開啟文件-修改表格-修改書籤-儲存pdf

libreoffice開啟文件-修改表格-修改書籤-儲存pdf

功能:
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;