How to add, delete and modify the header-footer of PDF file
Contents
System requirements
Platform: Windows, Mac, Linux
Programming Language: C,C++, C#, Dotnetcore, Java, Python, Objective-C
License Key requirement: standard license key
SDK Version: Foxit PDF SDK 7.2 or later
Located at the top and bottom of the PDF page, it can display page numbers, dates, and text.
Call PDFDoc:: GetEditableHeaderFooter() to get the editable header and footer object. After adjusting the header and footer content, call PDFDoc:: UpdateHeaderFooter() to update the header and footer.
// The document has already been loaded.
...
//Get the editable header footer object.
HeaderFooter headerfooter = doc.GetEditableHeaderFooter();
// Update header-footer
HeaderFooterContentGenerator contentgenerator;
//Set the date format to"m/d/yyyy "
contentgenerator.AddDate(HeaderFooterContentGenerator::e_DateFormatMSlashDSlashYYYY);
contentgenerator.AddString(L" foxit");
//Set the content of the right header.
HeaderFooterContent content = headerfooter.content;
content.header_right_content = contentgenerator.GenerateContent();
headerfooter.content = content;
//Update the header-footer.
doc.UpdateHeaderFooter(headerfooter);
Calling PDFDoc:: AddHeaderFooter() to add a header and footer.
HeaderFooter headerfooter = doc.GetEditableHeaderFooter();
...
// Add header-footer
headerfooter.text_color = 0x00FFFF;
headerfooter.page_range = PageNumberRange(1, doc.GetPageCount(), foxit::common::Range::e_Odd);
headerfooter.has_fixedsize_for_print = false;
headerfooter.has_text_shrinked = false;
headerfooter.start_page_number = 2;
headerfooter.page_margin = RectF(36, 70, 36, 70);
headerfooter.text_size = 8;
doc.AddHeaderFooter(headerfooter);
Calling PDFDoc::RemoveAllHeaderFooters() to remove all header-footer.
// Remove all header-footers
doc.RemoveAllHeaderFooters();
Code example
C++
#include "../../../include/common/fs_common.h"
#include "../../../include/pdf/fs_pdfdoc.h"
#include "../../../include/pdf/fs_headerfooter.h"
...
HeaderFooter headerfooter = doc.GetEditableHeaderFooter();
// Update header-footer
HeaderFooterContentGenerator contentgenerator;
contentgenerator.AddDate(HeaderFooterContentGenerator::e_DateFormatMSlashDSlashYYYY);
contentgenerator.AddString(L" foxit");
HeaderFooterContent content = headerfooter.content;
content.header_right_content = contentgenerator.GenerateContent();
headerfooter.content = content;
doc.UpdateHeaderFooter(headerfooter);
// Add header-footer
headerfooter.text_color = 0x00FFFF;
headerfooter.page_range = PageNumberRange(1, doc.GetPageCount(), foxit::common::Range::e_Odd);
headerfooter.has_fixedsize_for_print = false;
headerfooter.has_text_shrinked = false;
headerfooter.start_page_number = 2;
headerfooter.page_margin = RectF(36, 70, 36, 70);
headerfooter.text_size = 8;
doc.AddHeaderFooter(headerfooter);
// Remove all header-footers
doc.RemoveAllHeaderFooters();
...
C
#include "../../../include/fs_basictypes_c.h"
#include "../../../include/fs_common_c.h"
#include "../../../include/fs_pdfdoc_c.h"
#include "../../../include/fs_headerfooter_c.h"
...
FSDK_WStr_Init(&headerfooter.content.footer_center_content);
FSDK_WStr_Init(&headerfooter.content.footer_left_content);
FSDK_WStr_Init(&headerfooter.content.footer_right_content);
FSDK_WStr_Init(&headerfooter.content.header_center_content);
FSDK_WStr_Init(&headerfooter.content.header_left_content);
FSDK_WStr_Init(&headerfooter.content.header_right_content);
FSDK_PDFDoc_GetEditableHeaderFooter(doc, &headerfooter);
// Update header-footer
FSDK_HeaderFooterContentGenerator_Create(&contentgenerator);
FSDK_HeaderFooterContentGenerator_AddDate(contentgenerator, e_FSDateFormatMSlashDSlashYYYY);
FSDK_HeaderFooterContentGenerator_AddString(contentgenerator, L" foxit");
content = headerfooter.content;
FSDK_WStr_Init(&return_generatecontent);
FSDK_HeaderFooterContentGenerator_GenerateContent(contentgenerator, &return_generatecontent);
content.header_right_content = return_generatecontent;
headerfooter.content = content;
FSDK_PDFDoc_UpdateHeaderFooter(doc, headerfooter);
// Add header-footer
headerfooter.text_color = 0x00FFFF;
FSDK_PDFDoc_GetPageCount(doc, &page_count);
page_number_range.end_number = page_count;
page_number_range.filter = e_FSOdd;
page_number_range.start_number = 1;
headerfooter.page_range = page_number_range;
headerfooter.has_fixedsize_for_print = FALSE;
headerfooter.has_text_shrinked = FALSE;
headerfooter.start_page_number = 2;
rectF.left = 36;
rectF.bottom = 70;
rectF.right = 36;
rectF.top = 70;
headerfooter.page_margin = rectF;
headerfooter.text_size = 8;
FSDK_PDFDoc_AddHeaderFooter(doc, headerfooter);
// Remove all header-footers
FSDK_PDFDoc_RemoveAllHeaderFooters(doc, &return_removeall_headerfooters);
...
Java
import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.pdf.PageNumberRange;
import com.foxit.sdk.pdf.HeaderFooter;
import com.foxit.sdk.pdf.HeaderFooterContent;
import com.foxit.sdk.pdf.HeaderFooterContentGenerator;
import com.foxit.sdk.common.fxcrt.RectF;
...
HeaderFooter headerfooter = pdfDoc.getEditableHeaderFooter();
// Update header-footer
HeaderFooterContentGenerator contentgenerator = new HeaderFooterContentGenerator();
contentgenerator.addDate(e_DateFormatMSlashDSlashYYYY);
contentgenerator.addString(" foxit");
HeaderFooterContent content = headerfooter.getContent();
content.setHeader_right_content(contentgenerator.generateContent());
headerfooter.setContent(content);
pdfDoc.updateHeaderFooter(headerfooter);
// Add header-footer
headerfooter.setText_color(0x00FFFF);
headerfooter.setPage_range(new PageNumberRange(1, pdfDoc.getPageCount(), com.foxit.sdk.common.Range.e_Odd));
headerfooter.setHas_fixedsize_for_print(false);
headerfooter.setHas_text_shrinked(false);
headerfooter.setStart_page_number(2);
headerfooter.setPage_margin(new RectF(36,70,36,70));
headerfooter.setText_size(8);
pdfDoc.addHeaderFooter(headerfooter);
//Remove all header-footers
pdfDoc.removeAllHeaderFooters();
...
C#
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
...
using (HeaderFooter headerfooter = doc.GetEditableHeaderFooter())
{
// Update header-footer
using (HeaderFooterContentGenerator contentgenerator = new HeaderFooterContentGenerator())
{
contentgenerator.AddDate(HeaderFooterContentGenerator.DateFormatType.e_DateFormatMSlashDSlashYYYY);
contentgenerator.AddString(" foxit");
HeaderFooterContent content = headerfooter.content;
content.header_right_content = contentgenerator.GenerateContent();
headerfooter.content = content;
doc.UpdateHeaderFooter(headerfooter);
// Add header-footer
headerfooter.text_color = 0x00FFFF;
headerfooter.page_range = new PageNumberRange(1, doc.GetPageCount(), foxit.common.Range.Filter.e_Odd);
headerfooter.has_fixedsize_for_print = false;
headerfooter.has_text_shrinked = false;
headerfooter.start_page_number = 2;
headerfooter.page_margin = new RectF(36, 70, 36, 70);
headerfooter.text_size = 8;
doc.AddHeaderFooter(headerfooter);
output_file = output_path + "HeaderFooter_AddHeaderFooter.pdf";
doc.SaveAs(output_file, (int)PDFDoc.SaveFlags.e_SaveFlagNoOriginal);
Console.WriteLine("Add PDF document header-footer.");
// Remove all header-footers
doc.RemoveAllHeaderFooters();
output_file = output_path + "HeaderFooter_RemoveAll.pdf";
doc.SaveAs(output_file, (int)PDFDoc.SaveFlags.e_SaveFlagNoOriginal);
Console.WriteLine("Remove PDF document all header-footers.");
}
}
...
Object-C
#include "FSPDFObjC.h"
...
FSHeaderFooter* headerfooter = [doc getEditableHeaderFooter];
// Update header-footer
FSHeaderFooterContentGenerator* contentgenerator = [[FSHeaderFooterContentGenerator alloc] init];
[contentgenerator addDate:FSHeaderFooterContentGeneratorDateFormatMSlashDSlashYYYY];
[contentgenerator addString:@" foxit"];
FSHeaderFooterContent* content = headerfooter.content;
content.header_right_content = [contentgenerator generateContent];
[doc updateHeaderFooter:headerfooter];
// Add header-footer
headerfooter.text_color = 0x00FFFF;
headerfooter.page_range = [[FSPageNumberRange alloc] initWithStart_number:1 end_number:[doc getPageCount] filter:FSRangeOdd];
headerfooter.has_fixedsize_for_print = false;
headerfooter.has_text_shrinked = false;
headerfooter.start_page_number = 2;
headerfooter.page_margin = [[FSRectF alloc] initWithLeft1:36 bottom1:70 right1:36 top1:70];
headerfooter.text_size = 8;
[doc addHeaderFooter:headerfooter];
//Remove all header-footers
[doc removeAllHeaderFooters];
...
Python
...
headerfooter = doc.GetEditableHeaderFooter()
# Update header-footer
contentgenerator = HeaderFooterContentGenerator()
contentgenerator.AddDate(HeaderFooterContentGenerator.e_DateFormatMSlashDSlashYYYY)
contentgenerator.AddString(" foxit")
content = headerfooter.content
content.header_right_content = contentgenerator.GenerateContent()
headerfooter.content = content
doc.UpdateHeaderFooter(headerfooter)
# Add header-footer
headerfooter.text_color = 0x00FFFF
headerfooter.page_range = PageNumberRange(1, doc.GetPageCount(),
Range.e_Odd)
headerfooter.has_fixedsize_for_print = False
headerfooter.has_text_shrinked = False
headerfooter.start_page_number = 2
headerfooter.page_margin = RectF(36, 70, 36, 70)
headerfooter.text_size = 8
doc.AddHeaderFooter(headerfooter)
# Remove all header-footers
doc.RemoveAllHeaderFooters()
...
Updated on October 13, 2023