Working with Artifacts in .NET

Artifacts in PDF are graphics objects or other elements that are not part of the actual content of the document. They are usually used for decoration, layout, or background purposes. Examples of artifacts include page headers, footers, separators, or images that do not convey any meaning.

The purpose of artifacts in PDF is to allow the distinction between content and non-content elements. This is important for accessibility, as screen readers and other assistive technologies can ignore artifacts and focus on the relevant content. Artifacts can also improve the performance and quality of PDF documents, as they can be omitted from printing, searching, or copying.

To create an element as an artifact in PDF, you need to use the Artifact class. It contains following useful properties:

  • Artifact.Type – gets the artifact type (supports values of the Artifact.ArtifactType enumeration where values include Background, Layout, Page, Pagination and Undefined).
  • Artifact.Subtype – gets artifact subtype (supports the values of the Artifact.ArtifactSubtype enumeration where values include Background, Footer, Header, Undefined, Watermark).
  • Artifact.Image – Gets an artifact’s image (if an image is presents, else null).
  • Artifact.Text – Gets an artifact’s text.
  • Artifact.Contents – Gets a collection of artifact internal operators. Its supported type is System.Collections.ICollection.
  • Artifact.Form – Gets an artifact’s XForm (if XForm is used). Watermarks, header, and footer artifacts contains XForm which shows all artifact contents.
  • Artifact.Rectangle – Gets an position of an artifact on the page.
  • Artifact.Rotation – Gets an artifact’s rotation (in degrees, positive value indicates counter-clockwise rotation).
  • Artifact.Opacity – Gets an artifact’s opacity. Possible values are in the range 0…1, where 1 is completely opaque.

The following classes may also be useful for work with artifacts:

Working with Existing Watermarks

A watermark created with Adobe Acrobat is called an artifact (as described in Real Content and Artifacts of the PDF specification).

In order to get all Watermarks on a particular page, the Page class has the Artifacts property.

The following code snippet shows how to get all watermarks on the first page of a PDF file.

Note: This code also works with Aspose.PDF.Drawing library.

var document = new Document(System.IO.Path.Combine(_dataDir, "sample-w.pdf"));
var watermarks = document.Pages[1].Artifacts
    .Where(artifact =>
    artifact.Type == Artifact.ArtifactType.Pagination
    && artifact.Subtype == Artifact.ArtifactSubtype.Watermark);
foreach (WatermarkArtifact item in watermarks.Cast<WatermarkArtifact>())
    Console.WriteLine($"{item.Text} {item.Rectangle}");

Working with Backgrounds as Artifacts

Background images can be used to add a watermark, or other subtle design, to documents. In Aspose.PDF for .NET, each PDF document is a collection of pages and each page contains a collection of artifacts. The BackgroundArtifact class can be used to add a background image to a page object.

The following code snippet shows how to add a background image to PDF pages using the BackgroundArtifact object.

var document = new Document(System.IO.Path.Combine(_dataDir, "sample.pdf"));
var background = new BackgroundArtifact()
    BackgroundImage = System.IO.File.OpenRead(System.IO.Path.Combine(_dataDir, "background.jpg"))
document.Save(System.IO.Path.Combine(_dataDir, "sample_artifacts_background.pdf"));

If you want, for some reason, to use a solid color background, please change the previous code in the following manner:

var document = new Document(System.IO.Path.Combine(_dataDir, "sample.pdf"));
var background = new BackgroundArtifact()
    BackgroundColor = Color.DarkKhaki,
document.Save(System.IO.Path.Combine(_dataDir, "sample_artifacts_background.pdf"));

Counting Artifacts of a Particular Type

To calculate the total count of artifacts of a particular type (for example, the total number of watermarks), use the following code:

var document = new Document(System.IO.Path.Combine(_dataDir, "sample.pdf"));
var paginationArtifacts = document.Pages[1].Artifacts.Where(artifact => artifact.Type == Artifact.ArtifactType.Pagination);
Console.WriteLine("Watermarks: {0}", paginationArtifacts.Count(a => a.Subtype == Artifact.ArtifactSubtype.Watermark));
Console.WriteLine("Backgrounds: {0}", paginationArtifacts.Count(a => a.Subtype == Artifact.ArtifactSubtype.Background));
Console.WriteLine("Headers: {0}", paginationArtifacts.Count(a => a.Subtype == Artifact.ArtifactSubtype.Header));
Console.WriteLine("Footers: {0}", paginationArtifacts.Count(a => a.Subtype == Artifact.ArtifactSubtype.Footer));