I encountered a problem with Version 17.9 when embedding a font in a new pdf document. When opening the document I get the error that the font ist missing. The font is listet in the properties of the document, but the information seems to be wrong (translated from German UI):
Fontname
Type: Type 1 (CID)
Code: Identity-H
Original: Unknown
However, if I use the Version 11.7 which is provided in your samples, everything works as expected. There the information in the document properties states:
Fontname (Embedded OpenType)
Type: Type 1 (CID)
Code: Identity-H
I use the following code snippet to embedd the font:
Aspose.Pdf.Text.TextFragment mainTitleFragment = new Aspose.Pdf.Text.TextFragment("");
Aspose.Pdf.Text.TextSegment mainTitleSegment = new Aspose.Pdf.Text.TextSegment("Embedded font");
Aspose.Pdf.Text.TextState mainTitleState = new Aspose.Pdf.Text.TextState();
mainTitleState.Font = Aspose.Pdf.Text.FontRepository.OpenFont("P022X14T.otf");
mainTitleState.Font.IsEmbedded = true;
mainTitleState.FontSize = 32;
mainTitleSegment.TextState = mainTitleState;
mainTitleFragment.Segments.Add(mainTitleSegment);
basePage.Paragraphs.Add(mainTitleFragment);
The library 11.7 provided with the samples is around 13MB larger than the current one.
I have worked with the data shared by you. I have attached the generated file for your kind reference EmbedFontWhileDocCreation_out.pdf. This file contains DTLProkyonTOT-Medium as embedded but the problem is that Adobe Reader can’t read this font although this font is embedded in accordance with font embedding specification.
Foxit Reader 7.1 reads this PDF document well, without any trouble. Please see the attached screenshots for your kind reference. Font.JPGFoxitView.JPG. These screenshots illustrate how Foxit Reader displays new PDF document and the fact that font DTLProkyonTOT-Medium is really embedded into the document.
To achieve font embedding to be readable for Adobe Reader, font file DTLProkyonTOT-Medium.otf is not enough. Used font DTLProkyonTOT-Medium has special font type - Type1. This is very old font type. And this font type requires font metric file .pfm with the same name as .otf file. Furthermore, Aspose.Pdf does not support the functionality to mix these 2 files - .pfm and .otf into binary packet, at the moment, which corresponds to Adobe’s requirements for font embedding. A feature request for the implementation of aforementioned funcionality has been logged in our issue management system with ID PDFNET-43767. The issue ID has been linked with this thread so that you will receive notification as soon as the issue is resolved.
The font is embedded fine with Aspose.Pdf for .NET 11.7.0 but causes an issue with Aspose.Pdf for .NET 17.11. A regression ticket with ID PDFNET-43772 has been logged in our issue management system for further investigation and resolution of this issue. The issue ID has been linked with this thread so that you will receive notification as soon as the issue is resolved.
We have investigated and resolved the issues reported by you. Below are our findings, for your kind reference.
You can embed fonts in a PDF file by using the code snippet below:
// Instantiate Pdf object by calling its empty constructor
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
// Create a section in the Pdf object
Aspose.Pdf.Page page = doc.Pages.Add();
Aspose.Pdf.Text.TextFragment fragment = new Aspose.Pdf.Text.TextFragment("");
Aspose.Pdf.Text.TextSegment segment = new Aspose.Pdf.Text.TextSegment(" This is a sample text using Custom font.");
Aspose.Pdf.Text.TextState ts = new Aspose.Pdf.Text.TextState();
ts.Font = FontRepository.OpenFont( dataDir + @"\P022X14T\P022X14T.otf");
segment.TextState = ts;
fragment.Segments.Add(segment);
page.Paragraphs.Add(fragment);
//Convert document into PDF/A format
doc.Convert(new MemoryStream(), PdfFormat.PDF_A_1B, ConvertErrorAction.None);
dataDir = dataDir + "EmbedFontWhileDocCreation_18.2.pdf";
// Save PDF Document
doc.Save(dataDir);
Resultant PDF document EmbedFontWhileDocCreation_18.2.pdf has embedded font but it does not conform to PDF/A requirements because empty TextFragment object in your code snippet is not fully correct for this case - empty TextFragment leads to an additional Arial font which has no text and this additional font without text violates PDF/A requirements.
In case you want to make document PDF/A compliant, better use the approach with non-empty TextFragment and set desired font for this TextFragment directly as in the code snippet below:
// Instantiate Pdf object by calling its empty constructor
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
// Create a section in the Pdf object
Aspose.Pdf.Page page = doc.Pages.Add();
Aspose.Pdf.Text.TextFragment fragment = new Aspose.Pdf.Text.TextFragment(" This is a sample text using Custom font.");
fragment.TextState.Font = FontRepository.OpenFont(@"D:\P022X14T\P022X14T.otf");
page.Paragraphs.Add(fragment);
//Convert document into PDF/A format
doc.Convert(new MemoryStream(), PdfFormat.PDF_A_1B, ConvertErrorAction.None);
string dataDir = @"D:\EmbedFontWhileDocCreation_18.2.pdf";
// Save PDF Document
doc.Save(dataDir);
I hope this will be helpful. Feel free to let us know if you need any further assistance.