PDF管理控件Aspose.PDF for .Net使用教程(四十六):管理PDF文件的页眉和页脚

在本系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何管理PDF文件的页眉和页脚。

Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用AdobeAcrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。

在接下来的系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何管理PDF文件的页眉和页脚。

>>Aspose.PDF for .NET更新至最新版v20.9,欢迎下载体验。

整合所有格式API处理控件Aspose永久授权正在网火热销售中,新购乐享85折起!联系客服立马1分钟了解全部咨询!


在PDF文件标题中添加文本

使用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 documentDocument pdfDocument = new Document(dataDir+ "TextinHeader.pdf");// Create headerTextStamp textStamp = new TextStamp("Header Text");// Set properties of the stamptextStamp.TopMargin = 10;textStamp.HorizontalAlignment = HorizontalAlignment.Center;textStamp.VerticalAlignment = VerticalAlignment.Top;// Add header on all pagesforeach (Page page in pdfDocument.Pages){    page.AddStamp(textStamp);}// Save updated documentpdfDocument.Save(dataDir+ "TextinHeader_out.pdf");

在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 documentDocument pdfDocument = new Document(dataDir+ "TextinFooter.pdf");// Create footerTextStamp textStamp = new TextStamp("Footer Text");// Set properties of the stamptextStamp.BottomMargin = 10;textStamp.HorizontalAlignment = HorizontalAlignment.Center;textStamp.VerticalAlignment = VerticalAlignment.Bottom;// Add footer on all pagesforeach (Page page in pdfDocument.Pages){    page.AddStamp(textStamp);}dataDir = dataDir + "TextinFooter_out.pdf";// Save updated PDF filepdfDocument.Save(dataDir);

在PDF文件的标题中添加图像

使用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 documentDocument pdfDocument = new Document(dataDir+ "ImageinHeader.pdf");// Create headerImageStamp imageStamp = new ImageStamp(dataDir+ "aspose-logo.jpg");// Set properties of the stampimageStamp.TopMargin = 10;imageStamp.HorizontalAlignment = HorizontalAlignment.Center;imageStamp.VerticalAlignment = VerticalAlignment.Top;// Add header on all pagesforeach (Page page in pdfDocument.Pages){    page.AddStamp(imageStamp);}dataDir = dataDir + "ImageinHeader_out.pdf";// Save updated documentpdfDocument.Save(dataDir);

在PDF文件的页脚中添加图像

使用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 documentDocument pdfDocument = new Document(dataDir+ "ImageInFooter.pdf");// Create footerImageStamp imageStamp = new ImageStamp(dataDir+ "aspose-logo.jpg");// Set properties of the stampimageStamp.BottomMargin = 10;imageStamp.HorizontalAlignment = HorizontalAlignment.Center;imageStamp.VerticalAlignment = VerticalAlignment.Bottom;// Add footer on all pagesforeach (Page page in pdfDocument.Pages){    page.AddStamp(imageStamp);}dataDir = dataDir + "ImageInFooter_out.pdf";// Save updated PDF filepdfDocument.Save(dataDir);

在一个PDF文件中添加不同的标题

我们知道可以使用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 documentAspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir+ "AddingDifferentHeaders.pdf");// Create three stampsAspose.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 Boldstamp1.TextState.FontStyle = FontStyles.Bold;// Set the text fore ground color information as redstamp1.TextState.ForegroundColor = Color.Red;// Specify the font size as 14stamp1.TextState.FontSize = 14;// Now we need to set the vertical alignment of 2nd stamp object as Topstamp2.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top;// Set Horizontal alignment information for stamp as Center alignedstamp2.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;// Set the zooming factor for stamp objectstamp2.Zoom = 10;// Set the formatting of 3rd stamp object// Specify the Vertical alignment information for stamp object as TOPstamp3.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top;// Set the Horizontal alignment inforamtion for stamp object as Center alignedstamp3.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;// Set the rotation angle for stamp objectstamp3.RotateAngle = 35;// Set pink as background color for stampstamp3.TextState.BackgroundColor = Color.Pink;// Change the font face information for stamp to Verdanastamp3.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 documentdoc.Save(dataDir);

还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年8月12日
下一篇 2020年8月12日

相关推荐

发表回复

登录后才能评论