
頁首是放置在頁面頂部邊緣區域的文本,而頁腳則放置在頁面的底部或頁腳。通常這些區域用於插入文件訊息,例如文件名稱、章節標題、頁碼、建立日期等。
此外,請注意,頁首和頁尾有助於以可預測的格式提供有關文件的快速信息,也有助於設定文件的不同部分。在本文中,我們將學習如何使用 Aspose.PDF Cloud SDK for Java 為 PDF 檔案的頁首和頁尾區域新增文字和影像。
使用 cURL 將文字新增至標題
cURL 命令是一種透過命令列終端存取 RESTFul API 的令人興奮的方式。由於 Aspose.PDF Cloud SDK 是按照 REST 原則開發的,因此可以透過命令提示字元輕鬆存取。但是,請注意,為了確保使用者隱私和資料完整性,我們所有的 API 僅供授權人員存取。因此,您需要先造訪 Aspose.Cloud 儀表板,如果您有 GitHub 或 Google 帳戶,只需註冊即可。否則,請點擊建立新帳戶按鈕並提供所需資訊。現在使用憑證登入儀表板並從儀表板展開應用程式部分並向下捲動至用戶端憑證部分以查看用戶端 ID 和用戶端金鑰詳細資訊。
以下命令顯示如何建立 JWT 存取令牌,該令牌將用於在 PDF 文件的標頭區域中新增文字。
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=c235e685-1aab-4cda-a95b-54afd63eb87f&client_secret=b8da4ee37494f2ef8da3c727f3a0acb9" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
使用以下 cURL 命令在 PDF 文件的標題部分中新增文字。
curl -X POST "https://api.aspose.cloud/v3.0/pdf/PdfWithAnnotations.pdf/header/text?startPageNumber=2&endPageNumber=2" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-H "Content-Type: application/json" \
-d "{ \"Background\": true, \"HorizontalAlignment\": \"CENTER\", \"Opacity\": 0.8, \"Rotate\": \"None\", \"RotateAngle\": 0, \"XIndent\": 0, \"YIndent\": 0, \"Zoom\": 1, \"TextAlignment\": \"CENTER\", \"Value\": \"Aspose.PDF Cloud SDK for Java via cURL\", \"TextState\": { \"FontSize\": 16, \"Font\": \"Arial\", \"ForegroundColor\": { \"A\": 0, \"R\": 0xDA, \"G\": 0xA5, \"B\": 0x20 }, \"BackgroundColor\": { \"A\": 0, \"R\": 0xFA, \"G\": 0xEB, \"B\": 0xD7 }, }, \"LeftMargin\": 1, \"TopMargin\": 20, \"RightMargin\": 200}"

圖 1:- 透過 cURL 指令新增文字標題。
本例中使用的範例檔案可以從以下位置下載:
使用 Java 在 PDF 中新增文字標題
為了使用 SDK,您需要先從 Maven 或 GitHub 安裝它。下面給出了有關如何在 Maven 建置專案中下載和使用 Aspose.Pdf.jar 的詳細資訊。
在您的 pom.xml 檔案中新增以下依賴項。
<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>
下面給出瞭如何滿足此要求的步驟。
- 首先,建立 PdfApi 的實例,同時提供 ClientSecret 和 ClientId 詳細資訊
- 其次,建立一個 Color 物件來定義 ForeGround 和 BackGround 顏色細節。
- 其次,建立 TextState 類別的物件。
- 使用 fontSize(..) 方法設定字體大小詳細資料。 font(..) 方法定義字體。 foregroundColor(…) 方法定義文字前景細節,backgroundColor(…) 方法定義背景資訊。
- 現在建立 TextHeader 物件並定義邊距細節。
- 使用 value(..) 方法指定文字標題物件的內容。
- 現在呼叫 textState(…) 方法並將上面建立的 TextState 實例作為參數傳遞。
- 為了在頁面內容後面顯示 Header,將真值作為參數傳遞給 TextHeader 類別的 background(…) 方法。
- 現在,為了在左側呈現圖像,將 HorizontalAlignment.CENTER 值傳遞給 HorizontalAlignment(…) 方法。
- opacity(…) 方法表示印章的不透明度。預設值為 1.0。
- 一旦我們設定了縮放值,就建立 AsposeResponse 物件。
- 最後,呼叫 PdfApi.postDocumentTextHeader(…) 方法並傳遞來源 PDF 檔案名稱、TextHeader 物件、開始和結束頁面(需要添加印章的位置)作為參數。
// 從 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";
// 放置頁首影像的 PDF 起始頁
int startPage = 2;
// PDF 的結束頁放置頁首影像
int endPage = 3;
// 以十六進位代碼指定前景色為 SlateBlue
Color foregroundColor = new Color();
foregroundColor.setA(0x00);
foregroundColor.setR(0x6A);
foregroundColor.setG(0x5A);
foregroundColor.setB(0xCD);
// 以十六進位代碼指定背景顏色為 AntiqueWhite
Color backgroundColor = new Color();
backgroundColor.setA(0x00);
backgroundColor.setR(0xFA);
backgroundColor.setG(0xEB);
backgroundColor.setB(0xD7);
// 建立 TextState 物件來定義字體大小、字體和顏色細節
TextState textState = new TextState()
.fontSize(16.)
.foregroundColor(foregroundColor)
.backgroundColor(backgroundColor)
.font("Arial");
// 建立 TextHeader 物件的實例
TextHeader headerObject = new TextHeader()
.leftMargin(1.)
.rightMargin(200.)
.topMargin(10.)
// 要呈現的文字標題的內容
.value("Aspose.PDF Cloud SDK For Java")
// 設定對齊方式為居中
.textAlignment(com.aspose.asposecloudpdf.model.HorizontalAlignment.CENTER)
.textState(textState);
// 設定頁面內容後面的頁首文本
headerObject.background(true)
// 將文字標題對齊方式設定為居中
.horizontalAlignment(com.aspose.asposecloudpdf.model.HorizontalAlignment.CENTER)
// 將文字標題的不透明度設為 0.8
.opacity(0.8)
.rotate(com.aspose.asposecloudpdf.model.Rotation.NONE)
.rotateAngle(0.)
.xindent(2.0)
.yindent(5.0)
.zoom(1.0);
// 呼叫 API 方法將文字標題新增至 PDF
AsposeResponse response = pdfApi.postDocumentTextHeader(sourcePDF, headerObject,startPage, endPage, null, null);
assertEquals(200, (int)response.getCode());

圖 2:使用 Java 新增的文字標題的預覽。
為了在頁腳區域新增文本,請建立 TextFooter 類別的實例並呼叫 PdfApi 類別的 postDocumentTextFooter(…) 方法。其餘步驟和細節與上面提到的添加文字標題相同。
使用 cURL 在 PDF 標題中新增圖像
我們可以使用 cURL 指令,輕鬆地使用 PostDocumentImageHeader API 在 PDF 檔案的標題部分中新增圖像檔案。新增影像標記時,我們可以選擇指定水平對齊、不透明度、旋轉角度、影像的縮放比例、寬度、高度、左邊界、上邊距、右邊距等詳細資訊。
name 參數定義輸入/來源 PDF 文檔,imageHeader 指定要加蓋印章的影像文件,startPageNumber 屬性定義包含影像印章的 PDF 文件的起始頁和 endPageNumber 結束頁。在下面的 cURL 命令中,Background 屬性設定為 false,以便圖片可見。 HorizontalAlignment 指定為 Left,Opacity 值定義為 0.8,Zoom 因子設定為 1,Width 定義為 250,Height 定義為 80 點,RightMargin 指定為 500 點。
curl -X POST "https://api.aspose.cloud/v3.0/pdf/PdfWithAnnotations.pdf/header/image?startPageNumber=2&endPageNumber=2" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-H "Content-Type: application/json" \
-d "{"Background":false,"HorizontalAlignment":'Left',"Opacity": .8,"Rotate":'None',"RotateAngle": 0,"XIndent": 2,"YIndent": 5,"Zoom": 1,"FileName": 'asposetoolsnew.png',"Width": 250,"Height": 80,"LeftMargin": 1,"TopMargin": 10,"RightMargin": 500}"

圖像 3:PDF 標題部分的圖像印章。
上述範例中使用的範例檔案可以從以下連結下載:
使用 Java 在 Header 中加入圖像
Aspose.PDF Cloud SDK for Java 提供了一個顯著的功能,可以用更少的程式碼行在 PDF 檔案中加入影像頭。
以下是在 PDF 文件中新增圖像標題的步驟
- 建立 PdfApi 的對象,同時提供 ClientSecret 和 ClientId 詳細資料。
- 其次,建立 ImageHeader 類別的物件。
- 使用 ImageHeader 類別的 width(..) 和 height(…) 方法設定影像的寬度和高度細節。
- 現在使用 fileName(…) 方法設定標題圖像的路徑詳細資料。
- 為了顯示頁面內容後面的圖像,將 false 參數傳遞給 background(…) 方法。
- 現在,為了在左側呈現影像,將 HorizontalAlignment.LEFT 值傳遞給 HorizontalAlignment(…) 方法。
- opacity(…) 方法表示印章的不透明度。預設值為 1.0。
- 一旦我們設定了縮放值,就建立 AsposeResponse 物件。
- 最後,呼叫 PdfApi.postDocumentImageHeader(…) 方法,同時將來源 PDF 檔案名稱、ImageHeader 物件、起始頁和結束頁(需要新增印章的位置)作為參數傳遞給此方法。
// 從 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";
// 放置頁首影像的 PDF 起始頁
int startPage = 2;
// PDF 的結束頁放置頁首影像
int endPage = 3;
// 建立 ImageHeader 物件的實例
ImageHeader headerObject = new ImageHeader()
.width(250.0)
.height(80.0)
.leftMargin(1.)
.rightMargin(500.)
.topMargin(10.)
.fileName("asposetoolsnew.png");
// 將標題圖像置於前台/可見
headerObject.background(false)
// 將影像標題對齊方式設定為左對齊
.horizontalAlignment(com.aspose.asposecloudpdf.model.HorizontalAlignment.LEFT)
.opacity(0.8)
.rotate(com.aspose.asposecloudpdf.model.Rotation.NONE)
.rotateAngle(0.)
.xindent(2.0)
.yindent(5.0)
.zoom(1.0);
// 呼叫 API 方法在 PDF 中新增標題圖像
AsposeResponse response = pdfApi.postDocumentImageHeader(sourcePDF, headerObject,startPage, endPage, null, null);
為了在頁腳區域新增映像,請建立 ImageFooter 類別的物件並呼叫 pdfApi 類別的 postDocumentImageFooter(…) 方法。其餘步驟與上面標題部分的圖像的解釋相同。
結論
在本文中,我們討論瞭如何透過 cURL 命令存取 Aspose.PDF Cloud 以在 PDF 文件的頁首/頁尾區域中添加文字和圖像的步驟。我們也探討了安裝 Aspose.PDF Cloud SDK for Java 的步驟,並使用 Java 語言在 PDF 檔案的頁首或頁尾部分添加文字和圖像。有關 API 的其他令人興奮的功能的更多詳細信息,請瀏覽產品文件。
我們所有的雲端 SDK 都是開源的,因此可以從 GitHub 下載完整的原始碼。