With the help of Spire.PDF, developers can easily add new text to the PDF; create form fields to both the new and existing PDF file. Spire.PDF also owns the ability to set the text formatting for the PDF fields' area. This article will show you how to set the font and color for PDF Combo Box field by the method of PdfComboBoxField.
Note: Before Start, please download the latest version of Spire.PDF and add Spire.PDF.dll in the bin folder as the reference of Visual Studio.
Here comes to the details:
Step 1: Create a new PDF document.
PdfDocument doc = new PdfDocument();
Step 2: Add a new page to the PDF document and set the page size for the page.
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins());
Step 3: Draw the text to the PDF page and set the location, font and color for the text.
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold)); RectangleF labelBounds = new RectangleF(20, 20, 40, font.Height); page.Canvas.DrawString("My label", font, PdfBrushes.Black, labelBounds);
Step 4: Create a Combo Box and add value for it. Use comboBox.Font and comboBox.ForeColor to set the font and color for the text on Combo Box area.
PdfComboBoxField comboBox = new PdfComboBoxField(page, "cmb"); comboBox.Bounds = new RectangleF(80, 20, 80, font.Height); comboBox.Font = font; comboBox.ForeColor = Color.Blue; comboBox.Items.Add(new PdfListFieldItem("value 1", "text 1")); comboBox.Items.Add(new PdfListFieldItem("value 2", "text 2")); comboBox.Items.Add(new PdfListFieldItem("value 3", "text 3"));
Step 5: Add the Combo Box to the PDF file.
doc.Form.Fields.Add(comboBox);
Step 6: Save the document to file and launch to preview it.
string file = string.Format("result.pdf", Guid.NewGuid().ToString()); doc.SaveToFile(file); System.Diagnostics.Process.Start(file);
Effective screenshot after setting the font and color for text on the Combo Box area:
Full codes:
using Spire.Pdf; using Spire.Pdf.Fields; using Spire.Pdf.Graphics; using System; using System.Drawing; namespace SetFontColorInComboboxField { class Program { static void Main(string []args) { PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins()); PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold)); RectangleF labelBounds = new RectangleF(20, 20, 40, font.Height); page.Canvas.DrawString("My label", font, PdfBrushes.Black, labelBounds); PdfComboBoxField comboBox = new PdfComboBoxField(page, "cmb"); comboBox.Bounds = new RectangleF(80, 20, 80, font.Height); comboBox.Font = font; comboBox.ForeColor = Color.Blue; comboBox.Items.Add(new PdfListFieldItem("value 1", "text 1")); comboBox.Items.Add(new PdfListFieldItem("value 2", "text 2")); comboBox.Items.Add(new PdfListFieldItem("value 3", "text 3")); doc.Form.Fields.Add(comboBox); string file = string.Format("result.pdf", Guid.NewGuid().ToString()); doc.SaveToFile(file); System.Diagnostics.Process.Start(file); } } }