I encountered an error when trying to set the font for field in pdf.
“Format of font “Helvetica” is not supported for new composite fonts”
image.png (7.1 KB)
at #=zULFkdn83E6r1kO4I6X4cRDogt26yXOQcTHoVA2ErT1fi.#=zyx4Pz8KeeGHK0mB68g==(#=zwRQqr$6u17h2u5scyZ_Q0OI= #=zsXlk$AQ=)
at #=zt5lKQ7f2cVEYSQdQQWaZBwfg8lRDbWWZYMxXj1M=.#=zmsH5jLEVe4S_(#=zz0pJOcsR0nEFiMZueyMyTaYTDY$uRuYC0k3Jr$pMn2QV #=zV8jZbq8=, #=zwRQqr$6u17h2u5scyZ_Q0OI= #=zsXlk$AQ=)
at #=zEmfZEt2uVKC4TAlrzqTTM2suwn0ttcQDZgg7bro6Fhuk.#=zQ6mXm7Qx3Er9bxdASw==(#=zZk39x5RElUaoOVKSE_kII1QICLxkfLqk2b5IPOU= #=zXrXTUQE=, #=zwRQqr$6u17h2u5scyZ_Q0OI= #=zsXlk$AQ=, Boolean #=zvjEU4jk=, Boolean #=zBSy7VY4=, String& #=zzWWSj4A=)
at Aspose.Pdf.Text.FontCollection.Add(Font newFont, String& resName)
at Aspose.Pdf.Annotations.WidgetAnnotation.#=zby8hXnzLkp$p(Font #=zUPrTVKQ=)
at Aspose.Pdf.Annotations.WidgetAnnotation.set_DefaultAppearance(DefaultAppearance value)
at HelveticaFontIssue.AsposeHelper.SetValuesWithFont() in d:\Work\Applicint\ASPOSE\HelveticaFontIssue\Program.cs:line 70
The font is installed in Windows and the FindFont operator gets it.
image.png (26.5 KB)
If I open via the OpenFont operator, everything works. The problem is that I can’t put the font together with the application.
Any ideas what could be the reason?
PS: I attach a simple console application with an example pdf file.
HelveticaFontIssue.zip (1.6 MB)
using Aspose.Pdf;
using Aspose.Pdf.Annotations;
using Aspose.Pdf.Forms;
using Aspose.Pdf.Text;
namespace HelveticaFontIssue
{
internal class Program
{
static void Main()
{
var helper = new AsposeHelper();
helper.SetValuesWithFont();
Console.WriteLine("Press any key...");
Console.ReadKey();
}
}
public class AsposeHelper
{
private static readonly string FontPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Font", "Helvetica.ttf");
private Font? _defaultAsposeFont;
protected Font DefaultAsposeFont
{
get
{
if (_defaultAsposeFont != null)
return _defaultAsposeFont;
_defaultAsposeFont = FontRepository.FindFont("Helvetica");
//_defaultAsposeFont = FontRepository.OpenFont(FontPath); // This works
return _defaultAsposeFont;
}
}
public void SetValuesWithFont()
{
try
{
FontRepository.Sources.Add(new FolderFontSource(FontPath));
string externalPdfPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Pdf");
string inputPath = Path.Combine(externalPdfPath, "Input.pdf");
string outputPath = Path.Combine(externalPdfPath, "Output.pdf");
if (!File.Exists(inputPath))
{
Console.WriteLine($"Input file not found: {inputPath}");
return;
}
if (!File.Exists(FontPath))
{
Console.WriteLine($"Font file not found: {FontPath}");
//return;
}
using var pdfDocument = new Document(inputPath);
try
{
foreach (var field in pdfDocument.Form.Fields)
{
if (field is TextBoxField)
{
field.Value = "Value";
var newColor = System.Drawing.Color.Orange;
var appearance = new DefaultAppearance(DefaultAsposeFont, 10, newColor);
field.DefaultAppearance = appearance;
field.Color = Color.FromRgb(newColor);
}
}
}
catch (Exception fontEx)
{
Console.WriteLine($"Error with font embedding \n {inputPath} \n: {fontEx.Message}");
return;
}
pdfDocument.Save(outputPath);
Console.WriteLine($"PDF saved successfully: {outputPath}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
}