The sample demonstrates how to work with automatic fields in PDF document.
using System; using System.Drawing; using Spire.Pdf; using Spire.Pdf.AutomaticFields; using Spire.Pdf.Graphics; namespace AutomaticField { class Program { static void Main(string[] args) { //Create a pdf document. PdfDocument doc = new PdfDocument(); doc.DocumentInformation.Author = "Spire.Pdf"; //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(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; for (int i = 1; i < 4; i++) { //create section PdfSection section = doc.Sections.Add(); section.PageSettings.Size = PdfPageSize.A4; section.PageSettings.Margins = margin; for (int j = 0; j < i; j++) { // Create one page PdfPageBase page = section.Pages.Add(); DrawAutomaticField(page); } } //Save pdf file. doc.SaveToFile("AutomaticField.pdf"); doc.Close(); //Launching the Pdf file. System.Diagnostics.Process.Start("AutomaticField.pdf"); } private static void DrawAutomaticField(PdfPageBase page) { float y = 0; //title PdfBrush brush1 = PdfBrushes.CadetBlue; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Automatic Field List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Automatic Field List", format1).Height; y = y + 5; String[] fieldList = new String[] { "DateTimeField", "CreationDateField", "DocumentAuthorField", "SectionNumberField", "SectionPageNumberField", "SectionPageCountField", "PageNumberField", "PageCountField", "DestinationPageNumberField", "CompositeField" }; PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f)); PdfStringFormat fieldNameFormat = new PdfStringFormat(); fieldNameFormat.MeasureTrailingSpaces = true; foreach (String fieldName in fieldList) { //draw field name String text = String.Format("{0}: ", fieldName); page.Canvas.DrawString(text, font, PdfBrushes.DodgerBlue, 0, y); float x = font.MeasureString(text, fieldNameFormat).Width; RectangleF bounds = new RectangleF(x, y, 200, font.Height); DrawAutomaticField(fieldName, page, bounds); y = y + font.Height + 3; } } private static void DrawAutomaticField(string fieldName, PdfPageBase page, RectangleF bounds) { PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic)); PdfBrush brush = PdfBrushes.OrangeRed; PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); if ("DateTimeField" == fieldName) { PdfDateTimeField field = new PdfDateTimeField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.DateFormatString = "yyyy-MM-dd HH:mm:ss"; field.Draw(page.Canvas); } if ("CreationDateField" == fieldName) { PdfCreationDateField field = new PdfCreationDateField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.DateFormatString = "yyyy-MM-dd HH:mm:ss"; field.Draw(page.Canvas); } if ("DocumentAuthorField" == fieldName) { PdfDocumentAuthorField field = new PdfDocumentAuthorField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.Draw(page.Canvas); } if ("SectionNumberField" == fieldName) { PdfSectionNumberField field = new PdfSectionNumberField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.Draw(page.Canvas); } if ("SectionPageNumberField" == fieldName) { PdfSectionPageNumberField field = new PdfSectionPageNumberField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.Draw(page.Canvas); } if ("SectionPageCountField" == fieldName) { PdfSectionPageCountField field = new PdfSectionPageCountField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.Draw(page.Canvas); } if ("PageNumberField" == fieldName) { PdfPageNumberField field = new PdfPageNumberField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.Draw(page.Canvas); } if ("PageCountField" == fieldName) { PdfPageCountField field = new PdfPageCountField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.Draw(page.Canvas); } if ("DestinationPageNumberField" == fieldName) { PdfDestinationPageNumberField field = new PdfDestinationPageNumberField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.Page = page as PdfNewPage; field.Draw(page.Canvas); } if ("CompositeField" == fieldName) { PdfSectionPageNumberField field1 = new PdfSectionPageNumberField(); field1.NumberStyle = PdfNumberStyle.LowerRoman; PdfSectionPageCountField field2 = new PdfSectionPageCountField(); PdfCompositeField fields = new PdfCompositeField(); fields.Font = font; fields.Brush = brush; fields.StringFormat = format; fields.Bounds = bounds; fields.AutomaticFields = new PdfAutomaticField[] { field1, field2 }; fields.Text = "section page {0} of {1}"; fields.Draw(page.Canvas); } } } }
Imports System.Drawing Imports Spire.Pdf Imports Spire.Pdf.AutomaticFields Imports Spire.Pdf.Graphics Namespace AutomaticField Friend Class Program Shared Sub Main(ByVal args() As String) 'Create a pdf document. Dim doc As New PdfDocument() doc.DocumentInformation.Author = "Spire.Pdf" 'margin Dim unitCvtr As New PdfUnitConvertor() Dim margin As New PdfMargins() margin.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point) margin.Bottom = margin.Top margin.Left = unitCvtr.ConvertUnits(3.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point) margin.Right = margin.Left For i As Integer = 1 To 3 'create section Dim section As PdfSection = doc.Sections.Add() section.PageSettings.Size = PdfPageSize.A4 section.PageSettings.Margins = margin For j As Integer = 0 To i - 1 ' Create one page Dim page As PdfPageBase = section.Pages.Add() DrawAutomaticField(page) Next j Next i 'Save pdf file. doc.SaveToFile("AutomaticField.pdf") doc.Close() 'Launching the Pdf file. Process.Start("AutomaticField.pdf") End Sub Private Shared Sub DrawAutomaticField(ByVal page As PdfPageBase) Dim y As Single = 0 'title Dim brush1 As PdfBrush = PdfBrushes.CadetBlue Dim font1 As New PdfTrueTypeFont(New Font("Arial", 16.0F, FontStyle.Bold)) Dim format1 As New PdfStringFormat(PdfTextAlignment.Center) page.Canvas.DrawString("Automatic Field List", font1, brush1, _ page.Canvas.ClientSize.Width \ 2, y, format1) y = y + font1.MeasureString("Automatic Field List", format1).Height y = y + 5 Dim fieldList() As String _ = {"DateTimeField", "CreationDateField", "DocumentAuthorField", _ "SectionNumberField", "SectionPageNumberField", "SectionPageCountField", _ "PageNumberField", "PageCountField", "DestinationPageNumberField", "CompositeField"} Dim font As New PdfTrueTypeFont(New Font("Arial", 9.0F)) Dim fieldNameFormat As New PdfStringFormat() fieldNameFormat.MeasureTrailingSpaces = True For Each fieldName As String In fieldList 'draw field name Dim text As String = String.Format("{0}: ", fieldName) page.Canvas.DrawString(text, font, PdfBrushes.DodgerBlue, 0, y) Dim x As Single = font.MeasureString(text, fieldNameFormat).Width Dim bounds As New RectangleF(x, y, 200, font.Height) DrawAutomaticField(fieldName, page, bounds) y = y + font.Height + 3 Next fieldName End Sub Private Shared Sub DrawAutomaticField(ByVal fieldName As String, ByVal page As PdfPageBase, _ ByVal bounds As RectangleF) Dim font As New PdfTrueTypeFont(New Font("Arial", 9.0F, FontStyle.Italic)) Dim brush As PdfBrush = PdfBrushes.OrangeRed Dim format As New PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle) If "DateTimeField" = fieldName Then Dim field As New PdfDateTimeField() field.Font = font field.Brush = brush field.StringFormat = format field.Bounds = bounds field.DateFormatString = "yyyy-MM-dd HH:mm:ss" field.Draw(page.Canvas) End If If "CreationDateField" = fieldName Then Dim field As New PdfCreationDateField() field.Font = font field.Brush = brush field.StringFormat = format field.Bounds = bounds field.DateFormatString = "yyyy-MM-dd HH:mm:ss" field.Draw(page.Canvas) End If If "DocumentAuthorField" = fieldName Then Dim field As New PdfDocumentAuthorField() field.Font = font field.Brush = brush field.StringFormat = format field.Bounds = bounds field.Draw(page.Canvas) End If If "SectionNumberField" = fieldName Then Dim field As New PdfSectionNumberField() field.Font = font field.Brush = brush field.StringFormat = format field.Bounds = bounds field.Draw(page.Canvas) End If If "SectionPageNumberField" = fieldName Then Dim field As New PdfSectionPageNumberField() field.Font = font field.Brush = brush field.StringFormat = format field.Bounds = bounds field.Draw(page.Canvas) End If If "SectionPageCountField" = fieldName Then Dim field As New PdfSectionPageCountField() field.Font = font field.Brush = brush field.StringFormat = format field.Bounds = bounds field.Draw(page.Canvas) End If If "PageNumberField" = fieldName Then Dim field As New PdfPageNumberField() field.Font = font field.Brush = brush field.StringFormat = format field.Bounds = bounds field.Draw(page.Canvas) End If If "PageCountField" = fieldName Then Dim field As New PdfPageCountField() field.Font = font field.Brush = brush field.StringFormat = format field.Bounds = bounds field.Draw(page.Canvas) End If If "DestinationPageNumberField" = fieldName Then Dim field As New PdfDestinationPageNumberField() field.Font = font field.Brush = brush field.StringFormat = format field.Bounds = bounds field.Page = TryCast(page, PdfNewPage) field.Draw(page.Canvas) End If If "CompositeField" = fieldName Then Dim field1 As New PdfSectionPageNumberField() field1.NumberStyle = PdfNumberStyle.LowerRoman Dim field2 As New PdfSectionPageCountField() Dim fields As New PdfCompositeField() fields.Font = font fields.Brush = brush fields.StringFormat = format fields.Bounds = bounds fields.AutomaticFields = New PdfAutomaticField() {field1, field2} fields.Text = "section page {0} of {1}" fields.Draw(page.Canvas) End If End Sub End Class End Namespace