问题原因:在10.0之前的版本中,嵌入字体Font::Embed ()接口,是嵌入整个字体文件,包含所有字符。所以会导致嵌入时耗时长,和文件体积增大的情况。
解决办法:在10.0及以上的版本中,Font类型增加了Font::AddUnicodes ()接口,支持按用户指定字符数组嵌入字体,C++的示例代码如下:
PDFDoc doc;
doc.Load(NULL);
PDFPage page= doc.InsertPage(0,PDFPage::e_SizeA4);
page.StartParse(0, NULL, true);
POSITION position = page.GetLastGraphicsObjectPosition(GraphicsObject::e_TypeText);
TextObject* text_object = TextObject::Create();
Font font = Font(L"Noto Sans CJK HK", Font::e_StylesSmallCap, Font::e_CharsetANSI, 0);
//第二个参数为false时,返回的嵌入字体,将只嵌入指定字符(默认为true)
Font font_Embed = font.Embed(doc, false);
TextState state;
WString wstr = L"foxitsoftware";
UInt32Array wstr_Unicode;
//设置要嵌入的字符
for (int i = 0; i < wstr.GetLength(); i++)
{
wstr_Unicode.Add(static_cast<uint32_t>(wstr[i]));
}
UInt32Array wstr_Unicod_Embed = font_Embed.AddUnicodes(doc, wstr_Unicode);
//检查指定的字符是否全部成功嵌入
if (wstr_Unicode.GetSize() == wstr_Unicod_Embed.GetSize())
{
state.font = font_Embed;
}
state.font_size = 48;
state.textmode = TextState::e_ModeFillStrokeClip;
text_object->SetTextState(page, state, true, 750);
text_object->SetText(wstr);
text_object->SetFillColor(0xFFAAAAAA);
text_object->SetStrokeColor(0xFFF68C21);
page.InsertGraphicsObject(position, text_object);
RectF rect = text_object->GetRect();
float offset_x = (page.GetWidth() - (rect.right - rect.left)) / 2;
float offset_y = page.GetHeight() * 0.8f - (rect.top - rect.bottom) / 2;
text_object->Transform(Matrix(1, 0, 0, 1, offset_x, offset_y), false);
page.GenerateContent()
doc.SaveAs(L"D:/test.pdf", 0);