How to insert header and footer in PDF document by C #
Editor to share with you how C# inserts headers and footers in PDF documents, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.
1. Create a new page to add headers and footers 1.1 add headers
[C#]
Using Spire.Pdf;using Spire.Pdf.Graphics;using System.Drawing;using System;namespace AddHeader_PDF {class Program {static void Main (string [] args) {/ / create a new PdfDocument class object and add a page PdfDocument pdf = new PdfDocument (); PdfPageBase page = pdf.Pages.Add (); / / set margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor () PdfMargins margin = new PdfMargins (); margin.Top = unitCvtr.ConvertUnits (2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits (4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; / / call the AddHeader () method to add header AddHeader (pdf, PdfPageSize.A4, margin) / / Save and open the document pdf.SaveToFile ("PDF header .pdf"); System.Diagnostics.Process.Start ("PDF header .pdf");} static void AddHeader (PdfDocument doc, SizeF pageSize, PdfMargins margin) {/ / initialize a PdfPageTemplateElement object to create the header PdfPageTemplateElement headerSpace = new PdfPageTemplateElement (pageSize.Width, margin.Top) HeaderSpace.Foreground = true; doc.Template.Top = headerSpace; / / draw the text PdfTrueTypeFont font = new PdfTrueTypeFont (new Font ("Arial Unicode MS", 10f), true) in the header; PdfStringFormat format = new PdfStringFormat (PdfTextAlignment.Right); String headerText = "WORLD TRADE ORGANIZATION, WTO\ n THE INTERNATIONAL ORGANIZATION THAT REGULATES INTERNATIONAL TRADE"; float x = PdfPageSize.A4.Width Float y = 0; headerSpace.Graphics.DrawString (headerText, font, PdfBrushes.Black, x, y, format); / / draw PdfImage headerImage = PdfImage.FromFile (@ "C:\ Users\ Administrator\ Desktop\ 1.png") in the header; float width = headerImage.Width / 2; float height = headerImage.Height / 3 HeaderSpace.Graphics.DrawImage (headerImage, 0,0, width, height);}}
Add effect to the header:
1.2 add footer
[C#]
Using Spire.Pdf;using Spire.Pdf.Graphics;using System.Drawing;using System;using Spire.Pdf.AutomaticFields;namespace AddFooter_PDF {class Program {static void Main (string [] args) {/ / create a new PdfDocument class object and add a page PdfDocument doc = new PdfDocument (); PdfPageBase page = doc.Pages.Add () / / set margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor (); PdfMargins margin = new PdfMargins (); margin.Top = unitCvtr.ConvertUnits (2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits (4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left / call AddFooter () method to add footer AddFooter (doc, PdfPageSize.A4, margin); / / call AddPageNumber () method to add page number AddPageNumber (doc, margin); / / Save and open document doc.SaveToFile ("PDF footer .pdf"); System.Diagnostics.Process.Start ("PDF footer .pdf") } static void AddFooter (PdfDocument doc, SizeF pageSize, PdfMargins margin) {/ / initialize a PdfPageTemplateElement object to create footer PdfPageTemplateElement footerSpace = new PdfPageTemplateElement (pageSize.Width, margin.Bottom); footerSpace.Foreground = true; doc.Template.Bottom = footerSpace / / draw the text PdfTrueTypeFont font = new PdfTrueTypeFont (new Font ("Arial Unicode MS", 10f), true) in the footer; PdfStringFormat format = new PdfStringFormat (PdfTextAlignment.Center); String headerText = "Website: www.wto.org"; float x = PdfPageSize.A4.Width / 2; float y = 0 FooterSpace.Graphics.DrawString (headerText, font, PdfBrushes.Black, x, y, format);} static void AddPageNumber (PdfDocument doc, PdfMargins margin) {/ / add page number to footer foreach (PdfPageBase page in doc.Pages) {PdfStringFormat format1 = new PdfStringFormat (PdfTextAlignment.Left) Int x1 = Convert.ToInt32 (page.Canvas.ClientSize.Width / 2); int Y1 = Convert.ToInt32 (page.Canvas.ClientSize.Height-margin.Bottom + 20); Rectangle bounds = new Rectangle (x1, y1,20,20); PdfPageNumberField field = new PdfPageNumberField (); PdfTrueTypeFont font = new PdfTrueTypeFont ("Arial Unicode MS", 10f), true) Field.Font = font; field.StringFormat = format1; field.Brush = PdfBrushes.Black; field.Bounds = bounds; field.Draw (page.Canvas);}
Footer add effect:
two。 Add a header and footer to an existing PDF document
[C#]
Using Spire.Pdf;using Spire.Pdf.AutomaticFields;using Spire.Pdf.Graphics;using System;using System.Drawing;namespace PdfHeader {class Program {static void Main (string [] args) {/ / load a test document PdfDocument existingPdf = new PdfDocument (); existingPdf.LoadFromFile ("Test.pdf") / call the DrawHeader method to add the header DrawHeader (existingPdf) to the existing document; / / call the DrawFooter method to add the footer DrawFooter (existingPdf) to the existing document; / / Save and open the document existingPdf.SaveToFile ("output.pdf"); System.Diagnostics.Process.Start ("output.pdf") } / / draw the header static void DrawHeader (PdfDocument doc) {/ / get the page size SizeF pageSize = doc.Pages [0] .size; / / declare two float variables float x = 90; float y = 20; for (int I = 0) I < doc.Pages.Count; iTunes +) {/ / draw a picture at the specified location on each page PdfImage headerImage = PdfImage.FromFile ("logo.png"); float width = headerImage.Width / 7; float height = headerImage.Height / 7; doc.pages [I] .Canvas.DrawImage (headerImage, x, y, width, height) / / draw a horizontal line PdfPen pen = new PdfPen (PdfBrushes.Gray, 0.5f) at the specified position on each page; doc.pages [I] .Canvas.DrawLine (pen, x, y + height + 2, pageSize.Width-x, y + height + 2) }} / / draw the footer static void DrawFooter (PdfDocument doc) {/ / get the page size SizeF pageSize = doc.Pages [0] .size; / / declare two float variables float x = 90; float y = pageSize.Height-72 For (int I = 0; I < doc.Pages.Count; iTunes +) {/ / draw a horizontal line at the specified position on each page PdfPen pen = new PdfPen (PdfBrushes.Gray, 0.5f); doc.Pags [I] .Canvas.DrawLine (pen, x, y, pageSize.Width-x, y) / / draw text y = y + 5 at the specified position on each page; PdfTrueTypeFont font = new PdfTrueTypeFont ("boldface", 10f, FontStyle.Bold), true); PdfStringFormat format = new PdfStringFormat (PdfTextAlignment.Left); String footerText = "Website\ n https://g20.org/";" Doc.pages [I] .Canvas.DrawString (footerText, font, PdfBrushes.Black, x, y, format); / / current page number and total page number PdfPageNumberField number = new PdfPageNumberField () at the specified position on each page; PdfPageCountField count = new PdfPageCountField () PdfCompositeField compositeField = new PdfCompositeField (font, PdfBrushes.Black, "{0} / {1}", number, count); compositeField.StringFormat = new PdfStringFormat (PdfTextAlignment.Right, PdfVerticalAlignment.Top); SizeF size = font.MeasureString (compositeField.Text); compositeField.Bounds = new RectangleF (pageSize.Width-x-size.Width, y, size.Width, size.Height) CompositeField.Draw (doc.pages [I] .canvas);}
Test results:
The above is all the content of the article "how to insert headers and footers in PDF documents by C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!