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

Free Support Forum - aspose.com

Changing TextFragment.Text property does not span text to next line when reaching page margin

My application parses a pdf document looking for document names that are bracketed by “{” and “]”. Once I get the list of string tokens found in the pdf, The app loops through them, and replaces “[” and “]” with string.Empty. I also look for “\r\n” and replace that with a " " (SPACE). If a string token is split across 2 lines, Aspose.Pdf TextFragment’s Text property will contain “\r\n” in the string. Now, I assumed, that when I modify the Text property, the words would span to 2 second line if the resulting text would go beyond the page margins. However, what I have noticed, is that it doesn’t wrap the text.

How can I modify my code below in order to accomplish this task of making the TextFragment output to span across 2 lines if trhe text extends beyond the page margin?

THIS IS MY INPUT PDF
PublicDoc.pdf (45.9 KB)

THIS IS MY OUTPUT PDF
Hyperlinked Commitmen Output).pdf (221.0 KB)

THIS IS MY CODE

        // Update text and other properties
        textFragmentToContainLink.Text = this.Name;

        // TODO: MWK - ADD
        //if (hasLineBreak) {
        //    textFragmentToContainLink.TextState.HorizontalScaling = 75;
        //}

        // Create new text fragment
        TextFragment newFragment = new TextFragment(this.Name);
        newFragment.TextState.ApplyChangesFrom(textFragmentToContainLink.TextState);
        newFragment.TextState.FormattingOptions = new TextFormattingOptions(TextFormattingOptions.WordWrapMode.ByWords);
        newFragment.TextState.FontStyle = FontStyles.Italic;
        newFragment.TextState.Underline = true;
        newFragment.TextState.ForegroundColor = Color.Blue;

        // Create new text paragraph
        TextParagraph textParagraph = new TextParagraph();
        textParagraph.Position = new Position(
            textFragmentToContainLink.Position.XIndent, 
            textFragmentToContainLink.Position.YIndent - textFragmentToContainLink.TextState.FontSize 
                                                       - textFragmentToContainLink.TextState.LineSpacing);
        textParagraph.FormattingOptions = new TextFormattingOptions(TextFormattingOptions.WordWrapMode.ByWords);
        textParagraph.AppendLine(newFragment);

        // Add new paragraph to the document page
        TextBuilder textBuilder = new TextBuilder(textFragmentToContainLink.Page);
        textBuilder.AppendParagraph(textParagraph);

        // The reason we are not using the TextFragment.Rectangle, is that we want the selection to only be on the text, not the brackets
        Rectangle fileAnnotationRectangle = new Rectangle(
            textFragmentToContainLink.Rectangle.LLX,
            textFragmentToContainLink.Rectangle.LLY,
            textFragmentToContainLink.Rectangle.URX,
            textFragmentToContainLink.Rectangle.URY);

        // Create attachment annotation
        FileAttachmentAnnotation fileAttachmentAnnotation = new FileAttachmentAnnotation(
            textFragmentToContainLink.Page,
            fileAnnotationRectangle,
            new FileSpecification(selectHelper.GetDocumentStream(this.Name), $"{this.Name}.pdf", this.Name));

        // Set title & icon for attachment
        fileAttachmentAnnotation.Icon = FileIcon.Paperclip;
        fileAttachmentAnnotation.Opacity = 0;
        fileAttachmentAnnotation.Title = fileAttachmentAnnotation.RichText = this.Name;

        // Add attachment to the page referenced by the TextFragment
        textFragmentToContainLink.Page.Annotations.Add(fileAttachmentAnnotation);

        textFragmentToContainLink.Text = string.Empty;
        textFragmentToContainLink.TextState.Invisible = true;

@mkelley

Thank you for contacting support.

Would you please share SSCCE code so that we may try to reproduce the scenario and investigate it accordingly.

PROBLEM
My application parses a pdf document looking for document names that are bracketed by “{” and “]”. Once I get the list of string tokens found in the pdf, The app loops through them, and replaces “[” and “]” with string.Empty. I also look for “\r\n” and replace that with a " " (SPACE). If a string token is split across 2 lines, Aspose.Pdf TextFragment’s Text property will contain “\r\n” in the string. Now, I assumed, that when I modify the Text property, the words would span to 2 second line if the resulting text would go beyond the page margins. However, what I have noticed, is that it doesn’t wrap the text.

How can I modify my code below in order to accomplish this task of making the TextFragment output to span across 2 lines if trhe text extends beyond the page margin?

INPUT PDF
PublicDoc.pdf (45.9 KB)

OUTPUT PDF
Output.pdf (84.6 KB)

CODE
// Load source document
FileStream sourceFileStream = File.Open(@"…\Dox\PublicDoc.pdf", FileMode.Open);
Document sourceDocument = new Document(sourceFileStream);

        // Perform search using regex and get a list of textfragment instances
        List<TextFragment> foundStringTokens = new List<TextFragment>();
        TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(@"\[([^\[\]]*)\]", new TextSearchOptions(true));
        textFragmentAbsorber.Visit(sourceDocument);
        IEnumerator textFragmentsEnumerator = textFragmentAbsorber.TextFragments.GetEnumerator();
        while (textFragmentsEnumerator?.MoveNext() ?? false) {
            foundStringTokens.Add((TextFragment)textFragmentsEnumerator.Current);
        }
        
        // With each token found, replace the text to remove the [ and ] characters and replace \r\n with a blank, setup the textstate to fake a hyperlink
        foundStringTokens.ForEach(textFragment => {
            // Modify source TextFragment to replace the [ and ] chars from the text (I also remove \r\n because this text is a document name I must look up)
            textFragment.Text = textFragment.Text.Replace("[", string.Empty).Replace("]", string.Empty).Replace("\r\n", " ");

            // Setup text features for output
            textFragment.TextState.FormattingOptions = new TextFormattingOptions(TextFormattingOptions.WordWrapMode.ByWords);
            textFragment.TextState.FontStyle = FontStyles.Italic;
            textFragment.TextState.Underline = true;
            textFragment.TextState.ForegroundColor = Color.Blue;

        });

        // Output source document as output
        sourceDocument.Save(@"..\..\Dox\Output.pdf", SaveFormat.Pdf);

@mkelley

We have tried to append different variations of below code with your snippet but no difference was observed. Therefore, a ticket with ID PDFNET-46219 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

TextReplaceOptions objTextReplaceOptions = new TextReplaceOptions(TextReplaceOptions.ReplaceAdjustment.None)
{
    ReplaceAdjustmentAction = TextReplaceOptions.ReplaceAdjustment.None
};
textFragmentAbsorber.TextReplaceOptions = objTextReplaceOptions;

We are sorry for the inconvenience.