Skip to content

Commit c871666

Browse files
1015631: changes commit
1 parent c0d686a commit c871666

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

  • Document-Processing/PDF/PDF-Library/javascript

Document-Processing/PDF/PDF-Library/javascript/Text.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,5 +406,60 @@ document.save('Output.pdf');
406406
// Close the document
407407
document.destroy();
408408

409+
{% endhighlight %}
410+
{% endtabs %}
411+
412+
## Embedded font
413+
414+
This example shows how to embed fonts using `PdfDocument.embedFont()` method to ensure consistent text rendering across all platforms. The library supports embedding `PdfStandardFont`, `PdfCjkStandardFont`, and `PdfTrueTypeFont` for reliable Unicode text display. After embedding, the font can be applied through `PdfFont.getFont()` method, allowing precise control over size and style. Additionally, using embedded fonts helps reduce overall PDF size, since the font dictionary is not duplicated for each usage—ensuring cleaner and more efficient output.
415+
416+
{% tabs %}
417+
{% highlight typescript tabtitle="TypeScript" %}
418+
import { PdfDocument, PdfPage, PdfFont, PdfStandardFont, PdfCjkStandardFont, PdfFontFamily, PdfFontStyle, PdfCjkFontFamily, PdfBrush } from '@syncfusion/ej2-pdf';
419+
420+
// Create a new PDF document
421+
let document: PdfDocument = new PdfDocument();
422+
// Add a page
423+
let page: PdfPage = document.addPage();
424+
// Embed a standard font into the PDF document.
425+
const embedded1: PdfStandardFont = document.embedFont(PdfFontFamily.timesRoman, 12, PdfFontStyle.regular);
426+
// Gets a font variant from the base font with the given size and style
427+
const embedded2: PdfFont = embedded1.getFont(14, PdfFontStyle.bold);
428+
const embedded3: PdfFont = embedded1.getFont(14, PdfFontStyle.italic);
429+
// Embed a CJK font into the PDF document.
430+
const embedded4: PdfCjkStandardFont = document.embedFont(PdfCjkFontFamily.hanyangSystemsGothicMedium, 12, PdfFontStyle.regular , true);
431+
// Draw string using embed font.
432+
page.graphics.drawString('timesRoman with regular', embedded1, {x: 10, y: 10, width: 300, height: 24}, new PdfBrush({r: 0, g: 0, b: 255}));
433+
page.graphics.drawString('timesRoman with bold', embedded2, {x: 10, y: 50, width: 300, height: 24}, new PdfBrush({r: 0, g: 0, b: 255}));
434+
page.graphics.drawString('timesRoman with italic', embedded3, {x: 200, y: 50, width: 300, height: 24}, new PdfBrush({r: 0, g: 0, b: 255}));
435+
page.graphics.drawString('Cjkfont with regular', embedded4, {x: 200, y: 10, width: 300, height: 24}, new PdfBrush({r: 0, g: 0, b: 255}));
436+
// Save the document
437+
document.save('Output.pdf');
438+
// Close the document
439+
document.destroy();
440+
441+
{% endhighlight %}
442+
{% highlight javascript tabtitle="JavaScript" %}
443+
444+
// Create a new PDF document
445+
var document = new new ej.pdf.PdfDocument();
446+
// Add a page
447+
var page = document.addPage();
448+
// Embed a font into the PDF document.
449+
const embedded1 = document.embedFont(new ej.pdf.PdfFontFamily.timesRoman, 12, new ej.pdf.PdfFontStyle.regular);
450+
const embedded2 = document.embedFont(new ej.pdf.PdfCjkFontFamily.hanyangSystemsGothicMedium, 12, new ej.pdf.PdfFontStyle.regular , true);
451+
// Gets a font variant from the base font with the given size and style
452+
const embedded3 = embedded1.getFont(14, new ej.pdf.PdfFontStyle.bold);
453+
const embedded4 = embedded4.getFont(14, new ej.pdf.PdfFontStyle.bold);
454+
// Draw string using embed font.
455+
page.graphics.drawString('timesRoman with regular', embedded1, {x: 10, y: 10, width: 300, height: 24}, new ej.pdf.PdfBrush({r: 0, g: 0, b: 255}));
456+
page.graphics.drawString('timesRoman with bold', embedded3, {x: 10, y: 50, width: 300, height: 24}, new ej.pdf.PdfBrush({r: 0, g: 0, b: 255}));
457+
page.graphics.drawString('Cjkfont with regular', embedded2, {x: 200, y: 10, width: 300, height: 24}, new ej.pdf.PdfBrush({r: 0, g: 0, b: 255}));
458+
page.graphics.drawString('Cjkfont with bold', embedded4, {x: 200, y: 50, width: 300, height: 24}, new ej.pdf.PdfBrush({r: 0, g: 0, b: 255}));
459+
// Save the document
460+
document.save('Output.pdf');
461+
// Close the document
462+
document.destroy();
463+
409464
{% endhighlight %}
410465
{% endtabs %}

0 commit comments

Comments
 (0)