How to save or capture highlighted PDF text using PDF viewer. Basically is there way to do color coded Highlighted text. Please advise. Thanks
PDF Viewer Highlight text in different colors.
Answers approved by DevExpress Support
Hello Haruun,
Thank you for your clarification. You can use the PdfDocumentProcessor.FindText method to find text in the PDF document. Then, call the PdfViewerControl.ConvertDocumentPositionToPixel method to get coordinates of a point corresponding to a document position and call the PdfGraphics.FillPolygon method to highlight text in the PDF Document.
Thanks,
Elliot
PS. Please note that the PDFDocumentProcessor control is located in two packages: Universal Subscription and .NET Document Automation. So, it is necessary to have a license for one of these subscriptions to be able to use PDFDocumentProcessor in the application.
Hello,
The cause of this behavior is that multiline text is surrounded by several rectangles. Thus, the PdfTextSearchResults.Rectangles property contains more than one item. To overcome the issue, modify the code Mariya provided to calculate the correct location of the text. Modify the DrawGraphics method in Mariya's project for this purpose.
C# static void DrawGraphics(PdfGraphics graph, PdfTextSearchResults result, SolidBrush brush)
{
..
Should you have any questions or concerns while implementing this approach, let me know.
Can provide example how to modify the drawing using your method. The way I did works, but I am interested to see how your method will work. Thanks for the quick feedback.
A possible implementation of the DrawGraphics method to highlight multiline text may look like this:
C#static void DrawGraphics(PdfGraphics graph, PdfTextSearchResults result, SolidBrush brush)
{
for (int i = 0; i < result.Rectangles.Count; i++)
{
RectangleF rect = new RectangleF(new PointF((float)result.Rectangles[i].Left, (float)result.Page.CropBox.Top - (float)result.Rectangles[i].Top),
new SizeF((float)result.Rectangles[i].Width, (float)result.Rectangles[i].Height));
graph.FillRectangle(brush, rect);
}
graph.AddToPageForeground(result.Page, 72, 72);
}
I have modified Mariya's previous project to demonstrate the approach in action. You can find the project in the attachment.
I hope you will find my sample useful.
Hello Haruun,
Would you please clarify the task you trying to accomplish? Do you want to highlight text in the PDFViewer control or save highlighted text in the PDF document?
Thanks.
Elliot
I would like to accomplish both. I can highlighted, but how do I change the color or supply different color for the highlighted text. Then I would like to save it. I can open window asking user to save it, but I am not sure if the highlighted text will stay highlighted. Thanks for the feedback.