将文本添加到 PS 文件
用于处理 PS 文件文本的 C++ API 解决方案
PostScript 是一种功能强大的页面描述语言,可以对文本放置和格式进行精细控制。以下是在 PostScript 中处理文本时需要考虑的一些关键细微差别:
- PostScript 允许您使用 PostScript 名称来选择字体。默认情况下通常可以使用 Times Roman、Helvetica 和 Courier 等常见字体。为了确保跨不同系统的一致渲染,您可以将字体直接嵌入到 PostScript 文件中。这对于自定义字体或不太常见的字体系列尤其重要。
- 字体大小和样式 -
fontsize
运算符设置字体大小(以磅为单位)。要修改字体样式,请使用“setfont”等运算符,它指定粗体、斜体或其他变体。 - 文本定位 -
moveto
运算符将文本光标定位到页面上的特定点。虽然PostScript没有直接的对齐操作符,但是您可以通过仔细地实现对齐。 - 文本旋转和缩放 - “旋转”运算符可用于将文本旋转到特定角度,而“缩放”运算符可用于放大或缩小文本。
- 文本渲染 - PostScript 支持抗锯齿功能,可以平滑文本边缘以提高可读性。您还可以控制字符(字距调整)和线条(行距)之间的间距,以微调文本的外观。
了解这些方面有助于确保基于 PostScript 的工作流程中正确的字体使用和一致的渲染。但无论如何,Aspose.Page 为您提供了轻松管理 PS 文件字体的功能。通过此 API,您可以使用自定义或系统字体添加不同颜色和画笔的文本。要详细了解 如何处理 PS 文件 和 如何处理 PS 文件的文本 特别遵循文档。
要将文本插入 PS 文档,我们需要:
Aspose.Page for C++ API 是一个功能丰富、功能强大且易于使用的文档操作和转换 C++ API。
打开 NuGet 包管理器,搜索 Aspose.Page.Cpp 并安装。您还可以从包管理器控制台使用以下命令。
Package Manager Console Command
PM> Install-Package Aspose.Page.Cpp
将文本添加到 PS 文件的步骤。
下面的代码片段演示了如何使用 C# 中的 Aspose.Page 库将使用 Unicode 字符串的文本添加到 PostScript (PS) 文档中。要查看示例的完整版本和更多示例,请访问 Aspose.Page-for-C++ GitHub 项目 。
- 使用包含文档的目录的路径初始化 dataDir 变量。
- 将变量 FONTS_FOLDER 设置为包含必要字体的文件夹的路径。
- 使用 FileStream 类为 PostScript 文档创建输出流。
- 使用 PsSaveOptions 对象指定 PostScript 文档的保存选项。
- 选项对象的AdditionalFontsFolders 属性设置为一个包含FONTS_FOLDER 路径的数组。这允许系统在该文件夹中找到任何所需的字体。
- 指定文本及其大小。
- 使用输出流、选项和 false 作为参数创建 PsDocument 的新实例。这将使用指定的选项初始化一个新的 PostScript 文档。
- 对document对象调用 ClosePage() 方法,表示当前页面完成。
- 使用 Save() 方法保存对 PS 文档所做的更改。
将图像添加到 PS
// The path to the documents directory. | |
System::String dataDir = RunExamples::GetDataDir_WorkingWithText(); | |
System::String FONTS_FOLDER = RunExamples::GetDataDir_Data() + u"necessary_fonts/"; | |
//Create output stream for PostScript document | |
{ | |
System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddText_outPS.ps", System::IO::FileMode::Create); | |
// Clearing resources under 'using' statement | |
System::Details::DisposeGuard<1> __dispose_guard_0({ outPsStream}); | |
// ------------------------------------------ | |
try | |
{ | |
//Create save options with A4 size | |
System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>(); | |
// Set custom fonts folder. It will be added to system fonts folders for finding needed font. | |
options->set_AdditionalFontsFolders(System::MakeArray<System::String>({FONTS_FOLDER})); | |
//A text to write to PS file | |
System::String str = u"ABCDEFGHIJKLMNO"; | |
int32_t fontSize = 48; | |
// Create new 1-paged PS Document | |
System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false); | |
////////////////////////////////////// Using sysem font (located in system fonts folders) for filling text ////////////////// | |
System::SharedPtr<System::Drawing::Font> font = System::MakeObject<System::Drawing::Font>(u"Times New Roman", static_cast<float>(fontSize), System::Drawing::FontStyle::Bold); | |
//Fill text with default or already defined color. In given case it is black. | |
document->FillText(str, font, 50.0f, 100.0f); | |
//Fill text with Blue color. | |
document->FillText(str, font, 50.0f, 150.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Blue())); | |
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////// Using custom font (located in custom fonts folders) for filling text ///////////////// | |
System::SharedPtr<DrFont> drFont = ExternalFontCache::FetchDrFont(u"Palatino Linotype", static_cast<float>(fontSize), System::Drawing::FontStyle::Regular); | |
//Fill text with default or already defined color. In given case it is black. | |
document->FillText(str, drFont, 50.0f, 200.0f); | |
//Fill text with Blue color. | |
document->FillText(str, drFont, 50.0f, 250.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Blue())); | |
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////// Using sysem font (located in system fonts folders) for outlining text //////////////// | |
//Outline text with default or already defined pen. In given case it is black colored 1-points width pen. | |
document->OutlineText(str, font, 50.0f, 300.0f); | |
//Outline text with blue-violet colored 2-points width pen. | |
document->OutlineText(str, font, 50.0f, 350.0f, System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_BlueViolet()), 2.0f)); | |
//Fill text with orange color and stroke with blue colored 2-points width pen. | |
document->FillAndStrokeText(str, font, 50.0f, 400.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Yellow()), System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_BlueViolet()), 2.0f)); | |
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
////////////////////////////////////// Using custom font (located in custom fonts folders) for outlining text ///////////////// | |
//Outline text with default or already defined pen. In given case it is black colored 1-points width pen. | |
document->OutlineText(str, drFont, 50.0f, 450.0f); | |
//Outline text with blue-violet colored 2-points width pen. | |
document->OutlineText(str, drFont, 50.0f, 500.0f, System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_BlueViolet()), 2.0f)); | |
//Fill text with orange color and stroke with blue colored 2-points width pen. | |
document->FillAndStrokeText(str, drFont, 50.0f, 550.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Orange()), System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Blue()), 2.0f)); | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
/////////////////////////////// Using custom font (located in custom fonts folders) ang glyphs widths for filling text //////// | |
drFont = ExternalFontCache::FetchDrFont(u"Palatino Linotype", static_cast<float>(fontSize), System::Drawing::FontStyle::Regular); | |
//Glyphs widths | |
System::ArrayPtr<float> widths = System::MakeArray<float>({87, 87, 87, 87, 34, 87, 87}); | |
//Fill ASCII text using with assigning glyphs widths. | |
document->FillText(u"BAMBOOK", widths, drFont, 50.0f, 600.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Blue())); | |
///////////////////////////// Using custom font (located in custom fonts folders) ang glyphs widths for filling unicode text // | |
//Glyphs widths | |
widths = System::MakeArray<float>({87, 34, 87, 87, 87, 87, 87}); | |
//Fill Unicode text using with assigning glyphs widths. | |
document->FillText(u"ЗООПАРК", widths, drFont, 50.0f, 650.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Orange())); | |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
//Close current page | |
document->ClosePage(); | |
//Save the document | |
document->Save(); | |
} | |
catch(...) | |
{ | |
__dispose_guard_0.SetCurrentException(std::current_exception()); | |
} | |
} |
PS 什么是PS文件格式
PS 格式是页面描述语言 (PDL) 格式之一。它能够在页面上包含图形和文本信息。这就是为什么大多数图像编辑程序都支持该格式的原因。 postscript 文件本身就是对打印机的一种指令。它包含有关从其页面打印什么以及如何打印的信息。