翻譯|使用教程|編輯:李顯亮|2020-09-15 11:26:30.200|閱讀 572 次
概述:在本系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何管理PDF文件的頁眉和頁腳。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何管理PDF文件的頁眉和頁腳。
>>Aspose.PDF for .NET更新至最新版v20.9,歡迎下載體驗。
使用TextStamp類在PDF文件的標題中添加文本。TextStamp類提供創建基于文本的圖章所需的屬性,例如字體大小,字體樣式和字體顏色等。為了在標題中添加文本,您需要使用必需的屬性創建Document對象和TextStamp對象。之后,可以調用Page的AddStamp方法將文本添加到PDF的標題中。
需要以這樣的方式設置TopMargin屬性,使其調整PDF標題區域中的文本。您還需要將HorizontalAlignment設置為Center,將VerticalAlignment設置為Top。以下代碼段顯示了如何在PDF文件的標題中添加文本。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Open document Document pdfDocument = new Document(dataDir+ "TextinHeader.pdf"); // Create header TextStamp textStamp = new TextStamp("Header Text"); // Set properties of the stamp textStamp.TopMargin = 10; textStamp.HorizontalAlignment = HorizontalAlignment.Center; textStamp.VerticalAlignment = VerticalAlignment.Top; // Add header on all pages foreach (Page page in pdfDocument.Pages) { page.AddStamp(textStamp); } // Save updated document pdfDocument.Save(dataDir+ "TextinHeader_out.pdf");
使用TextStamp類在PDF文件的頁腳中添加文本。TextStamp類提供創建基于文本的圖章所需的屬性,例如字體大小,字體樣式和字體顏色等。為了在頁腳中添加文本,您需要使用必需的屬性創建Document對象和TextStamp對象。之后,可以調用Page的AddStamp方法在PDF頁腳中添加文本。
您需要設置底 邊距屬性以這樣一種方式,它在調整您的PDF的頁腳區域中的文本。您還需要將HorizontalAlignment設置為Center,將VerticalAlignment設置為Bottom。
以下代碼段顯示了如何在PDF文件的頁腳中添加文本。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Open document Document pdfDocument = new Document(dataDir+ "TextinFooter.pdf"); // Create footer TextStamp textStamp = new TextStamp("Footer Text"); // Set properties of the stamp textStamp.BottomMargin = 10; textStamp.HorizontalAlignment = HorizontalAlignment.Center; textStamp.VerticalAlignment = VerticalAlignment.Bottom; // Add footer on all pages foreach (Page page in pdfDocument.Pages) { page.AddStamp(textStamp); } dataDir = dataDir + "TextinFooter_out.pdf"; // Save updated PDF file pdfDocument.Save(dataDir);
使用Image Stamp類將圖像添加到PDF文件的標題中。Image Stamp類提供創建基于圖像的圖章所需的屬性,例如字體大小,字體樣式和字體顏色等。為了在標題中添加圖像,您需要使用必需的屬性來創建Document對象和Image Stamp對象。之后,可以調用Page的AddStamp方法將圖像添加到PDF的標題中。
您需要以這樣的方式設置TopMargin屬性,以調整PDF標題區域中的圖像。您還需要將HorizontalAlignment設置為Center,將VerticalAlignment設置為Top。
以下代碼段顯示了如何在PDF文件的標題中添加圖像。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Open document Document pdfDocument = new Document(dataDir+ "ImageinHeader.pdf"); // Create header ImageStamp imageStamp = new ImageStamp(dataDir+ "aspose-logo.jpg"); // Set properties of the stamp imageStamp.TopMargin = 10; imageStamp.HorizontalAlignment = HorizontalAlignment.Center; imageStamp.VerticalAlignment = VerticalAlignment.Top; // Add header on all pages foreach (Page page in pdfDocument.Pages) { page.AddStamp(imageStamp); } dataDir = dataDir + "ImageinHeader_out.pdf"; // Save updated document pdfDocument.Save(dataDir);
使用Image Stamp類將圖像添加到PDF文件的頁腳中。Image Stamp類提供了創建基于圖像的圖章所需的屬性,例如字體大小,字體樣式和字體顏色等。為了在頁腳中添加圖像,您需要使用必需的屬性創建Document對象和Image Stamp對象。之后,您可以調用Page的AddStamp方法將圖像添加到PDF的頁腳中。
您需要設置“ Bottom Margin”屬性,以便它可以調整PDF頁腳區域中的圖像。您還需要將HorizontalAlignment設置為Center,將VerticalAlignment設置為Bottom。
以下代碼段顯示了如何在PDF文件的頁腳中添加圖像。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Open document Document pdfDocument = new Document(dataDir+ "ImageInFooter.pdf"); // Create footer ImageStamp imageStamp = new ImageStamp(dataDir+ "aspose-logo.jpg"); // Set properties of the stamp imageStamp.BottomMargin = 10; imageStamp.HorizontalAlignment = HorizontalAlignment.Center; imageStamp.VerticalAlignment = VerticalAlignment.Bottom; // Add footer on all pages foreach (Page page in pdfDocument.Pages) { page.AddStamp(imageStamp); } dataDir = dataDir + "ImageInFooter_out.pdf"; // Save updated PDF file pdfDocument.Save(dataDir);
我們知道可以使用TopMargin或Bottom Margin屬性在文檔的頁眉/頁腳部分中添加TextStamp ,但是有時我們可能需要在單個PDF文檔中添加多個頁眉/頁腳。為了實現此要求,我們將創建單個TextStamp對象(對象的數量取決于所需的Header / Footers 的數量)并將其添加到PDF文檔中。我們還可以為單個圖章對象指定不同的格式信息。在下面的示例中,我們創建了Document對象和三個TextStamp對象,然后使用了Page的AddStamp方法。在PDF的標題部分添加文本。以下代碼段顯示了如何在PDF文件的頁腳中添加圖像。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Open source document Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir+ "AddingDifferentHeaders.pdf"); // Create three stamps Aspose.Pdf.TextStamp stamp1 = new Aspose.Pdf.TextStamp("Header 1"); Aspose.Pdf.TextStamp stamp2 = new Aspose.Pdf.TextStamp("Header 2"); Aspose.Pdf.TextStamp stamp3 = new Aspose.Pdf.TextStamp("Header 3"); // Set stamp alignment (place stamp on page top, centered horiznotally) stamp1.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top; stamp1.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center; // Specify the font style as Bold stamp1.TextState.FontStyle = FontStyles.Bold; // Set the text fore ground color information as red stamp1.TextState.ForegroundColor = Color.Red; // Specify the font size as 14 stamp1.TextState.FontSize = 14; // Now we need to set the vertical alignment of 2nd stamp object as Top stamp2.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top; // Set Horizontal alignment information for stamp as Center aligned stamp2.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center; // Set the zooming factor for stamp object stamp2.Zoom = 10; // Set the formatting of 3rd stamp object // Specify the Vertical alignment information for stamp object as TOP stamp3.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top; // Set the Horizontal alignment inforamtion for stamp object as Center aligned stamp3.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center; // Set the rotation angle for stamp object stamp3.RotateAngle = 35; // Set pink as background color for stamp stamp3.TextState.BackgroundColor = Color.Pink; // Change the font face information for stamp to Verdana stamp3.TextState.Font = FontRepository.FindFont("Verdana"); // First stamp is added on first page; doc.Pages[1].AddStamp(stamp1); // Second stamp is added on second page; doc.Pages[2].AddStamp(stamp2); // Third stamp is added on third page. doc.Pages[3].AddStamp(stamp3); dataDir = dataDir + "multiheader_out.pdf"; // Save the updated document doc.Save(dataDir);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn