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"; private static Dictionary IntToLocateDic = new Dictionary() { {0, "Top Left" }, {1, "Top Middle" }, {2, "Top Right" }, {3, "Bottom Left" }, {4, "Bottom Middle" }, {5, "Bottom Right" } }; 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 done."); } else { Console.WriteLine("Add common header and footer failed."); } #endregion Console.WriteLine("--------------------"); #region Add page header and footer if (AddPageHeaderFooter(document)) { Console.WriteLine("Add page header and footer done."); } else { Console.WriteLine("Add page header and footer failed."); } #endregion Console.WriteLine("--------------------"); #region Edit header and footer if (EditHeaderFooter(document)) { Console.WriteLine("Edit header and footer done."); } else { Console.WriteLine("Edit header and footer failed."); } #endregion Console.WriteLine("--------------------"); #region Delete header and footer if (DeleteHeaderFooter(document)) { Console.WriteLine("Delete header and footer done.\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(); for (int i = 0; i <= 2; i++) { // Set Text: ComPDFKit headerFooter.SetText(i, "ComPDFKit"); // Set page index: all the page headerFooter.SetPages("0-" + (document.PageCount - 1)); // Set color : red byte[] color = { 255, 0, 0 }; headerFooter.SetTextColor(i, color); // Set font size : 14 headerFooter.SetFontSize(i, 14); Console.WriteLine("Text: {0}", headerFooter.GetText(i)); Console.WriteLine("Location: {0}\n", IntToLocateDic[i]); } // Update HeaderFooter headerFooter.Update(); // Save to pointed path so you can observe the effect. string addHeaderFooterPath = outputPath + "\\AddCommonHeaderFooterTest.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(); for (int i = 3; i <= 5; i++) { // Set Text: 1 headerFooter.SetText(i, "<<1,2>>"); // Set page index: all the page headerFooter.SetPages("0-" + (document.PageCount - 1)); // Set color : red byte[] color = { 255, 0, 0 }; headerFooter.SetTextColor(i, color); // Set font size : 14 headerFooter.SetFontSize(i, 14); Console.WriteLine("Text: {0}", headerFooter.GetText(i)); Console.WriteLine("Location: {0}\n", IntToLocateDic[i]); } // Update HeaderFooter headerFooter.Update(); // Save to pointed path so you can observe the effect. string addHeaderFooterPath = outputPath + "\\AddPageHeaderFooterTest.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 old 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, "ComPDFKit Samples"); headerFooter.Update(); Console.WriteLine("Change head and footer 0 succeeded, new text is {0}", headerFooter.GetText(0)); string editHeaderFooterPath = outputPath + "\\EditHeaderFooterTest.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(); string deleteHeaderFooterPath = outputPath + "\\DeleteHeaderFooterTest.pdf"; if (document.WriteToFilePath(deleteHeaderFooterPath)) { Console.WriteLine("Browse the changed file in " + deleteHeaderFooterPath); return true; } else { return false; } } } }