Knowledgebase (2311)
Children categories
Set the font and color for the text on PDF Combo Box field area
2015-11-25 07:33:44 Written by KoohjiWith 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);
}
}
}
Spire.PDFViewer for ASP.NET contains two controls: PDFViewer and PDFDocumentViewer. Generally, PDFDocumentViewer is used for loading and viewing PDF files on website. But actually, it can also achieve other features such as zoom, fit and page after a simple design.
We've introduced the usage of PDFViewer in the previous article, so this article will illustrate how to zoom PDF File via PDFDocumentViewer in ASP.NET.
Before start, download Spire.PDFViewer for ASP.NET and install it on your system.
Step 1: Create a new ASP.NET Empty Web Application in Visual Studio. Add a new web Form to the project.
Step 2: Add the .dll files from the bin folder as the references of this project.

Step 3: Add the PDFDocumentViewer control into toolbox and drag it into Deafault.aspx.

(Detail manipulations of step 1, 2, 3 refer to this article: How to use Spire.PDFViewer for ASP.NET)
Step 4: Zoom PDF file via Spire. PDFDocumentViewer. Zoom feature is divided into three types in this article:
- Zoom: choose the zoom percentage manually.
- Zoom in: Increase the display page page zoom factor by ten percent.
- Zoom out: decrease the display page page zoom factor by ten percent.
Main codes
Section 1: Call the LoadFromFile() method of PdfDocumentViewer to load a sample PDF file in Default.aspx.cs. Note that you have to add the following if statement and !IsPostBack property before loading the pdf file.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//load the sample PDF file
this.PdfDocumentViewer1.CacheInterval = 1000;
this.PdfDocumentViewer1.CacheTime = 1200;
this.PdfDocumentViewer1.CacheNumberImage = 1000;
this.PdfDocumentViewer1.ScrollInterval = 300;
this.PdfDocumentViewer1.ZoomFactor = 1f;
this.PdfDocumentViewer1.CustomErrorMessages = "";
this.PdfDocumentViewer1.LoadFromFile("files/PDFViewer.pdf");
}
}
Section 2: Design, Drag a DropDownList and two buttons from toolbox into Deafault.aspx, set the properties like "ID", "text" etc. as below.

Generated source code is shown here:
<select id="PdfDocumentViewer1_SelectCurrentZoomLevel" name="PdfDocumentViewer1_SelectCurrentZoomLevel" onchange="pdfdocumentviewer1.SelectDropdownBox(this.value)">
<option value="0.5">50%</option>
<option value="0.75">75%</option>
<option value="1" selected="selected">100%</option>
<option value="1.5">150%</option>
<option value="2">200%</option>
<option value="4">400%</option>
</select>
<input type="button" id="btnZoomIn" value="Zoom In" onclick="pdfdocumentviewer1.ZoomPage()" />
<input type="button" id="btnZoomOut" value="Zoom Out" onclick="pdfdocumentviewer1.NarrowPage()" />
Effect screenshot after designing:

Transferring content between Microsoft Word documents is a frequent need for many users. Whether you're consolidating information from multiple sources or reorganizing the structure of a document, being able to effectively copy and paste text, graphics, and formatting is crucial.
This article demonstrates how to copy content from one Word document to another using C# and Spire.Doc for .NET.
- Copy Specified Paragraphs from One Word Document to Another
- Copy a Section from One Word Document to Another
- Copy the Entire Document and Append it to Another
- Create a Copy of a Word Document
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Copy Specified Paragraphs from One Word Document to Another in C#
With Spire.Doc library for .NET, you can clone individual paragraphs using the Paragraph.Clone() method, and then add the cloned paragraphs to a different Word document using the ParagraphCollection.Add() method. This allows you to selectively copy and transfer content between documents.
To copy specified paragraphs from one Word document to another, follow these steps:
- Create a Document object to load the source file.
- Create another Document object to load the target file.
- Get the specified paragraphs from the source file.
- Clone these paragraphs using Paragraph.Clone() method.
- Add the cloned paragraphs to the target file using ParagraphCollection.Add() method.
- Save the target file to a different Word file.
- C#
using Spire.Doc;
using Spire.Doc.Documents;
namespace CopyParagraphs
{
class Program
{
static void Main(string[] args)
{
// Create a Document object
Document sourceDoc = new Document();
// Load the source file
sourceDoc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\source.docx");
// Get the specified paragraphs from the source file
Paragraph p1 = sourceDoc.Sections[0].Paragraphs[8];
Paragraph p2 = sourceDoc.Sections[0].Paragraphs[9];
// Create another Document object
Document targetDoc = new Document();
// Load the target file
targetDoc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\target.docx");
// Get the last section
Section lastSection = targetDoc.LastSection;
// Add the paragraphs from the soure file to the target file
lastSection.Paragraphs.Add((Paragraph)p1.Clone());
lastSection.Paragraphs.Add((Paragraph)p2.Clone());
// Save the target file to a different Word file
targetDoc.SaveToFile("CopyParagraphs.docx", FileFormat.Docx2019);
// Dispose resources
sourceDoc.Dispose();
targetDoc.Dispose();
}
}
}

Copy a Section from One Word Document to Another in C#
A section in a Word document can contain not only paragraphs, but also other elements such as tables. To copy an entire section from one Word document to another, you need to iterate through all the child objects within the section and add them individually to a specified section in the target document.
The steps to copy a section between different Word documents are:
- Create Document objects to load the source file and the target file, respectively.
- Get the specified section from the source file.
- Iterate through the child objects in the section, and clone these objects using DocumentObject.Clone() method.
- Add the cloned child objects to a specified section of the target file using DocumentObjectCollection.Add() method.
- Save the updated target file to a new file.
- C#
using Spire.Doc;
namespace CopySection
{
class Program
{
static void Main(string[] args)
{
// Create a Document object
Document sourceDoc = new Document();
// Load the source file
sourceDoc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\source.docx");
// Get the specified section from the source file
Section section = sourceDoc.Sections[0];
// Create another Document object
Document targetDoc = new Document();
// Load the target file
targetDoc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\target.docx");
// Get the last section of the target file
Section lastSection = targetDoc.LastSection;
// Iterate through the child objects in the selected section
foreach (DocumentObject obj in section.Body.ChildObjects)
{
// Add the child object to the last section of the target file
lastSection.Body.ChildObjects.Add(obj.Clone());
}
// Save the target file to a different Word file
targetDoc.SaveToFile("CopySection.docx", FileFormat.Docx2019);
// Dispose resources
sourceDoc.Dispose();
targetDoc.Dispose();
}
}
}
Copy the Entire Document and Append it to Another in C#
To copy the full contents from one Word document into another, you can use the Document.InsertTextFromFile() method. This method appends the source document's contents to the target document, starting on a new page.
The steps to copy the entire document and append it to another are as follows:
- Create a Document object.
- Load a Word file (target file) from the given file path.
- Insert content of a different Word document to the target file using Document.InsertTextFromFile() method.
- Save the updated target file to a new Word document.
- C#
using Spire.Doc;
namespace CopyEntireDocument
{
class Program
{
static void Main(string[] args)
{
// Specify the path of the source document
String sourceFile = "C:\\Users\\Administrator\\Desktop\\source.docx";
// Create a Document object
Document targetDoc = new Document();
// Load the target file
targetDoc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\target.docx");
// Insert content of the source file to the target file
targetDoc.InsertTextFromFile(sourceFile, FileFormat.Docx);
// Save the target file to a different Word file
targetDoc.SaveToFile("CopyEntireDocuemnt.docx", FileFormat.Docx2019);
// Dispose resources
targetDoc.Dispose();
}
}
}
Create a Copy of a Word Document in C#
Using Spire.Doc library for .NET, you can leverage the Document.Clone() method to easily duplicate a Word document.
Here are the steps to create a copy of a Word document:
- Create a Document object.
- Load a Word file from the given file path.
- Clone the file using Document.Clone() method.
- Save the cloned document to a new Word file.
- C#
using Spire.Doc;
namespace CloneWordDocument
{
class Program
{
static void Main(string[] args)
{
// Create a new document object
Document sourceDoc = new Document();
// Load a Word file
sourceDoc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");
// Clone the document
Document newDoc = sourceDoc.Clone();
// Save the cloned document as a docx file
newDoc.SaveToFile("Copy.docx", FileFormat.Docx);
// Dispose resources
sourceDoc.Dispose();
newDoc.Dispose();
}
}
}
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.