Frequently Asked Question:
Unicode example with c++ or c#
Is there any code example how to print unicode strings in C# or C++?
I have tried following:
string cs_drawstr = "ČĐĕكČĐĕĚ€ßTESTҰӘЖτ§â"; //ĈĄčďĀčď";
byte[] unicode_drawstr = Encoding.Unicode.GetBytes(cs_drawstr);
// Add a subset font for the text string
int id = QuickPDFAddSubsettedFont(qp.instanceID, "Times New Roman", i, unicode_drawstr);
if (id != 0)
{
qp.SelectFont(id);
// Remap the string to ensure that the correct character codes are used.
string substr = QuickPDFGetSubsetString(qp.instanceID, unicode_drawstr);
int y = i % 20;
int x = i / 20;
// Draw the Unicode text onto the page
QuickPDFDrawText(qp.instanceID, 100 + (x * 200), 600 - (y * 15), substr);
//qp.DrawText(100 + (x * 200), 600 - (y * 15), substr);
}
But the string gets splitted as soon as the unicode string contains a zero, which is on the first "normal" character, the T, in this case.
I tried also the C++ version to rule out possibly marshalling problems with exactly the same result. It seems, that when passing the std:string the string gets truncated as soon as the unicode string contains a byte with the value 0, which is pretty often.
I have spent the whole day trying to find an answer or a example to show me how to do this right, but it is almost impossible. I guess I will have to install Delphi and compile the library myself to find out what isn't working. From what I have seen, the coding of the Delphi part speaks against all rules of clean interfacing to other languages, as the memory allocation should take place by the caller.
Is there anyone besides me using this library with unicode in anything else than Delphi?
The 7.23 version of the Quick PDF Library DLL Edition will have some changes made to the C# wrapper.
Instead of sending a pointer to the string, a temporary buffer will be created using the QuickPDFCreateBuffer
function. This allows the length of the string to be explicitly set.
Using the QuickPDFAddSubsettedFont
function as an example, the following changes will be made:
- The
SubsetChars
parameter will be changed toIntPtr
. - The
PDFLibrary.AddSubsettedFont
function will be changed to:
public int AddSubsettedFont(string FontName, int CharsetIndex, string SubsetChars)
{
if(dll==null)return0;
else
{
byte[]ba=Encoding.Unicode.GetBytes(SubsetChars);
GCHandlegch=GCHandle.Alloc(ba,GCHandleType.Pinned);
IntPtrbufferID=dll.QuickPDFCreateBuffer(instanceID,ba.Length);
dll.QuickPDFAddToBuffer(instanceID,bufferID,gch.AddrOfPinnedObject(),ba.Length);
intresult=dll.QuickPDFAddSubsettedFont(instanceID,FontName,CharsetIndex,bufferID);
dll.QuickPDFReleaseBuffer(instanceID,bufferID);
gch.Free();
returnresult;
}
}