Migrating from Pdf to Document

Hi


As suggested we are migrating our code to use Document object instead of Pdf object. So i was using GetStringWidth function of Pdf class but there is no such function in Document class.

Can you guide me what i need to do?

Regards,
Sandeep

Hi Sandeep,


Thanks for your inquiry. After initial investigation we have logged an issue as PDFNEWNET-36575 for alternate of GetStringWidth() in new DOM approach. We will keep you updated about the issue resolution progress via this forum thread.

Best Regards,

Hi Sandeep,


Aspose.Pdf.Generator supports the feature to create PDF files from scratch and in order to get the string width, the PDF file first needs to be saved over the system. Now concerning to your requirement, you may consider using following code snippet where you can use textFragment.Text.Length to get the width of TextFragment.

[C#]

//open document<o:p></o:p>

Document pdfDocument = new Document();

//get particular page

Page pdfPage = (Page)pdfDocument.Pages.Add();

//create text fragment

TextFragment textFragment = new TextFragment("main text");

textFragment.Position = new Position(100, 600);

Console.WriteLine(textFragment.Text.Length);

// create TextBuilder object

TextBuilder textBuilder = new TextBuilder(pdfPage);

// append the text fragment to the PDF page

textBuilder.AppendText(textFragment);

//save document

pdfDocument.Save(“c:/pdftest/TextSegment_output.pdf”);

Hi,


Thanks for your answer. But this is not what i was expecting. For your information GetStringWidth function didn’t give us the number of characters in string object. While it give us the width of the string depending on the TextInfo. That means that same string will have different width depending on Font, FontSize etc.

So code snippet given is not the give the same result.

Please respond ASAP.

Regards,
Sandeep

Hi Sandeep,


We already have logged a requirement in our issue tracking system to support similar feature in DOM approach. However you may consider getting rectangular region/coordinates for particular TextFragment. Please take a look over following code snippet and see if it can accomplish your requirement.

[C#]

//open document<o:p></o:p>

Document pdfDocument = new Document();

//get particular page

Page pdfPage = (Page)pdfDocument.Pages.Add();

//create text fragment

TextFragment textFragment = new TextFragment("main text");

textFragment.Position = new Position(100, 600);

Console.WriteLine("Rectangular Region of String = " + textFragment.Rectangle.Width);

textFragment.TextState.FontSize = 14;

Console.WriteLine("Rectangular Region of String = " + textFragment.Rectangle.Width);

// create TextBuilder object

TextBuilder textBuilder = new TextBuilder(pdfPage);

// append the text fragment to the PDF page

textBuilder.AppendText(textFragment);

//save document

pdfDocument.Save(“c:/pdftest/TextSegment_output.pdf”);



PS, when multiplying FontSize with character count in String, the value is different.

The issues you have found earlier (filed as PDFNEWNET-36575) have been fixed in Aspose.Pdf for .NET 9.2.0.

The blog post for this release is created over this link


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Can you please provide example for this.


Regards,
Sandeep

Hi Sandeep,


In order to accomplish your requirement, please try using the code snippet shared in my earlier post 532366.

As per post 543029 this function is included in new release. So just want to check what that function is and details around that.


Regards,
Sandeep

Hi Sandeep,


The issue is marked as won’t fix because the requirement can be accomplished with code snippet shared above in this thread.