Frequently Asked Question:
Draw text with Korean font onto PDF
I am trying to type some Korean characters in the PDF with this library, but it looks impossible to add Korean characters with this. Please let me know the best way to display Koreans.
I used source below:
PDFLibrary := TQuickPDF0718.Create;
try
UnlockResult := PDFLibrary.UnlockKey('...');
//Label1.Caption := PDFLibrary.LicenseInfo;
if UnlockResult = 1 then
begin
//PDFLibrary.AddTrueTypeFont('굴림',1);
idx := PDFLibrary.AddTrueTypeFont('굴림',1);
PDFLibrary.SelectFont(idx);
PDFLibrary.DrawText(100, 500, '한글테스트');
PDFLibrary.SaveToFile('C:\HelloFromDelphi.pdf');
end
else
begin
ShowMessage('Invalid license key');
end;
finally
PDFLibrary.Free;
end;
We are improving support for Korean fonts in future versions of Quick PDF Library - but at the moment the only way to get it to work is to use a subsetted font.
The AddSubsettedFont function can be used to create a subsetted font. You need to give it an 8-bit font name. We installed the ie_ko.exe font pack from Microsoft and the font name was shown as "Gulim".
The second parameter is the code page to use. We have used the number 5 for Korean.
The third parameter is a WideString containing the characters to include in the subsetted font.
Then you can use the normal text drawing functions but you will need to convert your 16-bit text strings into an 8-bit string with the mapped character codes. The GetSubsetString function can be used for this.
Here's a quick example:
procedure TForm1.Button1Click(Sender: TObject);
var
PDFLibrary: TQuickPDF0718;
UnlockResult: Integer;
SubsetChars: WideString;
MappedString: AnsiString;
begin
PDFLibrary := TQuickPDF0718.Create;
try
UnlockResult := PDFLibrary.UnlockKey('...');
if UnlockResult = 1 then
begin
// Set the WideString, in Delphi 2009 and later the Unicode
// string constant can rather be set directly
SubsetChars := '';
SubsetChars := SubsetChars + WideChar($C548) + WideChar($B155) +
WideChar($D558) + WideChar($C138) + WideChar($C694);
// Add a subsetted font with the list of used characters.
// Only 8-bit font names are supported, we have used "Gulim"
// for the font name
PDFLibrary.AddSubsettedFont('Gulim', 5, SubsetChars);
// Get a mapped string from the original string
MappedString := QP.GetSubsetString(SubsetChars);
PDFLibrary.DrawText(100, 500, MappedString);
PDFLibrary.SaveToFile('C:\Test.pdf');
end else
begin
ShowMessage('Invalid license key');
end;
finally
PDFLibrary.Free;
end;
end;
In future versions of Quick PDF Library all of this subsetting and mapping between 16-bit and 8-bit strings will be handled automatically by the library. For now we are limited to 8-bit strings because of historical reasons.