Skip to content

Commit 4c84bc7

Browse files
Merge pull request #1646 from syncfusion-content/989050-ug1
989050-ug1: Added PdfUnitConvertor for Unit Conversion in PDF Text Layout
2 parents f5d5fd7 + 9b17f99 commit 4c84bc7

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed

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

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,166 @@ document.Close(True)
798798

799799
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Text/Measure-tilting-space-in-PDF/.NET).
800800

801+
## Unit conversion in text layout
802+
803+
The [PdfUnitConverter](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfUnitConvertor.html) class is used to accurately position and layout paragraph text within a PDF document. By converting measurements from inches to points, it ensures consistent margins and precise placement of content.
804+
805+
This example demonstrates how to use converted units to accurately position and format paragraph text within a PDF document.
806+
807+
{% tabs %}
808+
809+
{% highlight c# tabtitle="C# [Cross-platform]" %}
810+
811+
using Syncfusion.Pdf;
812+
using Syncfusion.Pdf.Graphics;
813+
using Syncfusion.Drawing;
814+
815+
// Create a new PDF document
816+
using (PdfDocument document = new PdfDocument())
817+
{
818+
// Add a page
819+
PdfPage page = document.Pages.Add();
820+
821+
// Initialize unit converter
822+
PdfUnitConverter converter = new PdfUnitConverter();
823+
824+
// Convert margins from inches to points
825+
float margin = converter.ConvertUnits(1f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
826+
827+
// Define text bounds to fill the page with margins
828+
RectangleF textBounds = new RectangleF(
829+
margin,
830+
margin,
831+
page.Graphics.ClientSize.Width - 2 * margin,
832+
page.Graphics.ClientSize.Height - 2 * margin
833+
);
834+
835+
// Define font and paragraph text
836+
PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14);
837+
838+
string paragraphText = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is located in Washington with 290 employees, several regional sales teams are located throughout their market base.";
839+
840+
// Create text element and layout format
841+
PdfTextElement textElement = new PdfTextElement(paragraphText, font, PdfBrushes.Black);
842+
843+
PdfLayoutFormat layoutFormat = new PdfLayoutFormat
844+
{
845+
Break = PdfLayoutBreakType.FitPage,
846+
Layout = PdfLayoutType.Paginate
847+
};
848+
849+
// Draw the paragraph text within the bounds
850+
textElement.Draw(page, textBounds, layoutFormat);
851+
852+
//Save the document
853+
document.Save("Output.pdf");
854+
}
855+
856+
{% endhighlight %}
857+
858+
{% highlight c# tabtitle="C# [Windows-specific]" %}
859+
860+
using Syncfusion.Pdf;
861+
using Syncfusion.Pdf.Graphics;
862+
using System.Drawing;
863+
864+
// Create a new PDF document
865+
using (PdfDocument document = new PdfDocument())
866+
{
867+
// Add a page
868+
PdfPage page = document.Pages.Add();
869+
870+
// Initialize unit converter
871+
PdfUnitConverter converter = new PdfUnitConverter();
872+
873+
// Convert margins from inches to points
874+
float margin = converter.ConvertUnits(1f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
875+
876+
// Define text bounds to fill the page with margins
877+
RectangleF textBounds = new RectangleF(
878+
margin,
879+
margin,
880+
page.Graphics.ClientSize.Width - 2 * margin,
881+
page.Graphics.ClientSize.Height - 2 * margin
882+
);
883+
884+
// Define font and paragraph text
885+
PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14);
886+
887+
string paragraphText = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is located in Washington with 290 employees, several regional sales teams are located throughout their market base.";
888+
889+
// Create text element and layout format
890+
PdfTextElement textElement = new PdfTextElement(paragraphText, font, PdfBrushes.Black);
891+
892+
PdfLayoutFormat layoutFormat = new PdfLayoutFormat
893+
{
894+
Break = PdfLayoutBreakType.FitPage,
895+
Layout = PdfLayoutType.Paginate
896+
};
897+
898+
// Draw the paragraph text within the bounds
899+
textElement.Draw(page, textBounds, layoutFormat);
900+
901+
//Save the document
902+
document.Save("Output.pdf");
903+
}
904+
905+
{% endhighlight %}
906+
907+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
908+
909+
Imports Syncfusion.Pdf
910+
Imports Syncfusion.Pdf.Graphics
911+
Imports System.Drawing
912+
913+
Module Program
914+
Sub Main()
915+
' Create a new PDF document
916+
Using document As New PdfDocument()
917+
918+
' Add a page
919+
Dim page As PdfPage = document.Pages.Add()
920+
921+
' Initialize unit converter
922+
Dim converter As New PdfUnitConverter()
923+
924+
' Convert 1 inch margin to points
925+
Dim margin As Single = converter.ConvertUnits(1.0F, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point)
926+
927+
' Define text bounds to fill the page with margins
928+
Dim textBounds As New RectangleF(
929+
margin,
930+
margin,
931+
page.Graphics.ClientSize.Width - 2 * margin,
932+
page.Graphics.ClientSize.Height - 2 * margin
933+
)
934+
935+
' Define font and paragraph text
936+
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.TimesRoman, 14)
937+
Dim paragraphText As String = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is located in Washington with 290 employees, several regional sales teams are located throughout their market base."
938+
939+
' Create text element and layout format
940+
Dim textElement As New PdfTextElement(paragraphText, font, PdfBrushes.Black)
941+
Dim layoutFormat As New PdfLayoutFormat With {
942+
.Break = PdfLayoutBreakType.FitPage,
943+
.Layout = PdfLayoutType.Paginate
944+
}
945+
946+
text within the bounds
947+
textElement.Draw(page, textBounds, layoutFormat)
948+
949+
' Save the document
950+
document.Save("Output.pdf")
951+
End Using
952+
End Sub
953+
End Module
954+
955+
{% endhighlight %}
956+
957+
{% endtabs %}
958+
959+
You can download a complete working sample from GitHub.
960+
801961
## Embedding fonts and working with Unicode text
802962

803963
To embed a font or display Unicode text in the document, the ‘Unicode’ Boolean parameter of the [PdfTrueTypeFont](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfTrueTypeFont.html#Syncfusion_Pdf_Base__ctor) constructor has to be set to true. The following code illustrates the same.

0 commit comments

Comments
 (0)