Manage Header and Footer
Add Header in a PDF File
PdfFileStamp class allows you to add header in a PDF file. In order to add header, you first need to create object of PdfFileStamp class. You can format the header text using FormattedText class. Once you’re ready to add header in the file, you need to call AddHeader method of PdfFileStamp class. You also need to specify at least the top margin in the AddHeader method. Finally, save the output PDF file using Close method of PdfFileStamp class. The following code snippet shows you how to add header in a PDF file.
private static void AddHeader()
{
var dataDir = RunExamples.GetDataDir_AsposePdf_Images(); // Using dynamic path
// Create PdfFileStamp object using 'using' block to ensure proper disposal
using (var fileStamp = new Aspose.Pdf.Facades.PdfFileStamp())
{
// Open Document
fileStamp.BindPdf(dataDir + "sample.pdf");
// Create formatted text for the header
var formattedText = new Aspose.Pdf.Facades.FormattedText(
"Aspose - Your File Format Experts!",
System.Drawing.Color.Yellow,
System.Drawing.Color.Black,
Aspose.Pdf.Facades.FontStyle.Courier, // Updated to full enum name
Aspose.Pdf.Facades.EncodingType.Winansi, // Updated to full enum name
false,
14);
// Add header
fileStamp.AddHeader(formattedText, 10);
// Save updated PDF file with '_out' suffix
fileStamp.Save(dataDir + "AddHeader_out.pdf");
}
}
Add Footer in a PDF File
PdfFileStamp class allows you to add footer in a PDF file. In order to add footer, you first need to create object of PdfFileStamp class. You can format the footer text using FormattedText class. Once you’re ready to add footer in the file, you need to call AddFooter method of PdfFileStamp class. You also need to specify at least the bottom margin in the AddFooter method. Finally, save the output PDF file using Close method of PdfFileStamp class. The following code snippet shows you how to add footer in a PDF file.
private static void AddFooter()
{
var dataDir = RunExamples.GetDataDir_AsposePdf_Images(); // Using dynamic path
// Create PdfFileStamp object using 'using' block to ensure proper disposal
using (var fileStamp = new Aspose.Pdf.Facades.PdfFileStamp())
{
// Open Document
fileStamp.BindPdf(dataDir + "sample.pdf");
// Create formatted text for the footer
var formattedText = new Aspose.Pdf.Facades.FormattedText(
"Aspose - Your File Format Experts!",
System.Drawing.Color.Blue,
System.Drawing.Color.Gray,
Aspose.Pdf.Facades.FontStyle.Courier, // Updated to full enum name
Aspose.Pdf.Facades.EncodingType.Winansi, // Updated to full enum name
false,
14);
// Add footer
fileStamp.AddFooter(formattedText, 10);
// Save updated PDF file with '_out' suffix
fileStamp.Save(dataDir + "AddFooter_out.pdf");
}
}
Add Image in Header of an Existing PDF File
PdfFileStamp class allows you to add image in the header of a PDF file. In order to add image in header, you first need to create object of PdfFileStamp class. After that, you need to call AddHeader method of PdfFileStamp class. You can pass the image to the AddHeader method. Finally, save the output PDF file using Close method of PdfFileStamp class. The following code snippet shows you how to add image in header of PDF file.
private static void AddImageHeader()
{
var dataDir = RunExamples.GetDataDir_AsposePdf_Images(); // Using dynamic path
// Create PdfFileStamp object using 'using' block to ensure proper disposal
using (var fileStamp = new Aspose.Pdf.Facades.PdfFileStamp())
{
// Open Document
fileStamp.BindPdf(dataDir + "sample.pdf");
// Add Header
using (var fs = new FileStream(dataDir + "ImageHeader.png", FileMode.Open))
{
fileStamp.AddHeader(fs, 10); // Add image header with position offset
// Save updated PDF file with '_out' suffix
fileStamp.Save(dataDir + "AddImageHeader_out.pdf");
}
}
}
Add Image in Footer of an Existing PDF File
PdfFileStamp class allows you to add image in the footer of a PDF file. In order to add image in footer, you first need to create object of PdfFileStamp class. After that, you need to call AddFooter method of PdfFileStamp class. You can pass the image to the AddFooter method. Finally, save the output PDF file using Close method of PdfFileStamp class. The following code snippet shows you how to add image in the footer of PDF file.
private static void AddImageFooter()
{
var dataDir = RunExamples.GetDataDir_AsposePdf_Images(); // Using dynamic path
// Create PdfFileStamp object using 'using' block to ensure proper disposal
using (var fileStamp = new Aspose.Pdf.Facades.PdfFileStamp())
{
// Open Document
fileStamp.BindPdf(dataDir + "sample.pdf");
// Add footer
using (var fs = new FileStream(dataDir + "ImageFooter.png", FileMode.Open))
{
fileStamp.AddFooter(fs, 10); // Add image footer with position offset
// Save updated PDF file with '_out' suffix
fileStamp.Save(dataDir + "AddImageFooter_out.pdf");
}
}
}