Selection is extended with PDTextSelectGetBoundingRect
Hello,
I am trying to create highlight annotation for the selected text over multiple line.
For example, see in the below image:

This is my selected text. Now when I apply the annotation to this text, it looks like below:

That means selection is getting extended while applying the annotation.
Here is my code:
I have used
ACCB1 void ACCB2 createRequirement(char *sectionName) {
AVDoc avDoc = AVAppGetActiveDoc();
if (avDoc == NULL) {
AVAlertNote("Please open the document first.");
return;
}
PDDoc pdDoc = AVDocGetPDDoc(avDoc);
ASFixedRect boundingRect;
PDPage page = NULL;
PDAnnot annot, hilightAnnot;
LPCWSTR secName = convertCharArrayToLPCWSTR(sectionName);
LPCWSTR arg = L"value";
DWORD size = 1024;
LPCWSTR filePath = convertCharArrayToLPCWSTR(iniFilePath);
PDTextSelect pdtext =static_cast<PDTextSelect>(AVDocGetSelection(avDoc));
char reqId[2048];
char ident[2048];
int numPages = PDDocGetNumPages(pdDoc);
//Check if text is not selected
if (pdtext == NULL) {
AVAlertNote("Please select the text first.");
if (sectionName != NULL) {
WritePrivateProfileString(_T(sectionName), _T("selectedText"), _T(""), _T(iniFilePath));
}
return;
}
if (sectionName != NULL) {
//Create requirement from Reqtify
GetPrivateProfileString(sectionName, "value", NULL, reqId, size, iniFilePath);
strcpy(ident,reqId);
}
else {
//Input box to create requirement manually
createInputDialogBox(0, NULL, L"Requirement creation", L"Requirement ID:", L"REQ");
if (TheText[0] == 0) {
return;
}
wstring reqIdent(TheText);
string str(reqIdent.begin(), reqIdent.end());
strcpy(ident, str.c_str());
}
//Get selected text
AVDocCopySelection(avDoc);
wchar_t* selectedText = getClipboardText();
if (selectedText == NULL) {
AVAlertNote("Could not copy to clipboard.");
return;
}
// Get the bounding box for the selection
PDTextSelectGetBoundingRect(pdtext, &boundingRect);
//Create a PDPage object
AVPageView currentPageView = AVDocGetPageView(avDoc);
ASInt32 pageNum = AVPageViewGetPageNum(currentPageView);
page = PDDocAcquirePage(pdDoc, pageNum);
PDColorValueRec yellow;
yellow.space = PDDeviceRGB;
yellow.value[0] = ASInt32ToFixed(1);
yellow.value[1] = ASInt32ToFixed(1);
yellow.value[2] = ASInt32ToFixed(0);
//Use the bbox to create a highight annotation QuadPoints
CosObj ArrayObj, RecObj;
CosDoc cd = PDDocGetCosDoc(pdDoc);
CosObj cosPage = PDPageGetCosObj(page);
ArrayObj = CosNewArray(cd, false, 8);
CosArrayPut(ArrayObj, 0, CosNewFixed(cd, false, boundingRect.right));
CosArrayPut(ArrayObj, 1, CosNewFixed(cd, false, boundingRect.bottom));
CosArrayPut(ArrayObj, 2, CosNewFixed(cd, false, boundingRect.left));
CosArrayPut(ArrayObj, 3, CosNewFixed(cd, false, boundingRect.bottom));
CosArrayPut(ArrayObj, 4, CosNewFixed(cd, false, boundingRect.right));
CosArrayPut(ArrayObj, 5, CosNewFixed(cd, false, boundingRect.top));
CosArrayPut(ArrayObj, 6, CosNewFixed(cd, false, boundingRect.left));
CosArrayPut(ArrayObj, 7, CosNewFixed(cd, false, boundingRect.top));
// for the Rect. Highlight annotations don't require, but API call to create annotation requires this key
RecObj = CosNewArray(cd, false, 4);
CosArrayPut(RecObj, 0, CosNewFixed(cd, false, boundingRect.left));
CosArrayPut(RecObj, 1, CosNewFixed(cd, false, boundingRect.right));
CosArrayPut(RecObj, 2, CosNewFixed(cd, false, boundingRect.bottom));
CosArrayPut(RecObj, 3, CosNewFixed(cd, false, boundingRect.top));
CosObj cosDict = CosNewDict(cd, true, 4);
CosDictPutKeyString(cosDict, "Subtype", CosNewNameFromString(cd, false, "Highlight"));
CosDictPutKeyString(cosDict, "QuadPoints", ArrayObj);
CosDictPutKeyString(cosDict, "Rect", RecObj);
annot = PDAnnotFromCosObj(cosDict);
hilightAnnot = CastToPDTextAnnot(annot);
//Open the annotation, set the text, and add it to a page
PDTextAnnotSetOpen(hilightAnnot, true);
PDTextAnnotSetContents(hilightAnnot, ident, strlen(ident));
//Fix 197842 starts
//Adds logged in Username as a Annotation title
char username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
GetUserName(username, &username_len);
PDAnnotSetTitle(hilightAnnot, username, strlen(username));
//Fix 197842 ends
PDPageAddAnnot(page, -2, annot);
PDPageNotifyContentsDidChange(page);
PDAnnotSetColor(annot, &yellow);
AVPageViewDrawNow(currentPageView);
if (ASAtomFromString("Highlight") == PDAnnotGetSubtype(annot) && sectionName != NULL)
{
std::wstring text;
std::wstring str;
std::wstringstream stringStream;
stringStream << selectedText;
while (stringStream >> text)
{
str.append(text);
str.append(L" ");
}
trim(str);
LPCWSTR secName = convertCharArrayToLPCWSTR(sectionName);
LPCWSTR arg = L"selectedText";
LPCWSTR reqText = str.c_str();
LPCWSTR filePath = convertCharArrayToLPCWSTR(iniFilePath);
WritePrivateProfileStringW(secName, arg, reqText, filePath);
}
PDPageRelease(page);
}
I have used PDTextSelect to select the text and PDTextSelectGetBoundingRect to get the bounding rect of selected text.
Can you please tell me what is missing or wrong in this code?
Thank you in advance!!
