Spire.Doc for C++ 是一款专门对 Word 文档进行操作的 C++ 类库。这款控件的主要功能在于帮助开发人员轻松快捷高效地创建、编辑、转换、及比较Microsoft Word 文档。作为一款独立的 Word C++ 控件,Spire.Doc for C++ 的运行系统(服务器端或客户端)均无需安装 Microsoft Word,但是它却可以将 Microsoft Word 文档的操作功能集成到任何开发人员的 C++ 应用程序中。
Spire.Doc for C++ 能执行多种 Microsoft Word 文档处理任务的 C++ API。支持 Word97-2003、Word2007、Word2010、Word2013、Word2016 以及 Word2019。同时兼容大部分国产操作系统,能够在中标麒麟和中科方德等国产操作系统中正常运行。能在 Word 97/2003/2007/2010/2013 和 XML、RTF、TXT、XPS、EPUB、EMF、HTML、ODT 等格式文件之间进行双向转换,还能将 Word 文件高质量地转换为 PDF、OFD、PCL 和 SVG 文件格式。
随着 PDF 文档类型的日益多样化,您可能会需要对机密信息进行保护。尽管 PDF 的许多其他安全选项可用于确保机密信息的安全,但最常见的方法是为PDF文档添加自定义水印。在本文中,您将学习如何使用 Spire.PDF for C++ 在 C++ 中为 PDF 添加单行或多行文本水印。
有两种方法可以将 Spire.PDF for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire. PDF for C++ 集成到 C++ 程序中
Spire.PDF for C++ 不提供负责在 PDF 文件中插入水印的接口或类。但您可以在每页上绘制类似“机密”、“请勿复制”或“草稿”的文本,以模拟水印效果。以下是向 PDF 文档添加单行文本水印的步骤。
#include ""Spire.Pdf.o.h"";
using namespace std;
using namespace Spire::Pdf;
int main()
{
//指定输入文件和输出文件路径
wstring inputFilePath = L""C:\\Users\\Administrator\\Desktop\\示例文档.pdf"";
wstring outputFilePath = L""Output\\添加单行文本水印.pdf"";
//创建PdfDocument对象
PdfDocument* doc = new PdfDocument();
//加载PDF文件
doc->LoadFromFile(inputFilePath.c_str());
//创建true字体类型
PdfTrueTypeFont* font = new PdfTrueTypeFont(L""宋体"", 50.0f, PdfFontStyle::Bold, true);
//创建一个笔刷
boost::intrusive_ptr<PdfBrush> brush = PdfBrushes::GetDarkGray();
//指定水印文本
wstring text = L""请勿复制"";
//测量文本大小
SizeF textSize = font->MeasureString(text.c_str());
//计算两个偏移量,用于计算坐标系的平移量
float offset1 = (float)(textSize.GetWidth() * sqrt(2) / 4);
float offset2 = (float)(textSize.GetHeight() * sqrt(2) / 4);
//遍历文档中的页面
for (size_t i = 0; i < doc->GetPages()->GetCount(); i++)
{
//获取特定页面
boost::intrusive_ptr <PdfPageBase> page = doc->GetPages()->GetItem(i);
//设置页面透明度
page->GetCanvas()->SetTransparency(0.8);
//将坐标系转换为指定坐标
page->GetCanvas()->TranslateTransform(page->GetCanvas()->GetSize()->GetWidth() / 2 - offset1 - offset2, page->GetCanvas()->GetSize()->GetHeight() / 2 + offset1 - offset2);
//逆时针旋转坐标系45度
page->GetCanvas()->RotateTransform(-45);
//在页面上绘制水印文本
page->GetCanvas()->DrawString(text.c_str(), font, brush, 0, 0, new PdfStringFormat(PdfTextAlignment::Left));
}
//保存文档
doc->SaveToFile(outputFilePath.c_str());
doc->Close();
delete doc;
}
要实现平铺水印效果,可以使用 PdfTilingBrush 类。平铺笔刷生成一个图案,用它重复填充某个图形区域即可实现平铺效果。以下是向 PDF 文档添加平铺文本水印的步骤。
#include ""Spire.Pdf.o.h"";
using namespace std;
using namespace Spire::Pdf;
static void InsertTiledTextWatermark(PdfPageBase* page, wstring watermarkText, PdfTrueTypeFont* font, int rowNum, int columnNum)
{
//测量文本大小
SizeF textSize = font->MeasureString(watermarkText.c_str());
//计算两个偏移量,用于计算坐标系的平移量
float offset1 = (float)(textSize.GetWidth() * sqrt(2) / 4);
float offset2 = (float)(textSize.GetHeight() * sqrt(2) / 4);
//获取页面高度和宽度
float height = page->GetActualSize()->GetHeight();
float width = page->GetActualSize()->GetWidth();
//创建平铺笔刷
PdfTilingBrush* brush = new PdfTilingBrush(new SizeF(width / columnNum, height / rowNum));
brush->GetGraphics()->SetTransparency(0.5f);
brush->GetGraphics()->TranslateTransform(brush->GetSize()->GetWidth() / 2 - offset1 - offset2, brush->GetSize()->GetHeight() / 2 + offset1 - offset2);
brush->GetGraphics()->RotateTransform(-45);
//在画笔上绘制水印文本
brush->GetGraphics()->DrawString(watermarkText.c_str(), font, PdfBrushes::GetRed(), 0, 0, new PdfStringFormat(PdfTextAlignment::Left));
//使用平铺笔刷绘制一个矩形(覆盖整个页面)
page->GetCanvas()->DrawRectangle(brush, new RectangleF(new PointF(0, 0), page->GetActualSize()));
}
int main()
{
//指定输入文件和输出文件路径
wstring inputFilePath = L""C:\\Users\\Administrator\\Desktop\\示例文档.pdf"";
wstring outputFilePath = L""Output\\添加多行文本水印.pdf"";
//创建PdfDocument对象
PdfDocument* doc = new PdfDocument();
//加载PDF文件
doc->LoadFromFile(inputFilePath.c_str());
//指定水印文本
wstring text = L""请勿复制"";
//创建true字体类型
PdfTrueTypeFont* font = new PdfTrueTypeFont(L""宋体"", 20.0f, PdfFontStyle::Bold, true);
//遍历文档中的页面
for (size_t i = 0; i < doc->GetPages()->GetCount(); i++)
{
//调用自定义方法插入多行文本水印
boost::intrusive_ptr<Spire::Pdf::PdfPageBase> page = doc->GetPages()->GetItem(i);
InsertTiledTextWatermark(page.get(), text.c_str(), font, 3, 3);
}
//保存文档
doc->SaveToFile(outputFilePath.c_str());
doc->Close();
delete doc;
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
将文档作为附件添加到 PDF 为您带来了很多便利。例如,您可以将多个文档作为单个文档传输;您可以在 PDF 文档中打开另一个文件,而无需从其他位置查找该文档;您可以降低丢失 PDF 中引用文档的可能性。
Spire.PDF for C++ 允许您以两种方式附加文件:
本文将向您展示如何使用 Spire.PDF for C++ 在 C++ 中添加或删除 PDF 文档中的常规附件和注释附件。
有两种方法可以将 Spire.PDF for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire. PDF for C++ 集成到 C++ 程序中
要添加常规附件,可以使用 PdfDocument->GetAttachments()->Add() 方法。以下是详细步骤。
#include "Spire.Pdf.o.h";
using namespace Spire::Pdf;
using namespace std;
int main() {
//指定输入文件路径
wstring inputPdfPath = L"C:\\Users\\Administrator\\Desktop\\AI创意赛.pdf";
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\参赛人员名单.xlsx";
//指定输出文件路径
wstring outputFilePath = L"Output\\添加附件.pdf";
//创建PdfDocument对象
PdfDocument* doc = new PdfDocument();
//加载示例PDF文件
doc->LoadFromFile(inputPdfPath.c_str());
//基于外部文件创建PdfAttachment对象
PdfAttachment* attachment = new PdfAttachment(inputFilePath.c_str());
//将附件添加到PDF
doc->GetAttachments()->Add(attachment);
//保存到文件
doc->SaveToFile(outputFilePath.c_str());
delete doc;
}
注释附件由 PdfAttachmentAnnotation 类表示。您需要基于外部文件创建该类的实例,然后使用 PdfPageBase->GetAnnotationsWidget()->Add() 方法将其添加到特定页面。以下是详细步骤。
#include ""Spire.Pdf.o.h"";
using namespace Spire::Pdf;
using namespace std;
int main() {
//指定输入文件路径
wstring inputPdfPath = L""AI创意赛.pdf"";
wstring inputFilePath = L""赛制规则及流程安排.docx"";
//指定输出文件路径
wstring outputFilePath = L""Output\\添加注释附件.pdf"";
//创建PdfDocument对象
PdfDocument* doc = new PdfDocument();
//加载示例PDF文件
doc->LoadFromFile(inputPdfPath.c_str());
//获取特定页面
boost::intrusive_ptr <PdfPageBase> page = doc->GetPages()->GetItem(0);
//在PDF上绘制标签
wstring label = L""现场相关事宜安排见附件:"";
PdfTrueTypeFont* font = new PdfTrueTypeFont(L""宋体"", 13.0f, PdfFontStyle::Bold, true);
float x = 35;
float y = doc->GetPages()->GetItem(0)->GetActualSize()->GetHeight() - 220;
page->GetCanvas()->DrawString(label.c_str(), font, PdfBrushes::GetRed(), x, y);
//转换要附加到流的文件
ifstream is1(inputFilePath.c_str(), ifstream::in | ios::binary);
is1.seekg(0, is1.end);
int length1 = is1.tellg();
is1.seekg(0, is1.beg);
char* buffer1 = new char[length1];
is1.read(buffer1, length1);
Stream* stream = new Spire::Pdf::Stream((unsigned char*)buffer1, length1);
boost::intrusive_ptr <SizeF> size = font->MeasureString(label.c_str());
RectangleF* bounds = new RectangleF((float)(x + size->GetWidth() + 5), (float)y, 10, 15);
//基于文件创建PdfAttachmentAnnotation对象
PdfAttachmentAnnotation* annotation = new PdfAttachmentAnnotation(bounds, L""赛制规则及流程安排.docx"", stream);
annotation->SetColor(new PdfRGBColor(Spire::Pdf::Color::GetDarkOrange()));
annotation->SetFlags(PdfAnnotationFlags::ReadOnly);
annotation->SetIcon(PdfAttachmentIcon::Graph);
annotation->SetText(L""单击此处打开文件"");
//将附件注释添加到PDF
page->GetAnnotationsWidget()->Add(annotation);
//保存文件
doc->SaveToFile(outputFilePath.c_str());
delete doc;
}
Spire.PDF for C++ 提供 PdfDocument->GetAttachments() 方法用于返回 PDF 文档的常规附件集合。然后您可以使用 PdfAttachmentCollection->RemoveAt() 方法或 PdfAttachmentCollection->Clear() 方法删除特定附件或所有附件。详细步骤如下。
#include "Spire.Pdf.o.h";
using namespace Spire::Pdf;
using namespace std;
int main() {
//指定输入文件路径
wstring inputPdfPath = L"C:\\Users\\Administrator\\Desktop\\示例文档.pdf";
//指定输出文件路径
wstring outputFilePath = L"Output\\删除附件.pdf";
//创建PdfDocument对象
PdfDocument* doc = new PdfDocument();
//加载PDF文件
doc->LoadFromFile(inputPdfPath.c_str());
//获取所有附件
boost::intrusive_ptr <PdfAttachmentCollection> attachments = doc->GetAttachments();
//删除所有附件
attachments->Clear();
//删除指定附件
//attachments->RemoveAt(0);
//保存文件
doc->SaveToFile(outputFilePath.c_str());
doc->Close();
delete doc;
}注释是基于页面的元素。 您可以使用 PdfPageBase->GetAnnotationsWidget() 方法从特定页面获取注释,并确定某个注释是否为注释附件。之后,使用 PdfAnnotationCollection->Remove() 方法从注释集合中删除注释附件。 以下是详细步骤。
#include "Spire.Pdf.o.h";
using namespace Spire::Pdf;
using namespace std;
int main() {
//指定输入文件路径
wstring inputPdfPath = L"C:\\Users\\Administrator\\Desktop\\示例文档.pdf";
//指定输出文件路径
wstring outputFilePath = L"Output\\删除附件.pdf";
//创建PdfDocument对象
PdfDocument* doc = new PdfDocument();
//加载PDF文件
doc->LoadFromFile(inputPdfPath.c_str());
//获取所有附件
PdfAttachmentCollection* attachments = doc->GetAttachments();
//删除所有附件
attachments->Clear();
//删除指定附件
//attachments->RemoveAt(0);
//保存文件
doc->SaveToFile(outputFilePath.c_str());
doc->Close();
delete doc;
}如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
背景颜色或图片可以使文档更加美观和引人注目。如果您正在创建一个用于营销、教育或演示的文档,添加一个具有吸引力的背景色或图片将非常有用。在本文中,我们将演示如何使用 Spire.Doc for C++ 以编程方式给 Word 文档添加背景色或背景图。
有两种方法可以将 Spire.Doc for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire.Doc for C++ 集成到 C++ 程序中
使用 Spire.Doc for C++,向 Word 文档添加背景颜色非常简单。您只需要将文档的背景类型设置为颜色,然后指定一种颜色作为背景即可。详细步骤如下。
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main()
{
//初始化 Document 类的一个实例
intrusive_ptr <Document> document = new Document();
//加载 Word 文档
document->LoadFromFile(L"鲁迅.docx");
//获取文档的背景
intrusive_ptr <Background> background = document->GetBackground();
//将背景类型设置为颜色
background->SetType(BackgroundType::Color);
//设置背景颜色
background->SetColor(Color::GetAliceBlue());
//保存结果文档
document->SaveToFile(L"添加背景颜色.docx", FileFormat::Docx2013);
document->Close();
}
要添加渐变背景,您需要将背景类型设置为渐变,指定渐变颜色,然后设置渐变着色变体和样式。详细步骤如下。
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main()
{
//初始化 Document 类的一个实例
intrusive_ptr <Document> document = new Document();
//加载 Word 文档
document->LoadFromFile(L"鲁迅.docx");
//获取文档的背景
intrusive_ptr <Background> background = document->GetBackground();
//设置背景类型为渐变
background->SetType(BackgroundType::Gradient);
//指定两种渐变颜色
background->GetGradient()->SetColor1(Color::GetWhite());
background->GetGradient()->SetColor2(Color::GetLightBlue());
//设置渐变着色变体和样式
background->GetGradient()->SetShadingVariant(GradientShadingVariant::ShadingDown);
background->GetGradient()->SetShadingStyle(GradientShadingStyle::Horizontal);
//保存结果文档
document->SaveToFile(L"添加渐变背景.docx", FileFormat::Docx2013);
document->Close();
}
为 Word 文档添加背景图片,需要设置背景类型为图片,然后插入图片作为背景。详细步骤如下。
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main()
{
//初始化 Document 类的一个实例
intrusive_ptr <Document> document = new Document();
//加载 Word 文档
document->LoadFromFile(L"鲁迅.docx");
//获取文档的背景
intrusive_ptr <Background> background = document->GetBackground();
//设置背景类型为图片
background->SetType(BackgroundType::Picture);
//设置背景图片
background->SetPicture(L"背景图.jpeg");
//保存结果文档
document->SaveToFile(L"添加背景图.docx", FileFormat::Docx2013);
document->Close();
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
PDF 是当今使用的最通用且功能最丰富的文件格式之一。与 Word 相比,PDF 文档在各种设备上打开时丢失格式的可能性极低。此外,PDF在文档安全、归档和传输方面相比Word具有绝对的优势。这些就是我们为什么要将 Word 转换为 PDF 的一些原因。在本文中,您将学习如何将 Word 转换为 PDF 以及如何使用 Spire.Doc for C++ 在 C++ 中设置转换选项。
有两种方法可以将 Spire.Doc for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire.Doc for C++ 集成到 C++ 程序中
Spire.Doc for C++ 提供的 Document->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) 方法允许将 Word 另存为 PDF、XPS、HTML、RTF 等。如果您只想将 Word 文档另存为常规 PDF 而无需额外设置 , 请按照以下步骤操作。
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//指定输入文件路径
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\示例文档.docx";
//指定输出文件路径和名称
wstring outputPath = L"Output\\";
wstring outputFile = outputPath + L"ToPDF.pdf";
//创建Document对象
intrusive_ptr<Document> document = new Document();
//加载 Word 文件
document->LoadFromFile(inputFilePath.c_str());
//将文档保存为 PDF
document->SaveToFile(outputFile.c_str(), FileFormat::PDF);
document->Close();
}
书签可以增强文档的可读性。从 Word 生成 PDF 时,您可能希望保留 Word 文档的现有书签或从标题创建书签。以下是将 Word 转换为带书签的 PDF 的步骤。
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//指定输入文件路径
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\示例文档.docx";
//指定输出文件路径和名称
wstring outputPath = L"Output\\";
wstring outputFile = outputPath + L"ToPDF.pdf";
//创建Document对象
intrusive_ptr<Document> document = new Document();
//加载 Word 文件
document->LoadFromFile(inputFilePath.c_str());
//创建 ToPdfParameterList 对象
intrusive_ptr < ToPdfParameterList> parameters = new ToPdfParameterList();
//从 Word 标题创建书签
parameters->SetCreateWordBookmarksUsingHeadings(true);
//从 Word 中的现有书签创建 PDF 书签
//parameters->SetCreateWordBookmarks(true);
//将文档保存为 PDF
document->SaveToFile(outputFile.c_str(), parameters);
document->Close();
}
通过将 Word 文档中使用的字体嵌入到 PDF 文档中,可以确保 PDF 文档在任何未安装适当字体的设备上看起来都一样。在转换过程中将字体嵌入 PDF 的步骤如下。
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//指定输入文件路径
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\示例文档.docx";
//指定输出文件路径和名称
wstring outputPath = L"Output\\";
wstring outputFile = outputPath + L"ToPDF.pdf";
//创建Document对象
intrusive_ptr document = new Document();
//加载 Word 文件
document->LoadFromFile(inputFilePath.c_str());
//创建 ToPdfParameterList 对象
intrusive_ptr parameters = new ToPdfParameterList();
//在生成的 PDF 中嵌入 Word 中使用的字体
parameters->SetIsEmbeddedAllFonts(true);
//将文档保存为 PDF
document->SaveToFile(outputFile.c_str(), parameters);
document->Close();
} 
包含大量高质量图像的文档通常尺寸较大。当您将 Word 转换为 PDF 时,您可以决定是否压缩图像质量。以下是详细步骤。
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//指定输入文件路径
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\示例文档.docx";
//指定输出文件路径和名称
wstring outputPath = L"Output\\";
wstring outputFile = outputPath + L"ToPDF.pdf";
//创建 Document 对象
intrusive_ptr document = new Document();
//加载 Word 文件
document->LoadFromFile(inputFilePath.c_str());
//将图像压缩到原始质量的 40%
document->SetJPEGQuality(40);
//保持原始图像质量
//document->SetJPEGQuality(100);
//将文档保存为 PDF
document->SaveToFile(outputFile.c_str(), FileFormat::PDF);
document->Close();
} 如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.Doc for Java 11.3.0 已发布。该版本增强了 Word 到 PDF 的转换。此外,本次更新还修复了许多已知问题,如修复了替换书签时程序抛异常java.lang.NullPointerException的问题。详情请阅读以下内容。
问题修复:
Spire.PDF for Java 9.2.5 已发布。本次更新优化了文档压缩功能,同时增强了 PDF 到 Excel 的转换。此外,该版本还修复了一些已知问题。详情请阅读以下内容。
新功能:
PdfCompressor compressor = new PdfCompressor(inputFile);
compressor.getOptions().getImageCompressionOptions().setResizeImages(true);
compressor.getOptions().getImageCompressionOptions().setImageQuality(ImageQuality.Low);
compressor.compressToFile(outputFile);问题修复:
Word 文档是使用 Microsoft Word 或其他文字处理程序创建的文件。它们被世界上几乎所有类型的企业所使用。各种专业文档,如商业合同、论文、手册、信件、简历和报告,都以 Word 文档的形式创建和保存。在本文中,您将学习如何使用 Spire.Doc for C++ 以编程方式在 C++ 中创建或编辑 Word 文档。
有两种方法可以将 Spire.Doc for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire.Doc for C++ 集成到 C++ 程序中
使用 Spire.Doc for C++,您可以创建一个包含一个或多个部分的 Word 文档,并向其中添加各种元素,例如段落、表格、图像、列表、超链接、水印、页眉、页脚、内容控件和注释。
以下步骤向您展示了如何创建一个包含一个节和三个段落的简易 Word 文档:
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main()
{
//初始化 Document 类的一个实例
intrusive_ptr<Document> doc = new Document();
//在文档中添加一个节
intrusive_ptr<Section> section = doc->AddSection();
//设置页边距
section->GetPageSetup()->GetMargins()->SetAll(72);
//向该节添加标题段落
intrusive_ptr<Paragraph> titlePara = section->AddParagraph();
//向段落添加文本
titlePara->AppendText(L"Spire.Doc for C++ 简介");
//向该节添加正文段落
intrusive_ptr<Paragraph> bodyPara1 = section->AddParagraph();
//向段落添加文本
bodyPara1->AppendText(L"Spire.Doc for C++ 是一个专业的Word 库,专门为开发人员设计,可以在C++ 应用程序中创建、读取、写入、转换、合并、拆分和比较Word 文档,具有快速和高质量的性能。");
//向该节添加正文段落
intrusive_ptr<Paragraph> bodyPara2 = section->AddParagraph();
//向段落添加文本
bodyPara2->AppendText(L"通过使用 Spire.Doc for C++,用户可以将 Word Doc/Docx 转换为 XML、RTF、EMF、TXT、XPS、EPUB、HTML、SVG、ODT,反之亦然。Spire.Doc for C++ 还支持将 Word Doc/Docx 转换为 PDF 以及将 HTML 转换为图像。");
//创建样式并将其应用于标题段落
intrusive_ptr<ParagraphStyle> style1 = new ParagraphStyle(doc);
style1->SetName(L"titleStyle");
style1->GetCharacterFormat()->SetBold(true);
style1->GetCharacterFormat()->SetTextColor(Color::GetBlue());
style1->GetCharacterFormat()->SetFontName(L"宋体");
style1->GetCharacterFormat()->SetFontSize(16);
doc->GetStyles()->Add(style1);
titlePara->ApplyStyle(L"titleStyle");
//创建样式并将其应用于正文段落
intrusive_ptr<ParagraphStyle> style2 = new ParagraphStyle(doc);
style2->SetName(L"paraStyle");
style2->GetCharacterFormat()->SetFontName(L"宋体");
style2->GetCharacterFormat()->SetFontSize(12);
doc->GetStyles()->Add(style2);
bodyPara1->ApplyStyle(L"paraStyle");
bodyPara2->ApplyStyle(L"paraStyle");
//设置标题和正文段落的水平对齐方式
titlePara->GetFormat()->SetHorizontalAlignment(HorizontalAlignment::Center);
bodyPara1->GetFormat()->SetHorizontalAlignment(HorizontalAlignment::Justify);
bodyPara2->GetFormat()->SetHorizontalAlignment(HorizontalAlignment::Justify);
//在标题和正文段落后设置间距
titlePara->GetFormat()->SetAfterSpacing(10);
bodyPara1->GetFormat()->SetAfterSpacing(10);
//保存结果文件
doc->SaveToFile(L"创建Word文档.docx", FileFormat::Docx2013);
doc->Dispose();
}
除了从头开始创建 Word 文档外,Spire.Doc for C++ 还可以让您能够编辑现有的 Word 文档。 例如,您可以修改文档中的现有元素或向文档添加新元素。
以下步骤向您展示了如何修改 Word 文档中特定段落的文本:
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
int main()
{
//初始化Document类的实例
intrusive_ptr<Document> doc = new Document();
//加载Word文档
doc->LoadFromFile(L"创建Word文档.docx");
//访问文档中的第一节
intrusive_ptr<Section> section = doc->GetSections()->GetItemInSectionCollection(0);
//访问第一节中的第二段
intrusive_ptr<Paragraph> para = section->GetParagraphs()->GetItemInParagraphCollection(1);
//修改段落文本
para->SetText(L"本段已更新");
//保存结果文档
doc->SaveToFile(L"编辑Word文档.docx", FileFormat::Docx2013);
doc->Dispose();
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.Spreadsheet 7.2.0 已发布。本次更新支持缩放功能。详情请阅读以下内容。
新功能:
https://www.e-iceblue.cn/Downloads/Spire-Spreadsheet-NET.html
带标签的 PDF 是包含与 HTML 代码非常相似的标签的 PDF 文档。标签提供了一种逻辑结构,用于控制如何通过辅助技术呈现PDF内容。每个标签标识了关联的内容元素,例如标题级别 1 <H1>、段落 <P>、图像 <Figure> 或表格 <Table>。在本文中,您将学习如何使用 Spire.PDF for Java 在 Java 中创建带标签的 PDF 文档。
首先,您需要在 Java 程序中添加 Spire.Pdf.jar 文件作为依赖项。JAR 文件可以从此链接下载。如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而在应用程序中导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>11.11.11</version>
</dependency>
</dependencies>
要在带标签的 PDF 文档中添加结构元素,我们必须首先创建 PdfTaggedContent 类的对象。 然后使用 PdfTaggedContent.getStructureTreeRoot().appendChildElement() 方法向根添加一个元素。以下是使用 Spire.PDF for Java 向带标签的 PDF 添加“标题”元素的详细步骤。
以下代码提供了一个示例,展示了如何在 Java 标记的 PDF 文档中创建各种元素,包括文档、标题、段落、图像和表格。
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.interchange.taggedpdf.PdfStandardStructTypes;
import com.spire.pdf.interchange.taggedpdf.PdfStructureElement;
import com.spire.pdf.interchange.taggedpdf.PdfTaggedContent;
import com.spire.pdf.tables.PdfTable;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
public class CreateTaggedPdf {
public static void main(String[] args) throws Exception {
//创建一个 PdfDocument 对象
PdfDocument doc = new PdfDocument();
//添加页面
PdfPageBase page = doc.getPages().add(PdfPageSize.A4, new PdfMargins(20));
//设置 Tab 键顺序
page.setTabOrder(TabOrder.Structure);
//创建 PdfTaggedContent 类的对象
PdfTaggedContent taggedContent = new PdfTaggedContent(doc);
//设置文档的语言和标题
taggedContent.setLanguage("zh-CN");
taggedContent.setTitle("创建标签PDF文件");
//设置PDF/UA1标识
taggedContent.setPdfUA1Identification();
//创建字体和画笔
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("宋体",Font.PLAIN,13), true);
PdfSolidBrush brush = new PdfSolidBrush(new PdfRGBColor(Color.BLACK));
//添加“文档”元素
PdfStructureElement document = taggedContent.getStructureTreeRoot().appendChildElement(PdfStandardStructTypes.Document);
//添加“标题”元素
PdfStructureElement heading1 = document.appendChildElement(PdfStandardStructTypes.HeadingLevel1);
heading1.beginMarkedContent(page);
String headingText = "什么是标签PDF文件?";
page.getCanvas().drawString(headingText, font, brush, new Point2D.Float(5, 30));
heading1.endMarkedContent(page);
//添加“段落”元素
PdfStructureElement paragraph = document.appendChildElement(PdfStandardStructTypes.Paragraph);
paragraph.beginMarkedContent(page);
String paragraphText = "带标签的 PDF 似乎不是一个改变生活的术语。但对一些人来说,它是。" +
"对于盲人或视力不佳且使用辅助技术(例如屏幕阅读器和连接的盲文显示器)来访问信息的人来说," +
"未加标签的 PDF 意味着他们错过了文档中包含的信息," +
"因为辅助技术无法“阅读”未加标签的 PDF。" +
"数字可访问性为曾经对视障人士关闭的信息开辟了许多途径,但 PDF 经常被排除在外。";
Rectangle2D.Float rect = new Rectangle2D.Float(5, 60, (float) page.getCanvas().getClientSize().getWidth(), (float) page.getCanvas().getClientSize().getHeight());
page.getCanvas().drawString(paragraphText, font, brush, rect);
paragraph.endMarkedContent(page);
//添加“图形”元素
PdfStructureElement figure = document.appendChildElement(PdfStandardStructTypes.Figure);
figure.beginMarkedContent(page);
PdfImage image = PdfImage.fromFile("PDF.png");
page.getCanvas().drawImage(image, new Point2D.Float(5, 160));
figure.endMarkedContent(page);
//添加“表格”元素
PdfStructureElement table = document.appendChildElement(PdfStandardStructTypes.Table);
table.beginMarkedContent(page);
PdfTable pdfTable = new PdfTable();
pdfTable.getStyle().getDefaultStyle().setFont(font);
String[] data = {"姓名;年龄;性别",
"杨丽;22;女",
"李顺;25;男"
};
String[][] dataSource = new String[data.length][];
for (int i = 0; i < data.length; i++) {
dataSource[i] = data[i].split("[;]", -1);
}
pdfTable.setDataSource(dataSource);
pdfTable.getStyle().setShowHeader(true);
pdfTable.draw(page.getCanvas(), new Point2D.Float(5, 320), 300f);
table.endMarkedContent(page);
//将文档保存到文件
doc.saveToFile("创建标签PDF文件.pdf");
}
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。