libreoffice打开文档-修改表格-修改书签-保存pdf
来源:互联网 发布:腾讯微云网盘for mac 编辑:程序博客网 时间:2024/05/02 00:11
功能:
1.打开文档
2.修改表格内容
3.修改书签内容
4.保存为pdf
5.插入中文字符
#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;
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
- libreoffice打开文档-修改表格-修改书签-保存pdf
- libreoffice 文档中插入表格-保存-打印
- C# 添加、修改和删除PDF书签
- C# 添加、修改和删除PDF书签
- pdf文档修改
- C#程序如何修改一个打开的文档并保存
- PODOFO打开PDF问题修改
- 修改PDF文档的方法
- pdf文档内容如何修改
- 如何编辑修改PDF文档
- 怎样用捷速PDF编辑器修改PDF文档
- firefox修改配置文件保存路径 (书签、配置、历史记录等)
- 解决pdf不能打印,不能注释,不能修改,不能保存等文档限制
- 修改FoxitReader (福昕PDF阅读器)最近打开文档数目的上限
- ubuntu 无法修改pdf的打开方式
- 使用Aspose.Pdf修改PDF文件中的表格字段
- Libreoffice打开文档读取错误恢复
- linux安装libreoffice打开doc文档
- 34-2-lesson exercises
- Unity 使用代码修改Texture属性 一键生成精灵
- 如何关闭Xcode8的系统日志打印
- 中医.辨证论治
- java 使用Thumbnailator 上传图片 并压缩图片大小
- libreoffice打开文档-修改表格-修改书签-保存pdf
- Github上传代码教程
- Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library
- git 删除分支和删除文件夹
- 欢迎使用CSDN-markdown编辑器
- 对spring ioc工作原理的理解
- 形状工厂-lintcode
- ListView的基本使用及其优化
- 【PHP高级】新闻管理系统NMS(SQL语句)