创建和修改 XPS 文件

用于创建和更改 XPS 文件的 C# .NET API 解决方案。

 

Aspose.Page for .NET API 解决方案允许您操作 XPS 文件以及其他页面描述语言格式文件。丰富的功能允许向文档添加不同的形状,将几个文件合并为一个文件,或将它们转换为更好的格式。这里将描述如何创建新的 XPS 文件和修改(添加签名)已经存在的文件。添加代码片段是为了更好地理解。

要操作 XPS 文件,我们需要:

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

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

Package Manager Console Command


    PM> Install-Package Aspose.Page

使用 C# .NET 创建 XPS 文件的步骤。

  1. 设置文档目录的路径。
  2. 使用 XpsDocument 类 创建一个 XPS 文件。
  3. 如果需要,使用 AddGlyphs() 方法将字形添加到文档中。
  4. 使用 XPsDocument.Save() 方法保存更改后的 XPS 文档。

用于制作 XPS 文件的 C# 代码

    using Aspose.Page.XPS;
    using Aspose.Page.XPS.XpsModel;
    using System.Drawing;
    // The path to the documents directory.
    string dir = RunExamples.GetDataDir_WorkingWithDocument();
    // Create a new XPS Document
    XpsDocument xDocs = new XpsDocument();

    // Add the glyph to the document
    var glyphs = xDocs.AddGlyphs("Arial", 12, FontStyle.Regular, 300f, 450f, "Hello World!");

    glyphs.Fill = xDocs.CreateSolidColorBrush(Color.Black);

    // Save the result
    xDocs.Save(dir + "output.xps");
下一个代码片段展示了如何在 Aspose.Page for .NET Api 解决方案中修改 XPS 文件。

使用 C# .NET 修改 XPS 文件的步骤。

  1. 设置文档目录的路径。
  2. 打开 XPS 文件流。
  3. 使用 XpsDocument 类 创建一个 XPS 文件。
  4. 要创建签名文本的填充,请使用 CreateSolidColorBrush() 方法。
  5. 要定义将设置签名的页面,请使用 PageNumbers 属性。
  6. 使用 SelectActivePageAddGlyphs() 方法调整签名。
  7. 通过 XPsDocument.Save() 方法保存更改后的 XPS 文档。

用于编辑 XPS 文件的 C# 代码

    // The path to the documents directory.
    string dir = RunExamples.GetDataDir_WorkingWithDocument();
    // Open a stream of XPS file
    using (FileStream xpsStream = File.Open(dir + "input1.xps", FileMode.Open, FileAccess.Read))
    {
        // Create an XPS document from stream
        XpsDocument document = new XpsDocument(xpsStream, new XpsLoadOptions());

        // Create the fill of the signature text
        XpsSolidColorBrush textFill = document.CreateSolidColorBrush(Color.BlueViolet);

        // Define pages where the signature will be set
        int[] pageNumbers = new int[] {1, 2, 3};

        // For every defined page set signature "Confirmed" at coordinates x=650 and y=950
        for (int i = 0; i < pageNumbers.Length; i++)
        {
            // Define an active page
            document.SelectActivePage(pageNumbers[i]);

            // Create a glyphs object
            XpsGlyphs glyphs = document.AddGlyphs("Arial", 24, FontStyle.Bold, 650, 900, "Confirmed");

            // Define the fill for glyphs
            glyphs.Fill = textFill;
        }

        // Save the changed XPS document
        document.Save(dir + "input1_out.xps");
    }



常问问题

1. 如何编辑 XPS 文件?

要使用此 API 解决方案修改 XPS 文件,首先设置文件的路径,然后使用 XpsDocument 类的实体来实现更改。

2. 如何创建 .xps 文件?

要使用 Aspose.Page 创建新的 XPS 文件,您需要设置文档的路径,然后使用 XpsDocument 类的 XpsDocument 构造函数。

3. 如何将 XPS 转换为 DOCX?

要从 XPS 文件获取 DOCX 文件,请使用我们免费的跨平台 转换器

XPS 什么是XPS文件格式

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