今天,我将重点介绍 Aspose.Words for C++ 19.10 版本中引入的主要功能:

将 Word 文档另存为 MutiPage TIFF

您可能需要对 Word 文档(例如 DOCX、DOC、RTF 等)执行的一项有用操作是转换为图像文件。例如,如果您必须以可读和可打印但不可编辑的格式呈现您的 Word 文档(例如,用于在 Web 上发布)。您可以使用的一种简单方法是转换为多页 TIFF 文件。请参考以下文章了解如何使用 Aspose.Words for C++ 将 Word 文档转换为 TIFF 格式: 将 Word 文档转换为多页 TIFF 图像

// 如需完整的示例和数据文件,请访问 https://github.com/aspose-words/Aspose.Words-for-.NET
// 将文档另存为多页 TIFF。
doc.Save(dataDir + "TestFile Multipage TIFF_out.tiff");

使用 C++ 将图像转换为 PDF

现在可以使用 Aspose.Words for C++ API 从图像创建 PDF 文档。以下文章中提到的代码示例显示了使用 Aspose.Words for C++ 是多么容易。该代码允许将单帧图像(例如 JPEG、PNG、BMP、EMF 或 WMF)以及多帧 TIFF 图像和 GIF 转换为 PDF。 将图像转换为 PDF

使用 C++ 的 TIFF 二值化控制阈值

将灰度图像中包含的信息从 256 个灰度级减少到 2 个黑白灰度级并将其转换为二值图像的过程是二值化。将文档转换为 TIFF 文件格式时,您可以使用 ImageSaveOptions.ThresholdForFloydSteinbergDithering 属性控制 TIFF 二值化的阈值。此属性的默认值为 128。值越高,图像越暗。以下代码示例显示如何使用此属性来控制 TIFF 二值化的阈值

System::SharedPtr<ImageSaveOptions> options = System::MakeObject<ImageSaveOptions>(SaveFormat::Tiff);
options->set_TiffCompression(TiffCompression::Ccitt3);
options->set_ImageColorMode(ImageColorMode::Grayscale);
options->set_TiffBinarizationMethod(ImageBinarizationMethod::FloydSteinbergDithering);
options->set_ThresholdForFloydSteinbergDithering(254);

System::String outputPath = outputDataDir + u"ImageColorFilters.ExposeThresholdControlForTiffBinarization.tiff";
doc->Save(outputPath, options);

使用 C++ 将图像保存为每像素一位

下面的代码示例演示如何通过将 PixelFormat 设置为 Format1bppIndexed 将图像保存为每像素一位。

System::SharedPtr<ImageSaveOptions> opt = System::MakeObject<ImageSaveOptions>(SaveFormat::Png);
opt->set_PageIndex(1);
opt->set_ImageColorMode(ImageColorMode::BlackAndWhite);
opt->set_PixelFormat(ImagePixelFormat::Format1bppIndexed);

System::String outputPath = outputDataDir + u"ImageColorFilters.SaveImageToOnebitPerPixel.png";
doc->Save(outputPath, opt);

使用 C++ 使用密码加密 DOC 或 DOT 文件

DocSaveOptions 类用于在将文档保存为 DOC 或 DOT 格式时指定其他选项。使用此类,您可以将密码设置为加密文档,并在保存文档时忽略 RoutingSlip 数据。下面给出的代码示例显示了如何设置密码以使用 RC4 加密方法加密文档

System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.doc");
System::SharedPtr<DocSaveOptions> docSaveOptions = System::MakeObject<DocSaveOptions>();
docSaveOptions->set_Password(u"password");
System::String outputPath = outputDataDir + u"WorkingWithDoc.EncryptDocumentWithPassword.doc";
doc->Save(outputPath, docSaveOptions);

使用 C++ 使用密码加密 DOCX 文件

OoXMLSaveOptions 类提供了保存使用密码加密的任何文档的机会。使用此类,您可以在保存文档时使用 OoxmlSaveOptions.Password 属性设置密码。以下代码示例演示了如何设置密码并将文档保存为 DOCX 格式

System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.doc");
System::SharedPtr<OoxmlSaveOptions> ooxmlSaveOptions = System::MakeObject<OoxmlSaveOptions>();
ooxmlSaveOptions->set_Password(u"password");
System::String outputPath = outputDataDir + u"WorkingWithOoxml.EncryptDocxWithPassword.docx";
doc->Save(outputPath, ooxmlSaveOptions);

使用 DocumentVisitor 提取内容

您现在可以使用 DocumentVisitor 类来实现此使用场景。此类对应于著名的访问者设计模式。使用 DocumentVisitor,您可以定义和执行需要枚举文档树的自定义操作。更多详情,请查看: 如何使用 DocumentVisitor 提取内容

获取字体行间距

字体的行距是两行连续文本的基线之间的垂直距离。因此,行间距包括行之间的空白空间以及字符本身的高度。在 Font 类中引入了 LineSpacing 属性以获取字体行间距的值,如下面的示例所示:

// 初始化文档。
System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->get_Font()->set_Name(u"Calibri");
builder->Writeln(u"qText");

// 获取行距。
System::SharedPtr<Font> font = builder->get_Document()->get_FirstSection()->get_Body()->get_FirstParagraph()->get_Runs()->idx_get(0)->get_Font();
//Console.WriteLine($"lineSpacing = {font.LineSpacing}");
std::cout << "lineSpacing = " << font->get_LineSpacing() << std::endl;

另请参阅有用的链接

您可能需要完成任务的资源:

保持 Aspose 的传统,欢迎您通过在 Aspose.Words for C++ 支持论坛 中发布您的建议和疑虑来塑造即将发布的 Aspose.Words for C++ API。