I am attempting to create a pdf document which uses the standard “Type 1” fonts. When I try to add a Type 1 font to my page resources (“Courier” for example) I receive an exception stating that:
Format of font “Courier” is not supported for new composite fonts
I am using Aspose.PDF version 23.8.0. Here is the code that I am using to create the document:
private void CreatePDF_Low()
{
Document doc = new();
Aspose.Pdf.Page page = doc.Pages.Add();
Aspose.Pdf.Text.Font font = FontRepository.FindFont("Courier");
page.Resources.GetFonts(true).Add(font, out string fontResName);
page.Contents.Add(new GSave());
page.Contents.Add(new BT());
page.Contents.Add(new SelectFont(fontResName, 16));
page.Contents.Add(new MoveTextPosition(0, 720));
page.Contents.Add(new ShowText("the quick brown fox jumps over the lazy dog", font));
page.Contents.Add(new ET());
page.Contents.Add(new GRestore());
doc.Save(@"your_favorite_filepath");
}
I have found that using the higher-level TextFragment object allows me to successfully use Type 1 fonts. For instance:
private void CreatePDF_High()
{
Document doc = new Document();
// Add page to pages collection of Document object
Aspose.Pdf.Page page = doc.Pages.Add();
TextBuilder builder = new TextBuilder(page);
// Create text paragraph
TextParagraph paragraph = new TextParagraph();
// Set subsequent lines indent
paragraph.SubsequentLinesIndent = 20;
// Specify the location to add TextParagraph
paragraph.Rectangle = new Aspose.Pdf.Rectangle(100, 300, 200, 700);
// Specify word wraping mode
paragraph.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.ByWords;
// Create text fragment
TextFragment fragment = new TextFragment("the quick brown fox jumps over the lazy dog");
fragment.TextState.Font = FontRepository.FindFont("Courier");
fragment.TextState.FontSize = 12;
// Add fragment to paragraph
paragraph.AppendLine(fragment);
// Add paragraph
builder.AppendParagraph(paragraph);
doc.Save(@"your_favorite_filepath");
}
works just fine.
I am not sure what I am doing wrong that causes the exception to be thrown in the first example. Any help would be very much appreciated.