Hello.
I have multiple PDF templates and sometimes after merging the resulting PDF has fields with the same names. I’m trying to set the same font for all fields, but for fields with the same name, the font is applied to only one of the fields.
To emulate the situation, I created a PDF file with 3 fields, two of which have the same name.
image.png (50.8 KB)
Code
using Aspose.Pdf.Annotations;
using Aspose.Pdf.Forms;
using System.IO;
namespace Aspose.Pdf.Examples.CSharp.AsposePDF.Text
{
public class EmbedStandardType1Fonts
{
private const string InputPdfName = "InputFile.pdf";
private const string OutputPdfName = "OutputFile.pdf";
private static Document _pdfDocument;
public static Document PdfDocument => _pdfDocument
?? (_pdfDocument = new Document(DataDir + InputPdfName));
public static void Run4()
{
string fieldName = "FieldOne";
if (PdfDocument.Form[fieldName] is Field field)
{
SetField(field, "JOHN DOE JR");
}
fieldName = "FieldTwo";
if (PdfDocument.Form[fieldName] is Field field2)
{
SetField(field2, "JOHN DOE JR 2");
}
//PdfDocument.Save(DataDir + OutputPdfName);
var pdfStream = new MemoryStream();
PdfDocument.Save(pdfStream);
File.WriteAllBytes(DataDir + OutputPdfName, pdfStream.ToArray());
}
protected static Field SetField(Field field, string value)
{
//FacadeForm.FillField(name, value); // simple way to set value, but we need apply font too
var oldFontColor = field.DefaultAppearance.TextColor;
field.DefaultAppearance = new DefaultAppearance("Courier", 20, oldFontColor);
field.Value = value;
return field;
}
#region support methods
public static string GetDataDir_Data()
{
var parent = Directory.GetParent(Directory.GetCurrentDirectory()).Parent;
string startDirectory = null;
if (parent != null)
{
var directoryInfo = parent.Parent;
if (directoryInfo != null)
{
startDirectory = directoryInfo.FullName;
}
}
else
{
startDirectory = parent.FullName;
}
return Path.Combine(startDirectory, "Data\\");
}
public static string GetDataDir_AsposePdf_Text()
{
return Path.GetFullPath(GetDataDir_Data() + "AsposePDF/Text/");
}
private static readonly string DataDir = GetDataDir_AsposePdf_Text();
#endregion
}
}
And as a result, I have the font set in two of the three fields.
image.png (110.0 KB)
- Is it possible to apply a font to all fields with the same name?
- Is it possible to apply the font to all fields on one page or in the entire document?
InputFile.pdf (6.2 KB)