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

Free Support Forum - aspose.com

Printing a Text with max width without clipping whole words

using Aspose.Pdf.Generator if I need to print a text line with a maximum width of say 5 inches without clipping whole words, just the characters that not fit, can you please let me know a code sample?


Example: I have the phrase “This is just a Sample” and need to print it but just the characters that fit on specific width (“This is just a Samp” will fit), I don’t want to remove the whole last word (like “This is just a”).

Thanks.

I haven’t received any reply and would appreciate to know if this is something you are still investigating as I have a project to deliver that needs words not to be clipped when setting the max width.


Thanks,

jgarcia1:
using Aspose.Pdf.Generator if I need to print a text line with a maximum width of say 5 inches without clipping whole words, just the characters that not fit, can you please let me know a code sample?

Example: I have the phrase “This is just a Sample” and need to print it but just the characters that fit on specific width (“This is just a Samp” will fit), I don’t want to remove the whole last word (like “This is just a”).
Hi George,

Thanks for using our products and sorry for the delayed response.

I am afraid the requested feature is
currently not supported but for the sake of implementation, I have logged this
requirement in our issue tracking system under New Features list as PDFNEWNET-35496.
We will further investigate this requirement in details and will keep you
updated on the status of a correction. <o:p></o:p>

We apologize for your inconvenience.

Hi Nayyer, Thanks for your response.

Just to let you know that if what I need to print is just one word with many characters, Aspose.Pdf is able to truncate this only word correctly but not when the line has multiple words, in this case the whole word is truncated.

In the mean time, is there a way to calculate how many “points” specific phrase will occupy based on the font name and font size used? What I’m trying to figure out is if by dropping characters that do not fit on the specific fields size I can fix this issue.

PLEASE ADVISE.

Thanks,
Jorge

Hi Jorge,


Thanks for your feedback. I’m afraid currently there is no such feature to calculate the size of some text phrase in points. However, I’ve logged an investigation ticket as PDFNEWNET-35526 for the purpose in our issue tracking system. We will keep you updated about issue progress via this forum thread.

Sorry for the inconvenience faced.

Best Regards,

@jgarcia1

Thanks for your patience.

We have further investigated the earlier logged issue PDFNET-35526 and found that Aspose.Pdf already provides at least two ways to check TextFragment width. The first is TextFragment.Rectangle.Width property. And the second is TextFragment.TextState.MeasureString() function. Therefore you can use simple code to get string that will fit in the specified length (width) with current TextState.

Please consider the following code. It defines GetTrimmedStringByLength() function that returns trimmed string.

private static void UserCode()
{
    string myDir = @"D:\Temp\";

    Document pdfDocument = new Aspose.Pdf.Document();
    Page pdfPage = pdfDocument.Pages.Add();
    TextFragment textFragment = new TextFragment("long value for TextFragment for testing purposes....");
    Console.WriteLine("Original text: {0}", textFragment.Text);
    textFragment.Position = new Position(100, 600);
    textFragment.TextState.FontSize = 10;
    textFragment.TextState.Font = FontRepository.FindFont("Helvetica");
    textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Transparent);
    textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black);

    textFragment.HorizontalAlignment = HorizontalAlignment.Left;

    double length = textFragment.Segments[1].Rectangle.Width;
    Console.WriteLine("Text fragment rectangle width: {0}", length);
    Console.WriteLine("Text width measured by TextState: {0}", textFragment.TextState.MeasureString(textFragment.Text));
            
    //set text with length 70% of original example
    double newLength = length * 0.70;
    Console.WriteLine("Trimming text to new length...");
    string newText = GetTrimmedStringByLength(textFragment.Text, newLength, textFragment.TextState);
    Console.WriteLine("Trimmed text: {0}", newText);
    textFragment.Text = newText;
    Console.WriteLine("Text fragment rectangle width: {0}", textFragment.Rectangle.Width);

    //Create TextBuilder object
    TextBuilder textBuilder = new TextBuilder(pdfPage);

    //Append the text fragment to the PDF page
    textBuilder.AppendText(textFragment);

    pdfDocument.Save(myDir + "TextFragmentAdded.pdf");
}

private static string GetTrimmedStringByLength(string originalText, double length, TextState textState)
{
    StringBuilder trimmedText = new StringBuilder(originalText);
    double currentLength = textState.MeasureString(originalText);
    while (currentLength > length && trimmedText.Length > 0)
    {
        trimmedText.Length -= 1;
        currentLength = textState.MeasureString(trimmedText.ToString());
    }

    return trimmedText.ToString();
}

Please try above code snippet while using latest version Aspose.Pdf for .NET 17.11 and in event of any further query, please feel free to let us know.