
由於資料保密性,我們需要從 PDF 文件中刪除敏感細節,因此我們通常會從 PDF 文件的某個頁面區域進行編輯(刪除文字、圖像等)。但是,當處理大量文件時,我們需要使用API來完成這項要求。
在本文中,我們將討論如何使用 Aspose.PDF Cloud SDK for Java 的 RedactionAnnotation 類別線上編輯 PDF 的步驟。
SDK 的安裝
Aspose.PDF Cloud SDK for Java 是專門針對 Java 開發人員的程式設計 API,它建立在 Aspose.PDF Cloud API 之上。我們的雲端 API 可以在任何平台上使用,無需安裝任何特定的軟體。但是,為了使用 SDK,您需要先在系統上安裝它。
可以透過 Maven 和 GitHub 下載 Cloud SDK。現在在您的 pom.xml 檔案中添加以下詳細信息,以便在您的 Maven 建置專案中下載並使用 Aspose.Pdf.jar。
<repositories>
<repository>
<id>aspose-cloud</id>
<name>artifact.aspose-cloud-releases</name>
<url>https://artifact.aspose.cloud/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf-cloud</artifactId>
<version>21.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
安裝 SDK 後,下一步是取得您的個人化用戶端 ID 和客戶端金鑰。請造訪Aspose.Cloud 儀表板,如果您有 GitHub 或 Google 帳戶,只需註冊即可。否則,請點擊建立新帳戶按鈕並提供所需資訊。現在使用憑證登入儀表板並展開應用程式部分。向下捲動至客戶端憑證部分以查看客戶端 ID 和客戶端金鑰的詳細資訊。
使用 Java 新增修訂註釋
在下面的部分中,我們解釋瞭如何使用 Aspose.PDF Cloud SDK for Java 新增矩形框來刪除 PDF 檔案內容的步驟。
- 首先,我們需要建立一個 PdfApi 對象,同時傳遞從 Aspose.Cloud 儀表板取得的 ClientId 和 ClientSecret 詳細資訊。
- 其次,建立一個 Rectangle 類別的物件來指定頁面上將新增註解的區域。
- 第三,從 AnnotationFlags 枚舉中選擇預設值並將其新增至 AnnotationFlags 類型的清單中。
- 現在建立 RedactionAnnotation 類別的實例並使用 setRect(…) 方法設定矩形區域。
- 使用 setHorizontalAlignment(…) 方法設定水平對齊方式,並從 HorizontalAlignment 枚舉中選擇 Center。
- 為了填滿註解內的顏色,請使用 setFillColor(…) 方法並傳遞 Color 物件。
- 使用 setModified(…) 方法設定註解的最後修改日期。
- 倒數第二,建立一個 RedactionAnnotation 類型的 List,並將 RedactAnnotation 物件加入此清單。
- 最後呼叫PdfApi的postPageRedactionAnnotations(…)方法,傳遞來源檔案名,需要加入註解的頁碼。另外,傳遞 RedactionAnnotation List 作為參數來啟動編輯過程。
// 從 https://dashboard.aspose.cloud/ 取得 ClientID 和 ClientSecret
String clientId = "29ac1517-753f-4303-b755-7185e35cf939";
String clientSecret = "c537caf71eafc8a75a5ee7813b703276";
// createPdfApi 實例
PdfApi pdfApi = new PdfApi(clientSecret,clientId);
// 輸入PDF文檔
String sourcePDF = "PdfWithTable.pdf";
// 用於放置註釋的 PDF 頁面
int pageNumber = 1;
// 建立矩形物件來指定編輯註釋區域
// 該區域從頁面左下角開始計算
Rectangle rect = new Rectangle()
// 測量單位是點
.LLX(100.)
.LLY(700.)
.URX(200.)
.URY(600.);
// 建立 AnnotationFlags 數組
List<AnnotationFlags> flags = new ArrayList<>();
// 將 AnnotationFlag 設定為預設值
flags.add(AnnotationFlags.DEFAULT);
// 建立點的 ArrayList
List<Point> points = new ArrayList<>();
points.add(new Point().X(10.).Y(40.));
points.add(new Point().X(30.).Y(40.));
// 建立 Redact Annotation 對象
RedactionAnnotation annotation = new RedactionAnnotation();
// 設定註釋的名稱。
// 當我們在文件中有多個註釋時它很有用
annotation.setName("Name");
// 設定註釋的矩形區域
annotation.setRect(rect);
annotation.setFlags(flags);
// 設定水平對齊為中心
annotation.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 將 ZIndex 設定為 1。
annotation.setZindex(1);
// 以十六進位代碼定義 SlateBlue 顏色
Color color = new Color();
color.setA(0x00);
color.setR(0x6A);
color.setG(0x5A);
color.setB(0xCD);
// 指定註解的填滿顏色
annotation.setFillColor(color);
// 註釋最後修改的日期和時間。
annotation.setModified("05/21/2021 12:00:00.000 AM");
// 設定一個 8xN 數字數組,指定座標
// 想要刪除的內容區域。
annotation.setQuadPoint(points);
// 建立 RedactAnnotation 類型的清單對象
List<RedactionAnnotation> annotations = new ArrayList<>();
// 將先前建立的 Annotations 物件新增至 RedactAnnotation 陣列中
annotations.add(annotation);
// 將 RedactAnnotation 新增至 PDF 文檔
AsposeResponse response = pdfApi.postPageRedactionAnnotations(sourcePDF, pageNumber, annotations, null, null, true);
assertEquals(200, (int)response.getCode());

圖 1:- 已將編輯註釋新增至 PDF 檔案。
從以下網址下載上述範例中使用的範例文件
閱讀頁面修訂註釋
Aspose.PDF Cloud SDK for Java 也提供讀取 PDF 文件中現有 Redact 註解的資訊的功能。下面給出的步驟定義如何使用 Java 讀取註解詳細資訊。
// 從 https://dashboard.aspose.cloud/ 取得 ClientID 和 ClientSecret
String clientId = "29ac1517-753f-4303-b755-7185e35cf939";
String clientSecret = "c537caf71eafc8a75a5ee7813b703276";
// createPdfApi 實例
PdfApi pdfApi = new PdfApi(clientSecret,clientId);
// 輸入PDF文檔
String sourcePDF = "PdfWithTable.pdf";
// 取得文件修訂註釋
RedactionAnnotationsResponse responseAnnotations = pdfApi.getPageRedactionAnnotations(sourcePDF,1, null, null);
assertEquals(200, (int)responseAnnotations.getCode());
// 列印文件中可用註解的數量
System.out.println(responseAnnotations.getAnnotations().getList().size());
更新現有的修訂註釋
如果 PDF 文件已經包含 Redaction 註釋,我們也可以使用 API 來更新它們。下面給出了有關如何更新 PDF 文件中現有註釋的詳細資訊。
- 建立 PdfApi 的實例,同時傳遞客戶端 ID 和客戶端機密詳細資訊作為參數。
- 建立一個矩形物件來定義註解區域。它從文檔的左下角開始計算,預設單位是點。
- 建立 AnnotationFlags 清單並將 AnnotationFlags.DEFAULT 值新增至清單。
- 現在我們需要建立 RedactionAnnotation 物件並呼叫 setRect(…) 方法來定義註解的矩形區域。
- 使用 setModified(…) 方法設定註解的最後修改日期。
- 呼叫 PdfApi 的 getDocumentRedactionAnnotations(…) 方法從文件中讀取註解清單。
- 使用 responseAnnotations.getAnnotations().getList().get(0).getId() 取得特定的 Annotation 物件。
- 最後,呼叫PdfApi類別的方法putRedactionAnnotation(…)來更新PDF檔案中現有的Redaction Annotation。
// 從 https://dashboard.aspose.cloud/ 取得 ClientID 和 ClientSecret
String clientId = "29ac1517-753f-4303-b755-7185e35cf939";
String clientSecret = "c537caf71eafc8a75a5ee7813b703276";
// createPdfApi 實例
PdfApi pdfApi = new PdfApi(clientSecret,clientId);
// 輸入PDF文檔
String sourcePDF = "PdfWithAnnotations.pdf";
// 建立註釋的矩形區域
Rectangle rect = new Rectangle()
.LLX(200.)
.LLY(120.)
.URX(150.)
.URY(100.);
List<AnnotationFlags> flags = new ArrayList<>();
flags.add(AnnotationFlags.DEFAULT);
List<Point> points = new ArrayList<>();
points.add(new Point().X(10.).Y(40.));
points.add(new Point().X(30.).Y(40.));
// 建立 Redaction Annotation 對象
RedactionAnnotation annotation = new RedactionAnnotation();
// 設定註釋的名稱
annotation.setName("Name Updated");
// 設定註釋的矩形區域
annotation.setRect(rect);
annotation.setFlags(flags);
annotation.setHorizontalAlignment(HorizontalAlignment.CENTER);
annotation.setZindex(1);
annotation.setModified("01/01/2018 12:02:03.000 AM");
annotation.setQuadPoint(points);
// 以十六進位代碼定義 SlateBlue 顏色
Color color = new Color();
color.setA(0x00);
color.setR(0x6A);
color.setG(0x5A);
color.setB(0xCD);
// 指定註解的填滿顏色
annotation.setFillColor(color);
// 從文件中取得現有註釋
RedactionAnnotationsResponse responseAnnotations = pdfApi.getDocumentRedactionAnnotations(sourcePDF, null, null);
assertEquals(200, (int)responseAnnotations.getCode());
// 取得索引 0 處的註釋
String annotationId = responseAnnotations.getAnnotations().getList().get(0).getId();
// 更新索引 0 處的註釋
AsposeResponse response = pdfApi.putRedactionAnnotation(sourcePDF, annotationId, annotation, null, null, true);
assertEquals(200, (int)response.getCode());

圖 2:- 刪除註解已更新。
上述範例中使用的資源檔案可以從以下連結下載
結論
在本文中,我們討論瞭如何從 PDF 文件中刪除敏感資訊的步驟。除了 Redaction 註釋之外,此 API 還支援大量其他註釋功能,它們的詳細資訊可以在使用註釋 中找到。您可以考慮訪問產品主頁以獲取有關其功能的更多資訊。如果您有任何相關疑問,請隨時透過免費產品支援論壇聯繫。