[英]Change color of PDF annotations with iText
我想更改我所有PDF突出顯示注釋的顏色。 有功能com.itextpdf.text.pdf.PdfAnnotation.setColor(BaseColor color)
,該如何調用該功能? 下面是迭代所有突出顯示注釋的代碼。
public static void main(String[] args) {
try {
// Reads and parses a PDF document
PdfReader reader = new PdfReader("Test.pdf");
// For each PDF page
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
// Get a page a PDF page
PdfDictionary page = reader.getPageN(i);
// Get all the annotations of page i
PdfArray annotsArray = page.getAsArray(PdfName.ANNOTS);
// If page does not have annotations
if (page.getAsArray(PdfName.ANNOTS) == null) {
continue;
}
// For each annotation
for (int j = 0; j < annotsArray.size(); ++j) {
// For current annotation
PdfDictionary curAnnot = annotsArray.getAsDict(j);
if (PdfName.HIGHLIGHT.equals(curAnnot.get(PdfName.SUBTYPE))) {
//how to access com.itextpdf.text.pdf.PdfAnnotation.setColor(BaseColor color)
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
不幸的是,iText PdfAnnotation
類僅用於從頭開始創建新的注釋 ,而不是預先存在的包裝器 。
因此,您基本上必須使用類似下面這樣的低級方法來操作PdfDictionary curAnnot
curAnnot.put(PdfName.C, new PdfArray(new float[]{1, 0, 0}));
名稱C的此項指定為
C數組(可選; PDF 1.1)在0.0到1.0范圍內的數字數組,表示用於以下目的的顏色:
注釋圖標關閉時的背景
注釋的彈出窗口的標題欄
鏈接注釋的邊框
數組元素的數量確定了應在其中定義顏色的顏色空間:
0無顏色 透明
1個設備灰色
3個設備RGB
4個設備CMYK
(表164 –所有注釋詞典共有的條目– ISO 32000-1 )
PS:不要忘了最終使用類似的方法存儲更改
new PdfStamper(reader, new FileOutputStream("changed.pdf")).close();
PdfAnnotation annotation = new PdfAnnotation(your_pdf_writer, new Rectangle(your_rectangle_information_here));
annotation.setColor(your_color_here);
您還需要一個PdfWriter來寫到pdf以及有關您在PDF上繪制的矩形的信息。
希望這可以幫助!
參考: 創建修飾詞itext
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.