We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Exact position of each character in a TextLine

I am creating a new PDF from scratch.

Using Aspose.Pdf.Generator Namespace classes.

I need to define different spacings for each character-pair.

example sentence: "This is my test-sentence"

I need to define spacing between First and Second letter (between ‘T’ and ‘h’ in above example)
the spacing between second an third letter (between ‘h’ and ‘i’) is different from the first spacing.
the spacing between third and fourth letter (‘between i’ and ‘s’) is different again.
the spacing between fourth and fifth letter (between ‘s’ and first letter ‘i’ of next word) is different again.

Is there a way to define different spacings for each letter in a string?
Or can I set the exact position of each letter in a sentence?

Can it be accomplished with Aspose.Pdf.Generator classes ?
or do I have to use classes in Namespace Aspose.Pdf?

I prefer the coordinate-system of Aspose.Pdf.Generator where Top-Left-Corner is (X=0pt,Y=0pt)
while Aspose.Pdf.Document coordinate-system Bottom-Left-Corner ist (X=0pt,Y=0pt)


kind regards
Markus

Hi Markus,


Thanks for your inquiry. Yes you can set CharacterSpacing property of TextState of TextSegment to set different spacing for a text string. Please check following code snippet for the purpose.

It is recommended to use new generator(Aspose.Pdf.Document), as it is more improved and efficent. It can be used for both creating PDF document from scratch or manipulate existing PDF document. Furthermore old generator will be obsolete in new future.

Document pdfDocument = new Document();<o:p></o:p>

Aspose.Pdf.Page page = pdfDocument.Pages.Add();

TextFragment textFragment1 = new TextFragment();

TextSegment tSegment1 = new TextSegment("T");

tSegment1.TextState.CharacterSpacing = 1f;

TextSegment tSegment2 = new TextSegment("h");

tSegment2.TextState.CharacterSpacing = 1.5f;

TextSegment tSegment3 = new TextSegment("i");

tSegment3.TextState.CharacterSpacing = 2f;

TextSegment tSegment4 = new TextSegment("s");

tSegment4.TextState.CharacterSpacing = 4f;

textFragment1.Segments.Add(tSegment1);

textFragment1.Segments.Add(tSegment2);

textFragment1.Segments.Add(tSegment3);

textFragment1.Segments.Add(tSegment4);

TextFragment textFragment2 = new TextFragment("This");

//textFragment1.TextState.CharacterSpacing = 2.0f;

page.Paragraphs.Add(textFragment1);

page.Paragraphs.Add(textFragment2);

pdfDocument.Save(myDir+"CharacterSpacing.pdf");


Please feel free to contact us for any further assistance.

Best Regards,