Here we highlight a set of words in a source document by drawing a rectangle around each one.
[C#]
string theSrc =
Server.MapPath("Acrobat.pdf");
string theDst = Server.MapPath("HighlightedText.pdf");
string searchString = "Acrobat";
using (Doc theDoc = new Doc()) {
theDoc.Read(theSrc);
TextOperation op = new TextOperation(theDoc);
op.PageContents.AddPages();
string text = op.GetText();
int pos = 0;
while (true) {
pos = text.IndexOf(searchString, pos, StringComparison.CurrentCultureIgnoreCase);
if (pos < 0)
break;
IList<TextFragment> theSelection = op.Select(pos, searchString.Length);
IList<TextGroup> theGroups = op.Group(theSelection);
foreach (TextGroup theGroup in theGroups) {
theDoc.Rect.String = theGroup.Rect.String;
theDoc.FrameRect();
}
pos += searchString.Length;
}
theDoc.Save(theDst);
}
[Visual
Basic]
Dim theSrc As String = Server.MapPath("Acrobat.pdf")
Dim theDst As String = Server.MapPath("HighlightedText.pdf")
Dim searchString As String = "Acrobat"
Using theDoc As New Doc()
theDoc.Read(theSrc)
Dim op As New TextOperation(theDoc)
op.PageContents.AddPages()
Dim text As String = op.GetText()
Dim pos As Integer = 0
While True
pos = text.IndexOf(searchString, pos, StringComparison.CurrentCultureIgnoreCase)
If pos < 0 Then
Exit While
End If
Dim theSelection As IList(Of TextFragment) = op.[Select](pos, searchString.Length)
Dim theGroups As IList(Of TextGroup) = op.Group(theSelection)
For Each theGroup As TextGroup In theGroups
theDoc.Rect.[String] = theGroup.Rect.[String]
theDoc.FrameRect()
Next
pos += searchString.Length
End While
theDoc.Save(theDst)
End Using
|
|
|