使用Aspose.Pdf處理現有的水印

由Adobe Acrobat創建的水印稱爲artifact。Aspose.Pdf擁有兩個類專門處理artifact:Artifact和ArtifactCollection。

爲了得到特定頁面上的所有artifacts,該頁面類上要有Artifacts屬性。

本文解釋瞭如何使用Aspose.Pdf處理PDF文件中的Artifacts。

處理Artifacts

Artifact類包含以下屬性:

  • Artifact.Type——獲得artifact類型(支持包括背景、佈局、頁面、分頁和未定義在內的Artifact.ArtifactType枚舉值)。
  • Artifact.Subtype——得到工件artifact子類型(支持包括背景、頁腳、頁眉、未定義,水印在內的Artifact.ArtifactSubtype枚舉值)。
  • Artifact.Contents——得到一組artifact內部運算符。它所支持的類型是System.Collections.ICollection。
  • Artifact.Form——得到一個artifact的變換(如果使用XForm)。水印、頁眉和頁腳artifacts包含了所有的artifacts內容變換。
  • Artifact.Image——得到一個artifact的圖像(如果只呈現一個圖像,其他的爲空值)。
  • Artifact.Text——得到一個artifact的文本。
  • Artifact.Rectangle--得到頁面上artifact的位置。
  • Artifact.Rotation--得到一個artifact的旋轉(degrees, positive的值表示逆時針旋轉)。
  • Artifact.Opacity——得到一個artifact的不透明度。可能的值範圍在0……1,1是代表完全透明的。

下面的代碼片段顯示瞭如何獲得在第一頁的PDF文件上的每個水印。

C#

//Open document
Document pdfDocument = new Document("input.pdf");
// Iterate through and get tub-type, text and location of artifact
foreach(Artifact artifact in pdfDocument.Pages[1].Artifacts)
{ 
    Console.WriteLine(artifact.Subtype + " " + artifact.Text + " " + artifact.Rectangle); 
}

VB.NET

'Open document
Dim pdfDocument As Document = New Document("input.pdf")
' Iterate through and get tub-type, text and location of artifact
For Each artifact As Artifact In pdfDocument.Pages(1).Artifacts
    Console.WriteLine(artifact.Subtype.ToString() & " " & artifact.Text & " " & artifact.Rectangle.ToString())
Next

編程實例:計算某一特定類型的Artifacts

計算某一特定類型的artifacts總數(例如,水印的總數),使用下面的代碼:

C#

//Open document
Document pdfDocument = new Document("input.pdf");
int count = 0;
foreach(Artifact artifact in pdfDocument.Pages[1].Artifacts)
{
    // if artifact type is watermark, increate the counter
    if (artifact.Subtype == Artifact.ArtifactSubtype.Watermark) count++;
}
Console.WriteLine("Page contains " + count + " watermarks");

VB.NET

' Open document
Dim pdfDocument As Document = New Document("d:/pdftest/DifferentStamps.pdf")
Dim count As Integer = 0
For Each artifact As Artifact In pdfDocument.Pages(1).Artifacts
    ' if artifact type is watermark, increate the counter
    If (artifact.Subtype = artifact.ArtifactSubtype.Watermark) Then
        count += 1
    End If
Next
Console.WriteLine("Page contains " & count & " watermarks")

發佈了15 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章