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;