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

Free Support Forum - aspose.com

Border around TextParagraph

I'm using the latest version of aspose.pdf, using the new API model (Document object).

Looking for options to put a border around a TextParagraph. Looks like there is no border option, so I'm not sure if there is a way to use it, or if there is some way to draw a box around the text to simulate a border.

I played around with PolylineAnnotation and PDFContentEditor, but either I could not figure out the required parameters, or they didn't work for my situation.

Please help, I need to know what object type to use and how to use it.

Hi Michael,


Thanks for contacting support.

Aspose.Pdf for .NET supports the feature to search individual TextFragment and with the help of CreatePolygon(…) method of PdfContentEditor class, you can draw rectangle around each fragment. However for text paragraph, you may consider using some regular expression to determine the paragraph break and draw rectangle around it. Please take a look over following code snippet.

[C#]

var document = new Document(@“C:\pdftest\36886.pdf”);<o:p></o:p>

//create TextAbsorber object to find all the phrases matching the regular expression

TextFragmentAbsorber textAbsorber = new TextFragmentAbsorber(@"[\S]+");

TextSearchOptions textSearchOptions = new TextSearchOptions(true);

textAbsorber.TextSearchOptions = textSearchOptions;

document.Pages.Accept(textAbsorber);

var editor = new PdfContentEditor(document);

foreach (TextFragment textFragment in textAbsorber.TextFragments)

{

foreach (TextSegment textSegment in textFragment.Segments)

{

DrawBox(editor, textFragment.Page.Number, textSegment, System.Drawing.Color.Red);

}

}

document.Save(@"C:\pdftest\36886-edited.pdf");


private static void DrawBox(PdfContentEditor editor, int page, TextSegment segment, System.Drawing.Color color)

{

var lineInfo = new LineInfo();

lineInfo.VerticeCoordinate = new[] {

(float)segment.Rectangle.LLX, (float)segment.Rectangle.LLY,

(float)segment.Rectangle.LLX, (float)segment.Rectangle.URY,

(float)segment.Rectangle.URX, (float)segment.Rectangle.URY,

(float)segment.Rectangle.URX, (float)segment.Rectangle.LLY

};

lineInfo.Visibility = true;

lineInfo.LineColor = color;

editor.CreatePolygon(lineInfo, page, new System.Drawing.Rectangle(0, 0, 0, 0), null);

}<o:p></o:p>

Thank you for the code sample. I’m trying to use this, but nothing is displaying on the page. Does this still work an output stream? That’s the only thing I can think of that is different.


Also, how could I do a border around say two sides? Is there a separate way to draw a line that would be similar?

Thanks,
-Mike

I’m doing basically the following, and it is not working for me. No border is ever written.


I’m using the same Rectangle coordinates as a text paragraph on my page.
[C#]
Document pdf = new Document();
PdfContentEditor _pdfContentEditor = new PdfContentEditor();
_pdfContentEditor.BindPdf(_pdf);
_pdf.Pages.Add();
var lineInfo = new LineInfo();
Rectangle rectangle = GetRectangle();
lineInfo.VerticeCoordinate = new[] {
(float)rectangle.LLX, (float)rectangle.LLY,
(float)rectangle.LLX, (float)rectangle.URY,
(float)rectangle.URX, (float)rectangle.URY,
(float)rectangle.URX, (float)rectangle.LLY
};
lineInfo.Visibility = true;
lineInfo.LineColor = System.Drawing.Color.Black;
_contentEditor.CreatePolygon(lineInfo, _pageNumber, new System.Drawing.Rectangle(0, 0, 0, 0), null);
_pdf.Save(stream);


acordmike:
Also, how could I do a border around say two sides? Is there a separate way to draw a line that would be similar?
Hi Mike,

Can you please share some details regarding this requirement. If possible, please share some sample document or image file which can help us in understanding this requirement.

Hi Mike,


For above stated issue, the problem might be related to input PDF file which you are using. Can you please share the resource file so that we can test the scenario at our end. We are really sorry for this inconvenience.