Skip to content

Commit 6cedbfb

Browse files
Merge pull request #1692 from syncfusion-content/989885-ug
989885-ug: Added missing cross-platform in work with text.
2 parents 06115db + fdab0d0 commit 6cedbfb

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

Document-Processing/PDF/PDF-Library/NET/Working-with-Text.md

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,33 @@ document.Close(True)
386386

387387
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Text/Draw-text-in-PDF-document-using-standard-fonts/).
388388

389-
### Draw text using TrueType fonts
389+
### Draw Text using TrueType fonts
390390

391-
You can add text using the TrueType fonts installed in the system, by initializing [PdfFont](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfFont.html) class as [PdfTrueTypeFont](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfTrueTypeFont.html) class. The following code snippet illustrates this.
391+
You can add text using TrueType fonts either installed on the system or provided as a font stream by initializing the [PdfFont](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfFont.html) class as a [PdfTrueTypeFont](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfTrueTypeFont.html). The following code Example demonstrates this approach.
392392

393393
{% tabs %}
394394

395+
{% highlight c# tabtitle="C# [Cross-platform]" %}
396+
397+
//Create a new PDF document.
398+
PdfDocument document = new PdfDocument();
399+
//Add a page to the document.
400+
PdfPage page = document.Pages.Add();
401+
402+
//Create PDF graphics for the page.
403+
PdfGraphics graphics = page.Graphics;
404+
//Provide the path of the local *.ttf file
405+
PdfFont font = new PdfTrueTypeFont("Arial.ttf", 14);
406+
//Draw the text.
407+
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
408+
409+
//Save the document.
410+
document.Save("Output.pdf");
411+
//Close the document.
412+
document.Close(true);
413+
414+
{% endhighlight %}
415+
395416
{% highlight c# tabtitle="C# [Windows-specific]" %}
396417

397418
using Syncfusion.Pdf;
@@ -459,8 +480,7 @@ PdfPage page = document.Pages.Add();
459480
//Create PDF graphics for the page.
460481
PdfGraphics graphics = page.Graphics;
461482
//Load the TrueType font from the local *.ttf file.
462-
FileStream fontStream = new FileStream("Arial.ttf", FileMode.Open, FileAccess.Read);
463-
PdfFont font = new PdfTrueTypeFont(fontStream, 14);
483+
PdfFont font = new PdfTrueTypeFont("Arial.ttf", 14);
464484
//Draw the text.
465485
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));
466486

@@ -716,10 +736,8 @@ using Syncfusion.Pdf.Graphics;
716736
PdfDocument document = new PdfDocument();
717737
//Add a page to the document
718738
PdfPage page = document.Pages.Add();
719-
// Load the font file from the stream
720-
FileStream fontStream = new FileStream(@"../../../arial.ttf", FileMode.Open, FileAccess.Read);
721739
//Create a new PDF font instance
722-
PdfFont font = new PdfTrueTypeFont(fontStream, 14, PdfFontStyle.Italic);
740+
PdfFont font = new PdfTrueTypeFont("arial.ttf", 14, PdfFontStyle.Italic);
723741
//Create a new PDF string format instance
724742
PdfStringFormat format = new PdfStringFormat();
725743
//Enable the measure tilting space
@@ -774,10 +792,8 @@ Imports System.Drawing
774792
Dim document As PdfDocument = New PdfDocument()
775793
'Add a page to the document
776794
Dim page As PdfPage = document.Pages.Add()
777-
'Load the font file from the stream
778-
Dim fontStream As FileStream = New FileStream("font.ttf", FileMode.Open, FileAccess.Read)
779795
'Create a new PDF font instance
780-
Dim font As PdfFont = New PdfTrueTypeFont(fontStream, 14, PdfFontStyle.Italic)
796+
Dim font As PdfFont = New PdfTrueTypeFont("font.ttf", 14, PdfFontStyle.Italic)
781797
'Create a new PDF string format instance
782798
Dim format As PdfStringFormat = New PdfStringFormat()
783799
'Enable a measure tilting space
@@ -1054,8 +1070,7 @@ PdfPage page = doc.Pages.Add();
10541070
//Create PDF graphics for the page
10551071
PdfGraphics graphics = page.Graphics;
10561072
//Create a new PDF font instance
1057-
FileStream fontStream = new FileStream("arial.ttf", FileMode.Open, FileAccess.Read);
1058-
PdfFont font = new PdfTrueTypeFont(fontStream, 14);
1073+
PdfFont font = new PdfTrueTypeFont("arial.ttf", 14);
10591074
//Set the format for string
10601075
PdfStringFormat format = new PdfStringFormat();
10611076
//Set right-to-left text direction for RTL text
@@ -1065,8 +1080,7 @@ format.Alignment = PdfTextAlignment.Right;
10651080
format.ParagraphIndent = 35f;
10661081

10671082
//Read the text from file
1068-
FileStream rtlText = new FileStream("Arabic.txt", FileMode.Open, FileAccess.Read);
1069-
StreamReader reader = new StreamReader(rtlText, Encoding.Unicode);
1083+
StreamReader reader = new StreamReader("Arabic.txt", Encoding.Unicode);
10701084
string text = reader.ReadToEnd();
10711085
reader.Dispose();
10721086
//Draw string with right-to-left format
@@ -1451,8 +1465,7 @@ PdfPage page = document.Pages.Add();
14511465
//Create PDF graphics for the page
14521466
PdfGraphics graphics = page.Graphics;
14531467
//Read the long text from the text file
1454-
FileStream inputStream = new FileStream("Input.txt", FileMode.Open, FileAccess.Read);
1455-
StreamReader reader = new StreamReader(inputStream, Encoding.ASCII);
1468+
StreamReader reader = new StreamReader("Input.txt", Encoding.ASCII);
14561469
string text = reader.ReadToEnd();
14571470
reader.Dispose();
14581471
const int paragraphGap = 10;
@@ -2022,9 +2035,8 @@ PdfPage page = doc.Pages.Add();
20222035

20232036
//Create PDF graphics for the page
20242037
PdfGraphics graphics = page.Graphics;
2025-
FileStream fontStream = new FileStream("tahoma.ttf", FileMode.Open, FileAccess.Read);
20262038
//Create a new PDF font instance
2027-
PdfFont pdfFont = new PdfTrueTypeFont(fontStream, 10);
2039+
PdfFont pdfFont = new PdfTrueTypeFont("tahoma.ttf", 10);
20282040
//Set the format for string
20292041
PdfStringFormat format = new PdfStringFormat();
20302042
//Set the format as complex script layout type
@@ -2219,8 +2231,7 @@ PdfDocument document = new PdfDocument();
22192231
PdfPage page = document.Pages.Add();
22202232

22212233
//Create font
2222-
FileStream fontFileStream = new FileStream("Font.otf", FileMode.Open, FileAccess.Read);
2223-
PdfFont font = new PdfTrueTypeFont(fontFileStream, 14);
2234+
PdfFont font = new PdfTrueTypeFont("Font.otf", 14);
22242235
//Text to draw
22252236
string text = "Syncfusion Essential PDF is a.NET Core PDF library used to create, read, and edit PDF files in any application";
22262237
//Create a brush
@@ -2321,14 +2332,11 @@ PdfPage page = doc.Pages.Add();
23212332

23222333
//Create PDF graphics for the page
23232334
PdfGraphics graphics = page.Graphics;
2324-
FileStream fontStream = new FileStream("tahoma.ttf", FileMode.Open, FileAccess.Read);
2325-
FileStream fontStream1 = new FileStream("Arial.ttf", FileMode.Open, FileAccess.Read);
2326-
FileStream fontStream2 = new FileStream("Calibri.ttf", FileMode.Open, FileAccess.Read);
23272335
//Create a new PDF font instance
2328-
PdfFont font = new PdfTrueTypeFont(fontStream, 8);
2329-
PdfFont font1 = new PdfTrueTypeFont(fontStream1, 20);
2336+
PdfFont font = new PdfTrueTypeFont("tahoma.ttf", 8);
2337+
PdfFont font1 = new PdfTrueTypeFont("Arial.ttf", 20);
23302338
PdfFont font2 = new PdfStandardFont(PdfFontFamily.Helvetica,16);
2331-
PdfFont font3 = new PdfTrueTypeFont(fontStream2, 25);
2339+
PdfFont font3 = new PdfTrueTypeFont("Calibri.ttf", 25);
23322340
//Set the format for string
23332341
PdfStringFormat format = new PdfStringFormat();
23342342
//Set the line alignment

0 commit comments

Comments
 (0)