翻譯|使用教程|編輯:李顯亮|2020-09-15 11:26:30.200|閱讀 584 次
概述:在本系列教程中,將為開發(fā)者帶來(lái)Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。本文將介紹如何管理PDF文件的頁(yè)眉和頁(yè)腳。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺(tái)應(yīng)用程序中執(zhí)行文檔管理和操作任務(wù)。API可以輕松用于生成、修改、轉(zhuǎn)換、渲染、保護(hù)和打印PDF文檔,而無(wú)需使用Adobe Acrobat。此外,API還提供PDF壓縮選項(xiàng),表格創(chuàng)建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務(wù),擴(kuò)展的安全控制和自定義字體處理。
在接下來(lái)的系列教程中,將為開發(fā)者帶來(lái)Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。本文將介紹如何管理PDF文件的頁(yè)眉和頁(yè)腳。
>>Aspose.PDF for .NET更新至最新版v20.9,歡迎下載體驗(yàn)。
使用TextStamp類在PDF文件的標(biāo)題中添加文本。TextStamp類提供創(chuàng)建基于文本的圖章所需的屬性,例如字體大小,字體樣式和字體顏色等。為了在標(biāo)題中添加文本,您需要使用必需的屬性創(chuàng)建Document對(duì)象和TextStamp對(duì)象。之后,可以調(diào)用Page的AddStamp方法將文本添加到PDF的標(biāo)題中。
需要以這樣的方式設(shè)置TopMargin屬性,使其調(diào)整PDF標(biāo)題區(qū)域中的文本。您還需要將HorizontalAlignment設(shè)置為Center,將VerticalAlignment設(shè)置為Top。以下代碼段顯示了如何在PDF文件的標(biāo)題中添加文本。
// 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文件的頁(yè)腳中添加文本。TextStamp類提供創(chuàng)建基于文本的圖章所需的屬性,例如字體大小,字體樣式和字體顏色等。為了在頁(yè)腳中添加文本,您需要使用必需的屬性創(chuàng)建Document對(duì)象和TextStamp對(duì)象。之后,可以調(diào)用Page的AddStamp方法在PDF頁(yè)腳中添加文本。
您需要設(shè)置底 邊距屬性以這樣一種方式,它在調(diào)整您的PDF的頁(yè)腳區(qū)域中的文本。您還需要將HorizontalAlignment設(shè)置為Center,將VerticalAlignment設(shè)置為Bottom。
以下代碼段顯示了如何在PDF文件的頁(yè)腳中添加文本。
// 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文件的標(biāo)題中。Image Stamp類提供創(chuàng)建基于圖像的圖章所需的屬性,例如字體大小,字體樣式和字體顏色等。為了在標(biāo)題中添加圖像,您需要使用必需的屬性來(lái)創(chuàng)建Document對(duì)象和Image Stamp對(duì)象。之后,可以調(diào)用Page的AddStamp方法將圖像添加到PDF的標(biāo)題中。
您需要以這樣的方式設(shè)置TopMargin屬性,以調(diào)整PDF標(biāo)題區(qū)域中的圖像。您還需要將HorizontalAlignment設(shè)置為Center,將VerticalAlignment設(shè)置為Top。
以下代碼段顯示了如何在PDF文件的標(biāo)題中添加圖像。
// 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文件的頁(yè)腳中。Image Stamp類提供了創(chuàng)建基于圖像的圖章所需的屬性,例如字體大小,字體樣式和字體顏色等。為了在頁(yè)腳中添加圖像,您需要使用必需的屬性創(chuàng)建Document對(duì)象和Image Stamp對(duì)象。之后,您可以調(diào)用Page的AddStamp方法將圖像添加到PDF的頁(yè)腳中。
您需要設(shè)置“ Bottom Margin”屬性,以便它可以調(diào)整PDF頁(yè)腳區(qū)域中的圖像。您還需要將HorizontalAlignment設(shè)置為Center,將VerticalAlignment設(shè)置為Bottom。
以下代碼段顯示了如何在PDF文件的頁(yè)腳中添加圖像。
// 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屬性在文檔的頁(yè)眉/頁(yè)腳部分中添加TextStamp ,但是有時(shí)我們可能需要在單個(gè)PDF文檔中添加多個(gè)頁(yè)眉/頁(yè)腳。為了實(shí)現(xiàn)此要求,我們將創(chuàng)建單個(gè)TextStamp對(duì)象(對(duì)象的數(shù)量取決于所需的Header / Footers 的數(shù)量)并將其添加到PDF文檔中。我們還可以為單個(gè)圖章對(duì)象指定不同的格式信息。在下面的示例中,我們創(chuàng)建了Document對(duì)象和三個(gè)TextStamp對(duì)象,然后使用了Page的AddStamp方法。在PDF的標(biāo)題部分添加文本。以下代碼段顯示了如何在PDF文件的頁(yè)腳中添加圖像。
// 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);
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn