添加和操作打印票据
通过 C++ 创建、编辑、链接并获取 XPS 文件的打印票据
在 XPS 文件的上下文中,打印票证是一组指定如何打印文档的指令。它本质上是一个配置文件,包含以下信息:
- 打印页面的尺寸和方向。
- 要使用纸盘或进纸器。
- 打印输出的分辨率和颜色深度。
- 是否在纸张上双面打印。
- 打印和整理页面的顺序。
- 任何附加的整理选项,例如装订、装订或打孔。
通过了解和利用打印票据,您可以优化打印流程并实现 XPS 文档所需的输出。
Aspose.Page API 解决方案在不同的其他功能中允许使用打印票证。在这里您将找到解释如何创建、编辑、获取和链接它们的信息。要操作 XPS 文件的打印票据,我们需要:
-Aspose.Page for C++ API 是一个功能丰富、功能强大且易于使用的文档操作和转换工具。
-打开 NuGet 包管理器,然后搜索 Aspose.Page 并安装。您还可以从包管理器控制台使用以下命令。
Package Manager Console Command
PM> Install-Package Aspose.Page
创建自定义打印票据 C++ 的步骤。
- 设置文档目录的路径。
- 使用 XpsDocument Class 创建 XPS 文件。
- 使用 JobPrintTicket 构造函数添加自定义作业打印票证。
- 向票证添加自定义页面参数初始值设定项和自定义页面解析选项。
- 使用 XPsDocument.Save() 方法保存更改的 XPS 文档。
创建自定义打印票据
// The path to the documents directory. | |
System::String dir = RunExamples::GetDataDir_WorkingWithPrintTickets(); | |
// Create new XPS document | |
{ | |
System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(); | |
// Clearing resources under 'using' statement | |
System::Details::DisposeGuard<1> __dispose_guard_0({ document}); | |
// ------------------------------------------ | |
try | |
{ | |
// Set a custom job-level print ticket | |
document->set_JobPrintTicket(System::MakeObject<JobPrintTicket>( | |
System::MakeArray<System::SharedPtr<IJobPrintTicketItem>>({ | |
// Specify input bin. | |
System::MakeObject<JobInputBin>(System::MakeArray<System::SharedPtr<InputBin::IInputBinItem>>({InputBin::InputBinOption::Manual->Clone()->Add(System::MakeArray<System::SharedPtr<InputBin::IInputBinOptionItem>>( | |
{InputBin::FeedFace::FaceDown, InputBin::FeedDirection::LongEdgeFirst, System::MakeObject<InputBin::MediaSheetCapacity>(100)}))})), | |
// Specify output bin. | |
System::MakeObject<JobOutputBin>(System::MakeArray<System::SharedPtr<OutputBin::IOutputBinItem>>({ | |
System::MakeObject<OutputBin::OutputBinOption>(System::MakeArray<System::SharedPtr<OutputBin::IOutputBinOptionItem>>( | |
{OutputBin::BinType::Sorter})), | |
System::MakeObject<OutputBin::OutputBinOption>(System::MakeArray<System::SharedPtr<OutputBin::IOutputBinOptionItem>>( | |
{OutputBin::BinType::Stacker, System::MakeObject<OutputBin::MediaSheetCapacity>(100)}))})), | |
// Specify page orientation. | |
System::MakeObject<PageOrientation>(System::MakeArray<System::SharedPtr<PageOrientation::PageOrientationOption>>( | |
{PageOrientation::PageOrientationOption::Landscape})), | |
// Specify duplex mode for the output. | |
System::MakeObject<JobDuplexAllDocumentsContiguously>(System::MakeArray<System::SharedPtr<Duplex::DuplexOption>>( | |
{Duplex::DuplexOption::TwoSidedLongEdge(Duplex::DuplexMode::Automatic)})), | |
// Specify the color settings for the output. | |
System::MakeObject<PageOutputColor>(System::MakeArray<System::SharedPtr<PageOutputColor::IPageOutputColorItem>>( | |
{PageOutputColor::PageOutputColorOption::Grayscale(0, 8)}))}))); | |
// Save the document with the custom job-level print ticket. | |
document->Save(dir + u"output1.xps"); | |
} | |
catch(...) | |
{ | |
__dispose_guard_0.SetCurrentException(std::current_exception()); | |
} | |
} |
通过 C++ 编辑 XPS 打印票据的步骤。
- 设置文档目录的路径。
- 使用 XpsDocument 类 打开带有打印票据的 XPS 文档。
- 要从票证中删除不需要的参数,请使用 Remove() 方法。
- 通过 XPsDocument.Save() 方法保存带有更改后的作业打印票证的文档。
编辑打印票据
// The path to the documents directory. | |
System::String dir = RunExamples::GetDataDir_WorkingWithPrintTickets(); | |
// Open XPS Document with print tickets | |
System::SharedPtr<XpsDocument> xDocs = System::MakeObject<XpsDocument>(dir + u"input3.xps"); | |
System::SharedPtr<JobPrintTicket> pt = xDocs->get_JobPrintTicket(); | |
// Remove some parameters from job print ticket | |
pt->Remove(System::MakeArray<System::String>({u"ns0000:PageDevmodeSnapshot", u"ns0000:JobInterleaving", u"ns0000:JobImageType"})); | |
// Add some parameters to job print ticket | |
pt->Add(System::MakeArray<System::SharedPtr<IJobPrintTicketItem>>({System::MakeObject<JobCopiesAllDocuments>(2), | |
System::MakeObject<PageMediaSize>(System::MakeArray<System::SharedPtr<PageMediaSize::IPageMediaSizeItem>>({PageMediaSize::PageMediaSizeOption::ISOA4}))})); | |
// Save the document with changed job print ticket. | |
xDocs->Save(dir + u"output3.xps"); |
通过 C++ 获取打印门票的步骤。
- 设置文档目录的路径。
- 使用 XpsDocument 类 打开带有打印票据的 XPS 文档。
- 使用 JobPrintTicket 构造函数创建作业打印票证。
- 使用 GetDocumentPrintTicket() 方法创建文档打印票证。
- 使用 GetPagePrintTicket() 方法获取页面打印票证。
- 通过 XPsDocument.Save() 方法保存更改后的作业打印票证的文档。
获取打印票
// The path to the documents directory. | |
System::String dir = RunExamples::GetDataDir_WorkingWithPrintTickets(); | |
// Open XPS Document without print tickets | |
System::SharedPtr<XpsDocument> xDocs = System::MakeObject<XpsDocument>(dir + u"input1.xps"); | |
// Get job print ticket | |
System::SharedPtr<JobPrintTicket> jobPrintTicket = xDocs->get_JobPrintTicket(); | |
// must be null for this document | |
// Get document print ticket | |
System::SharedPtr<DocumentPrintTicket> docPrintTicket = xDocs->GetDocumentPrintTicket(1); | |
// must be null for this document | |
// Get page print ticket | |
System::SharedPtr<PagePrintTicket> pagePrintTicket = xDocs->GetPagePrintTicket(1, 1); | |
// must be null for this document | |
// Save the document. Default print tickets are automatically added to document while saving. | |
xDocs->Save(dir + u"output1.xps"); | |
// Open saved earlier XPS Document with print tickets | |
System::SharedPtr<XpsDocument> xDocs2 = System::MakeObject<XpsDocument>(dir + u"output1.xps"); | |
// Print tickets must not be null | |
System::Console::WriteLine(xDocs2->get_JobPrintTicket()); | |
System::Console::WriteLine(xDocs2->GetDocumentPrintTicket(1)); | |
System::Console::WriteLine(xDocs2->GetPagePrintTicket(1, 1)); |
通过 C++ 链接 XPS 文件的打印票据的步骤。
- 设置文档目录的路径。
- 创建一个新的 XPS 文件并使用 XpsDocument Class 打开带有打印票据的 XPS 文档。
- 使用 XpsDocument 类 打开带有打印票据的 XPS 文档
- 将作业打印票证与 JobPrintTicket 构造函数链接。
- 使用 GetDocumentPrintTicket() 和 SetDocumentPrintTicket() 方法链接文档打印票证
- 使用 GetPagePrintTicket() 和 SetPagePrintTicket() 方法链接页面打印票证。
- 通过 XPsDocument.Save() 方法保存更改后的作业打印单的文档。
链接打印票证
// The path to the documents directory. | |
System::String dir = RunExamples::GetDataDir_WorkingWithPrintTickets(); | |
// Create new XPS document | |
System::SharedPtr<XpsDocument> xDocs1 = System::MakeObject<XpsDocument>(); | |
// Open XPS Document with print tickets | |
System::SharedPtr<XpsDocument> xDocs2 = System::MakeObject<XpsDocument>(dir + u"input2.xps"); | |
// Link job print ticket | |
xDocs1->set_JobPrintTicket(xDocs2->get_JobPrintTicket()); | |
// Link document print ticket | |
xDocs1->SetDocumentPrintTicket(1, xDocs2->GetDocumentPrintTicket(2)); | |
// Link page print ticket | |
xDocs1->SetPagePrintTicket(1, 1, xDocs2->GetPagePrintTicket(3, 2)); | |
// Save the document with linked print tickets. | |
xDocs1->Save(dir + u"output1.xps"); |
XPS 什么是XPS文件格式
XPS 格式类似于 PDF 格式。两者都是页面描述语言 (PDL) 格式。 EPS 基于 HTML 而不是 PostScript 语言。 .eps 文件能够包含文档结构的标记以及有关文档外观的信息。还添加了有关如何打印和呈现文档的说明。该格式的特点是它修复了文档的描述,这意味着无论谁以及从哪个操作系统打开它,它看起来都是一样的。