On pdflib Chinese output (ii) How to use other Simplified Chinese fonts in pdflib

Source: Internet
Author: User
Tags contains pdflib printf resource sprintf

In addition to Pdflib fonts, users can also use fonts and other user fonts that are installed on the system.

Pdflib the TrueType, OpenType, and PostScript fonts installed in Windows and Mac operating systems (existing or copied into the corresponding system font directory) are host fonts (host font). Pdflib can be invoked directly by reference to the font name, but must be exactly the same as the file name (strictly case-sensitive). For example, to invoke a font installed on a Windows system:

C:\WINDOWS\Fonts\SimHei.ttf

int font_cs = 0;

Font_cs = Pdf_load_font (P, "Simhei", 0, "Unicode", "");

Note that the font name may be different from the font filename, or even the same font name will be different under the operating system of the same language. To view the font name in a Windows environment, double-click the font file, and the first line of the window opens with the end TrueType, OpenType the font name. For example, to invoke a font installed on a Windows system: C:\WINDOWS\Fonts\SimHei.ttf, after double-clicking the file, the window's first behavior is "bold TrueType." The file's font name is "bold". To invoke multibyte file names in Pdflib, it must be in the form of bom+ UTF8. The form of the bom+ UTF8 of "blackbody" is "\xef\xbb\xbf\xe9\xbb\x91\xe4\xbd\x93".

So for the Chinese blackbody, under Chinese windows, then we use

Pdf_load_font (P, "\xef\xbb\xbf\xe9\xbb\x91\xe4\xbd\x93", 0, "Unicode", "");

Under Windows English, you should use the

Pdf_load_font (P, "Simhei", 0, "Unicode", "");

(Small tip: We can use WINDOWS2000/XP Notepad to obtain UTF8 code, the specific method Example: in the Notepad input "bold" and save, save in the Code drop-down box to select UTF-8, and then with Ultraedit,winhex, VC, etc. can be used for binary editing tool to open the file can be obtained with the BOM UTF8 string)

In addition to the fonts installed in the Windows system, Pdflib can invoke other user fonts. However, the path name needs to be given at the time of invocation. If I want to use C:\Program files\adobe\acrobat 7.0\RESOURCE\CIDFONT\ADOBESONGSTD-LIGHT.OTF this font:

Font_CS= PDF_load_font(p,
"C:\\Program Files\\Adobe\\Acrobat 7.0\\Resource\\CIDFont\\ AdobeSongStd-Light",
0, "unicode", "");

But here's the exception, that is. TTC (TrueType Collection) font. TTC is a collection font file that contains multiple fonts in each file. So the user cannot invoke the font with the filename, but instead use the real font name. For example, we know C:\WINDOWS\Fonts\MSGOTHIC. TTC contains three fonts, named Ms Gothic,ms PGothic, in turn, and Ms UI Gothic. We can invoke them with their corresponding font names:

int Font_E = 0;
Font_E= PDF_load_font(p, "MS Gothic", 0, "winansi", ""); /* Use MS Gothic */
PDF_setfont(p, Font_E, 20);
PDF_show_xy(p, "MS Gothic font:" , 50, 800);
Font_E= PDF_load_font(p, "MS PGothic", 0, "winansi", ""); /* Use MS PGothic */
……
Font_E= PDF_load_font(p, "MS UI Gothic", 0, "winansi", ""); /* Use MS UI Gothic */

But we often don't know. What fonts are included in TTC. In this case Pdflib provides another way to invoke-the index. In this way, you must first give the font file name an alias, and then add the number with a colon after the alias (0 for the first font in the file, 1 for the second, and so on). )

int Font_E = 0;
/* Give “C:\WINDOWS\Fonts\MSGOTHIC.TTC an alias “gothic” */
PDF_set_parameter(p, "FontOutline", "gothic=C:\\WINDOWS\\Fonts\\MSGOTHIC.TTC");
Font_E= PDF_load_font(p, "gothic:0", 0, "winansi", ""); /* Use MS Gothic */
Font_E= PDF_load_font(p, "gothic:1", 0, "winansi", ""); /* Use MS PGothic */
Font_E= PDF_load_font(p, "gothic:2", 0, "winansi", ""); /* Use MS UI Gothic */

The following is a related example--c source program

/*******************************************************************//* This example demostrates the usage of host
Font and other fonts/* Based on Chinese simplifed Windows. /*******************************************************************/#include <stdio.h> #include <
    stdlib.h> #include <string.h> #include "pdflib.h" int main (void) {PDF *p = NULL;
    int i = 0, j = 0, left = +, top = 800;
    int font_e = 0, Font_cs = 0;
    Char fontfile[1024];
    Char buf[1024];
    Char textunicode[] = "\x80\x7b\x53\x4f\x2d\x4e\x87\x65";  /* Create a new Pdflib object */if (P = pdf_new ()) = = (PDF *) 0) {printf ("couldn ' t create Pdflib object
        (out of memory)!\n ");
    return (2); } pdf_try (P) {if (Pdf_begin_document (p, "Pdflib_cs2.pdf", 0, "") = = 1) {printf ("Error:%s\n", p
	    Df_get_errmsg (p));
	return (2);
	} pdf_set_info (P, "Creator", "pdflib_cs2.c");Pdf_set_info (P, "Author", "myi@pdflib.com");
        Pdf_set_info (P, "Title", "Output Chinese simplify with host font and others"); /* Start a new page.
        * * Pdf_begin_page_ext (P, A4_width, A4_height, "");
        font_e = Pdf_load_font (P, "Helvetica-bold", 0, "Winansi", ""); 
           /* Using host font-C:\WINDOWS\Fonts\SimHei.ttf. Pdflib is using BOM UTF8 a string to calling Multi-Byte character string Simhei.ttf The font name is "bold", its corres Ponding BOM UTF8 string is "\xef\xbb\xbf\xe9\xbb\x91\xe4\xbd\x93" * * font_cs= Pdf_load_font (P, "\xef\x
        Bb\xbf\xe9\xbb\x91\xe4\xbd\x93 ", 0," Unicode "," "); /* font_cs= Pdf_load_font (P, "Simhei", 0, "Unicode", "");
        * * Pdf_setfont (P, font_e, 20);
        Pdf_show_xy (P, "Simhei font:", left, top);
        Pdf_setfont (P, Font_cs, 24);
        top-=30;
        Pdf_show_xy (P, Textunicode, left, top); /* Using The other disk-based font file, isn't installed in system directory--c:\psfonts\cs\gkai00mp.ttf*/top-=50;
        strcpy (Fontfile, "C:\\psfonts\\cs\\gkai00mp.ttf");
        sprintf (buf, "kai=%s", fontfile); /* Defines Kai as alias for.. 
        \gkai00mp.ttf * * Pdf_set_parameter (P, "Fontoutline", buf);
        Font_cs= Pdf_load_font (P, "Kai", 0, "Unicode", "");
        Pdf_setfont (P, font_e, 20);
        Pdf_show_xy (P, "AR PL kaitim GB font:", left, top);
        Pdf_setfont (P, Font_cs, 24);
        top-=30;
        Pdf_show_xy (P, Textunicode, left, top);
        /* Using TrueType Collection font with index-c:\windows\fonts\simsun.ttc*/top-=50;
        strcpy (Fontfile, "C:\\WINDOWS\\FONTS\\SIMSUN.TTC");
        sprintf (buf, "simsun=%s", fontfile); /* defines ADOBESONGSTD as alias for.. 
        \ADOBESONGSTD-LIGHT.OTF This is need to claim once is sufficient to configure all fonts in simsun.ttc*/ 
        Pdf_set_parameter (P, "Fontoutline", buf); * TTC files contain multiple SEParate fonts. Address 1st font by appending a colon character and 0 after alias SimSun/font_cs= Pdf_load_font (P, "simsun:0",
        0, "Unicode", "" ");
        Pdf_setfont (P, font_e, 20);
        Pdf_show_xy (P, "simsun:0 font:", left, top);
        Pdf_setfont (P, Font_cs, 24);
        top-=30;
        Pdf_show_xy2 (P, Textunicode, 8, left, top);
        /*address 2nd font by appending a colon character and 1 after alias SimSun/top-=50;
        Font_cs= Pdf_load_font (P, "Simsun:1", 0, "Unicode", "");
        Pdf_setfont (P, font_e, 20);
        Pdf_show_xy (P, "Simsun:1 font:", left, top);
        Pdf_setfont (P, Font_cs, 24);
        top-=30;
        Pdf_show_xy2 (P, Textunicode, 8, left, top); /* End of page.
        * * Pdf_end_page_ext (P, "");
    Pdf_end_document (P, "");
        } pdf_catch (P) {printf ("Pdflib exception occurred in PDFLIB_CS2 sample:\n"); printf ("[%d]%s:%s\n", Pdf_get_errnum (p), Pdf_get_apiname (p), pdf_get_errmsg(p));
        Pdf_delete (P);
    return (2);
    } pdf_delete (P);
return 0; }

This article supporting source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.