添加和操作打印票

通过 C# 创建、编辑、链接和获取 XPS 文件的打印票证

 

所有页面描述语言格式都支持打印。其中一些,如 PDF,支持具有各种色彩空间和与分辨率无关的环境的高质量打印。由于 XPS 旨在在普通办公打印机上打印,因此它支持较少的色彩空间和仅 2 种字体。 Aspose.Page API 解决方案在不同的其他功能中允许使用打印票。在这里,您将找到说明如何创建、编辑、获取和链接它们的信息。

要操作 XPS 文件的打印票证,我们需要:

  • Aspose.Page for .NET API 是一个功能丰富、功能强大且易于使用的 C# 平台文档操作和转换 API。

  • 打开 NuGet 包管理器,搜索 Aspose.Page 并安装。您也可以从包管理器控制台使用以下命令。

Package Manager Console Command


    PM> Install-Package Aspose.Page

创建自定义打印票 C# .NET 的步骤。

  1. 设置文档目录的路径。
  2. 使用 XpsDocument 类 创建一个 XPS 文件。
  3. 使用 JobPrintTicket 构造函数添加自定义作业打印票。
  4. 向票证添加自定义页面参数初始化程序和自定义页面解析选项。
  5. 使用 XPsDocument.Save() 方法保存更改后的 XPS 文档。

在 XPS 文件中制作打印票的 C# 代码

    using Aspose.Page.XPS;
    using Aspose.Page.XPS.XpsMetadata;
    using Aspose.Page.XPS.XpsModel;
    using System.Drawing;
    using System;
    // The path to the documents directory.
    string dir = RunExamples.GetDataDir_WorkingWithPrintTickets();

    // Create a new XPS document
    XpsDocument xDocs = new XpsDocument();

    // Add a custom job print ticket
    xDocs.JobPrintTicket = new JobPrintTicket(
        new PageDevModeSnaphot("SABlAGwAbABvACEAAAA="),             // Custom page parameter initializer
        new DocumentCollate(Collate.CollateOption.Collated),
        new JobCopiesAllDocuments(1),
        new PageICMRenderingIntent(PageICMRenderingIntent.PageICMRenderingIntentOption.Photographs),
        new PageColorManagement(PageColorManagement.PageColorManagementOption.None),
        new JobNUpAllDocumentsContiguously(
            new NUp.PresentationDirection(NUp.PresentationDirection.PresentationDirectionOption.RightBottom),
            new Borders(Borders.BordersOption.On) /* Custom nested feature */).AddPagesPerSheetOption(1),
        new PageMediaSize(PageMediaSize.PageMediaSizeOption.NorthAmericaLetter),
        new JobInputBin(InputBin.InputBinOption.AutoSelect),
        new JobDuplexAllDocumentsContiguously(Duplex.DuplexOption.OneSided),
        new PageOrientation(PageOrientation.PageOrientationOption.Portrait),
        new PageResolution(
            new PageResolution.PageResolutionOption("ns0000:ESDL300x300")             // Custom page resolution option
                .SetResolutionX(300).SetResolutionY(300)),
        new PageMediaType(PageMediaType.PageMediaTypeOption.Plain),
        new PageOutputColor(PageOutputColor.PageOutputColorOption.Color.Clone().SetDeviceBitsPerPixel(0).SetDriverBitsPerPixel(24)));


    // Save the document with the custom job print ticket.
    xDocs.Save(dir + "output1.xps");

通过 C# .NET 编辑 XPS 打印票的步骤。

  1. 设置文档目录的路径。
  2. 使用 XpsDocument 类 打开带有打印票证的 XPS 文档。
  3. 要从工单中删除不需要的参数,请使用 Remove() 方法。
  4. 通过 XPsDocument.Save() 方法保存带有更改的作业打印票的文档。

在 XPS 文件中编辑打印票的 C# 代码

    // The path to the documents directory.
    string dir = RunExamples.GetDataDir_WorkingWithPrintTickets();

    // Open the XPS Document with print tickets
    XpsDocument xDocs = new XpsDocument(dir + "input3.xps");

    JobPrintTicket pt = xDocs.JobPrintTicket;

    // Remove some parameters from the job print ticket
    pt.Remove(
        "ns0000:PageDevmodeSnapshot",
        "ns0000:JobInterleaving",
        "ns0000:JobImageType");

    // Add some parameters to the job print ticket
    pt.Add(
        new JobCopiesAllDocuments(2),
        new PageMediaSize(PageMediaSize.PageMediaSizeOption.ISOA4));

    // Save the document with the changed job print ticket.
    xDocs.Save(dir + "output3.xps");

通过 C# .NET 获取打印票的步骤。

  1. 设置文档目录的路径。
  2. 使用 XpsDocument 类 打开带有打印票证的 XPS 文档。
  3. 使用 JobPrintTicket 构造函数创建作业打印票。
  4. 使用 GetDocumentPrintTicket() 方法创建文档打印票。
  5. 使用 GetPagePrintTicket() 方法获取页面打印票。
  6. 通过 XPsDocument.Save() 方法保存带有更改的作业打印票的文档。

在 XPS 文件中获取打印票证的 C# 代码

    // The path to the documents directory.
    string dir = RunExamples.GetDataDir_WorkingWithPrintTickets();

    // Open the XPS Document without print tickets
    XpsDocument xDocs = new XpsDocument(dir + "input1.xps");

    // Get the job print ticket
    JobPrintTicket jobPrintTicket = xDocs.JobPrintTicket; // must be null for this document

    // Get the document print ticket
    DocumentPrintTicket docPrintTicket = xDocs.GetDocumentPrintTicket(1); // must be null for this document

    // Get the page print ticket
    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 + "output1.xps");

    // Open the saved earlier XPS Document with print tickets
    XpsDocument xDocs2 = new XpsDocument(dir + "output1.xps");

    // Print tickets must not be null

    Console.WriteLine(xDocs2.JobPrintTicket);

    Console.WriteLine(xDocs2.GetDocumentPrintTicket(1));

    Console.WriteLine(xDocs2.GetPagePrintTicket(1, 1));

通过 C# .NET 链接 XPS 文件的打印票证的步骤。

  1. 设置文档目录的路径。
  2. 创建一个新的 XPS 文件并使用 XpsDocument 类 打开带有打印票证的 XPS 文档。
  3. 使用 XpsDocument 类 打开带有打印票证的 XPS 文档
  4. 将作业打印票与 JobPrintTicket 构造函数链接。
  5. 使用 GetDocumentPrintTicket()SetDocumentPrintTicket() 方法链接文档打印票
  6. 使用 GetPagePrintTicket()SetPagePrintTicket() 方法链接页面打印票证。
  7. 通过 XPsDocument.Save() 方法保存带有更改的作业打印票的文档。

用于链接 XPS 文件中的打印票证的 C# 代码

    // The path to the documents directory.
    string dir = RunExamples.GetDataDir_WorkingWithPrintTickets();

    // Create a new XPS document
    XpsDocument xDocs1 = new XpsDocument();

    // Open the XPS Document with print tickets
    XpsDocument xDocs2 = new XpsDocument(dir + "input2.xps");

    // Link the job print ticket
    xDocs1.JobPrintTicket = xDocs2.JobPrintTicket;

    // Link the document print ticket
    xDocs1.SetDocumentPrintTicket(1, xDocs2.GetDocumentPrintTicket(2));

    // Link the page print ticket
    xDocs1.SetPagePrintTicket(1, 1, xDocs2.GetPagePrintTicket(3, 2));


    // Save the document with linked print tickets.
    xDocs1.Save(dir + "output1.xps");



常问问题

1. 如何为 XPS 文件创建打印票据?

要在将文档发送到打印机之前为其创建打印票证(或打印信息),请使用 JobPrintTicket 类。

2. Aspose.Page API 解决方案中可以进行哪些打印票据操作?

使用此 .NET 解决方案,您可以创建、编辑、获取和链接打印信息。

3. 如何编辑XPS文件的打印信息?

设置路径并打开带有打印票据的 XPS 文档。使用 PrintTicket 类的方法。

XPS 什么是XPS文件格式

XPS 格式类似于 PDF 格式。两者都是页面描述语言 (PDL) 格式。 EPS 基于 HTML 而不是 PostScript 语言。 .eps 文件能够包含文档结构的标记以及有关文档外观的信息。还添加了有关如何打印和呈现文档的说明。该格式的特点是它修复了文档的描述,这意味着无论谁以及从哪个操作系统打开它,它看起来都是一样的。