using ComPDFKit.PDFDocument; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; using static System.Net.Mime.MediaTypeNames; namespace HeaderFooterTest { internal class HeaderFooterTest { private static string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\HeaderFooter"; static void Main(string[] args) { SDKLicenseHelper.LicenseVerify(); CPDFDocument document = CPDFDocument.InitWithFilePath("Blank Page.pdf"); if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } #region Add common header and footer if (AddCommonHeaderFooter(document)) { Console.WriteLine("Add common header and footer succeeded.\n"); } else { Console.WriteLine("Add common header and footer failed.\n"); } Console.WriteLine(); #endregion #region Add page header and footer if (AddPageHeaderFooter(document)) { Console.WriteLine("Add page header and footer succeeded.\n"); } else { Console.WriteLine("Add page header and footer failed.\n"); } Console.WriteLine(); #endregion #region Edit header and footer if (EditHeaderFooter(document)) { Console.WriteLine("Edit header and footer succeeded.\n"); } else { Console.WriteLine("Edit header and footer failed.\n"); } Console.WriteLine(); #endregion #region Delete header and footer if (DeleteHeaderFooter(document)) { Console.WriteLine("Delete header and footer succeeded.\n"); } else { Console.WriteLine("delete header and footer failed\n"); } Console.WriteLine(); #endregion Console.ReadLine(); } /// /// Follow these steps to add a header and footer in a blank pages file. /// /// Documents that require manipulation private static bool AddCommonHeaderFooter(CPDFDocument document) { // Init HeaderFooter CPDFHeaderFooter headerFooter = document.GetHeaderFooter(); // Set Text: 1 headerFooter.SetText(0, "ComPDFKit"); // Set page index: all the page headerFooter.SetPages("0-" + (document.PageCount - 1)); // Set color : red byte[] color = { 255, 0, 0 }; headerFooter.SetTextColor(0, color); // Set font size : 14 headerFooter.SetFontSize(0, 14); // Update HeaderFooter headerFooter.Update(); // Save to pointed path so you can observe the effect. string addHeaderFooterPath = outputPath + "\\Blank Page_AddCommonHeaderFooter.pdf"; if (document.WriteToFilePath(addHeaderFooterPath)) { Console.WriteLine("Browse the changed file in " + addHeaderFooterPath); return true; } else { return false; } } private static bool AddPageHeaderFooter(CPDFDocument document) { // Init HeaderFooter CPDFHeaderFooter headerFooter = document.GetHeaderFooter(); // Set Text: 1 headerFooter.SetText(0, "<<1,2>>"); // Set page index: all the page headerFooter.SetPages("0-" + (document.PageCount - 1)); // Set color : red byte[] color = { 255, 0, 0 }; headerFooter.SetTextColor(0, color); // Set font size : 14 headerFooter.SetFontSize(0, 14); // Update HeaderFooter headerFooter.Update(); // Save to pointed path so you can observe the effect. string addHeaderFooterPath = outputPath + "\\Blank Page_AddPageHeaderFooter.pdf"; if (document.WriteToFilePath(addHeaderFooterPath)) { Console.WriteLine("Browse the changed file in " + addHeaderFooterPath); return true; } else { return false; } } /// /// Follow these steps to delete a header and footer in a blank pages file. /// /// Documents that require manipulation private static bool EditHeaderFooter(CPDFDocument document) { CPDFHeaderFooter headerFooter = document.GetHeaderFooter(); if (headerFooter.GetText(0) != string.Empty) { Console.WriteLine("Get head and footer 0 succeeded, text is {0}", headerFooter.GetText(0)); } else { Console.WriteLine("Get head and footer 0 failed"); return false; } headerFooter.SetText(0, "PDF SDK"); headerFooter.Update(); string editHeaderFooterPath = outputPath + "\\Blank Page_EditHeaderFooter.pdf"; if (document.WriteToFilePath(editHeaderFooterPath)) { Console.WriteLine("Browse the changed file in " + editHeaderFooterPath); return true; } else { return false; } } private static bool DeleteHeaderFooter(CPDFDocument document) { CPDFHeaderFooter headerFooter = document.GetHeaderFooter(); headerFooter.Clear(); //headerFooter.Update(); var str = headerFooter.GetText(0); if (headerFooter.GetText(0) != string.Empty) { Console.WriteLine("Delete failed"); } string deleteHeaderFooterPath = outputPath + "\\Blank Page_DeleteHeaderFooter.pdf"; if (document.WriteToFilePath(deleteHeaderFooterPath)) { Console.WriteLine("Browse the changed file in " + deleteHeaderFooterPath); return true; } else { return false; } } } }