在 .NET 中使用工件

PDF 中的工件是图形对象或其他不属于文档实际内容的元素。它们通常用于装饰、布局或背景目的。工件的示例包括页眉、页脚、分隔符或不传达任何意义的图像。

PDF 中工件的目的是区分内容元素和非内容元素。这对于辅助功能非常重要,因为屏幕阅读器和其他辅助技术可以忽略工件并专注于相关内容。工件还可以提高 PDF 文档的性能和质量,因为它们可以在打印、搜索或复制时省略。

要在 PDF 中将元素创建为工件,您需要使用 Artifact 类。 它包含以下有用的属性:

  • Artifact.Type – 获取工件类型(支持 Artifact.ArtifactType 枚举的值,其中包括 Background, Layout, Page, Pagination 和 Undefined)。
  • Artifact.Subtype – 获取工件子类型(支持 Artifact.ArtifactSubtype 枚举的值,其中包括 Background, Footer, Header, Undefined, Watermark)。
  • Artifact.Image – 获取工件的图像(如果存在图像,否则为 null)。
  • Artifact.Text – 获取工件的文本。
  • Artifact.Contents – 获取工件内部操作符的集合。其支持的类型为 System.Collections.ICollection。
  • Artifact.Form – 获取工件的 XForm(如果使用 XForm)。水印、页眉和页脚工件包含 XForm,显示所有工件内容。
  • Artifact.Rectangle – 获取工件在页面上的位置。
  • Artifact.Rotation – 获取工件的旋转角度(以度为单位,正值表示逆时针旋转)。
  • Artifact.Opacity – 获取工件的不透明度。可能的值在 0…1 范围内,其中 1 表示完全不透明。

以下类在处理工件时也可能有用:

使用现有的水印

使用 Adobe Acrobat 创建的水印被称为工件(如 PDF 规范的 14.8.2.2 实际内容和工件中所描述)。

要获取特定页面上的所有水印,Page 类具有 Artifacts 属性。

下面的代码片段显示了如何获取 PDF 文件第一页上的所有水印。

注意: 此代码也适用于 Aspose.PDF.Drawing 库。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExtractWatermarkFromPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "sample-w.pdf"))
    {
        // Get the watermarks from the first page artifacts
        var watermarks = document.Pages[1].Artifacts
            .Where(artifact =>
                artifact.Type == Aspose.Pdf.Artifact.ArtifactType.Pagination
                && artifact.Subtype == Aspose.Pdf.Artifact.ArtifactSubtype.Watermark);

        // Iterate through the found watermark artifacts and print details
        foreach (Aspose.Pdf.WatermarkArtifact item in watermarks.Cast<Aspose.Pdf.WatermarkArtifact>())
        {
            Console.WriteLine($"{item.Text} {item.Rectangle}");
        }
    }
}

作为工件使用背景

背景图像可用于为文档添加水印或其他低调设计。在 Aspose.PDF for .NET 中,每个 PDF 文档都是页面的集合,每个页面包含一组工件。可以使用 BackgroundArtifact 类向页面对象添加背景图像。

下面的代码片段展示了如何使用 BackgroundArtifact 对象向 PDF 页面添加背景图像。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddBackgroundImageToPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "sample.pdf"))
    {
        // Create a new BackgroundArtifact and set the background image
        var background = new Aspose.Pdf.BackgroundArtifact()
        {
            BackgroundImage = File.OpenRead(dataDir + "background.jpg")
        };

        // Add the background image to the first page's artifacts
        document.Pages[1].Artifacts.Add(background);

        // Save PDF document with the added background
        document.Save(dataDir + "SampleArtifactsBackground_out.pdf");
    }
}

如果出于某种原因您希望使用纯色背景,请按如下方式更改前面的代码:

 // For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET

 private static void AddBackgroundColorToPDF()
 {
    // The path to the documents directory
     var dataDir = RunExamples.GetDataDir_AsposePdf();

     // Open PDF document
     using (var document = new Aspose.Pdf.Document(dataDir + "sample.pdf"))
     {
         // Create a new BackgroundArtifact and set the background color
         var background = new Aspose.Pdf.BackgroundArtifact()
         {
             BackgroundColor = Aspose.Pdf.Color.DarkKhaki
         };

         // Add the background color to the first page's artifacts
         document.Pages[1].Artifacts.Add(background);

         // Save PDF document
         document.Save(dataDir + "SampleArtifactsBackground_out.pdf");
     }
 }

统计特定类型的工件数量

要计算特定类型工件的总数(例如,总水印数),请使用以下代码:

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CountPDFArtifacts()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "sample.pdf"))
    {
        // Get pagination artifacts from the first page
        var paginationArtifacts = document.Pages[1].Artifacts
            .Where(artifact => artifact.Type == Aspose.Pdf.Artifact.ArtifactType.Pagination);

        // Count and display the number of each artifact type
        Console.WriteLine("Watermarks: {0}",
            paginationArtifacts.Count(a => a.Subtype == Aspose.Pdf.Artifact.ArtifactSubtype.Watermark));
        Console.WriteLine("Backgrounds: {0}",
            paginationArtifacts.Count(a => a.Subtype == Aspose.Pdf.Artifact.ArtifactSubtype.Background));
        Console.WriteLine("Headers: {0}",
            paginationArtifacts.Count(a => a.Subtype == Aspose.Pdf.Artifact.ArtifactSubtype.Header));
        Console.WriteLine("Footers: {0}",
            paginationArtifacts.Count(a => a.Subtype == Aspose.Pdf.Artifact.ArtifactSubtype.Footer));
    }
}

添加 Bates 编号工件

要向文档添加 Bates 编号工件,请在 PageCollection 上调用 AddBatesNumbering(BatesNArtifact batesNArtifact) 扩展方法,并传入 BatesNArtifact 对象作为参数:

或者,您可以传入一个 PaginationArtifacts 的集合:

另外,您可以使用操作委托来添加 Bates 编号工件:

要删除 Bates 编号,请使用以下代码: