void SetUpUI(void) { setSelectionProc = ASCallbackCreateNotification(AVDocDidSetSelection, &wordPickAVDocDidSetSelection); AVAppRegisterNotification(AVDocDidSetSelectionNSEL, gExtensionID, setSelectionProc, NULL); cbIsEnabled = ASCallbackCreateProto (AVComputeEnabledProc, &IsEnabled); cbIsMarked = ASCallbackCreateProto (AVComputeMarkedProc, &IsMarked); if (ASGetConfiguration(ASAtomFromString("CanEdit"))) SetUpToolButton(); }
/** ** Called to perform WordFinder creation and text extraction on a PDF document ** ** @param pdDoc IN The PDDoc object on which to perform text extraction. ** @param startPg IN The page to start text extraction. ** @param endPg IN The page to end text extraction (inclusive). ** @param toUnicode IN Whether to extract text to Unicode encoding. ** @param pConfig IN Pointer to a WordFinder Configuration Record. ** @param pOutput IN/OUT Pointer to an output FILE stream to which the extracted ** text will be written. ** @return true to indicate text extraction operation a success, false otherwise. */ bool ExtractText(PDDoc pdDoc, ASInt32 startPg, ASInt32 endPg, ASBool toUnicode, PDWordFinderConfig pConfig, FILE* pOutput) { if (startPg < 0 || endPg <0 || startPg > endPg || endPg > PDDocGetNumPages(pdDoc) - 1) { AVAlertNote("Exceeding starting or ending page number limit of current document."); return false; } PDWordFinder pdWordFinder = NULL; DURING pdWordFinder = PDDocCreateWordFinderEx(pdDoc, WF_LATEST_VERSION, toUnicode, pConfig); if (toUnicode) fprintf(pOutput, "%c%c", 0xfe, 0xff); for (int i = startPg; i <= endPg; i++) PDWordFinderEnumWords(pdWordFinder, i, ASCallbackCreateProto(PDWordProc, &WordEnumProc), pOutput); PDWordFinderDestroy(pdWordFinder); E_RETURN(true); HANDLER char buf[256], errmsg[256]; sprintf(buf, "[ExtractText()]Error %d: %s", ErrGetCode(ERRORCODE), ASGetErrorString(ERRORCODE, errmsg, sizeof(errmsg))); AVAlertNote(buf); if (pdWordFinder) PDWordFinderDestroy(pdWordFinder); if( pOutput) fclose(pOutput); return false; END_HANDLER return true; }
static ACCB1 void ACCB2 wordPickAVDocDidSetSelection( AVDoc doc, ASAtom selType, void * selData, void * clientData) { if(!boolPickWords) return; if(selType != ASAtomFromString("Text")) return; wbuffer[0] = 0; PDTextSelect Text = static_cast<PDTextSelect>(selData); /* NOTE Acrobat enumerates text in the order it appears in the PDF file, which is often not the same as the order in which a person would read the text. */ PDTextSelectEnumTextProc enumProc = ASCallbackCreateProto(PDTextSelectEnumTextProc, &PDTextSelectEnumTextProcCB); DURING PDTextSelectEnumTextUCS(Text, enumProc, NULL); HANDLER ASCallbackDestroy(enumProc); return; END_HANDLER ASCallbackDestroy(enumProc); if(!ConvertBufferToUTF8()) return; GlobalData->CurMod.BeginPos = -1; strcpy_s(GlobalData->CurMod.MatchedWord, MAX_SCAN_TEXT_SIZE, buffer); GlobalData->CurMod.IgnoreScanModifierKey = true; NotifyStarDictNewScanWord(1000); }
/** ** Function that provides the initial interface between your plug-in and the application. ** This function provides the callback functions to the application that allow it to ** register the plug-in with the application environment. ** ** Required Plug-in handshaking routine: <b>Do not change its name!</b> ** ** @param handshakeVersion the version this plug-in works with. There are two versions possible, the plug-in version ** and the application version. The application calls the main entry point for this plug-in with its version. ** The main entry point will call this function with the version that is earliest. ** @param handshakeData OUT the data structure used to provide the primary entry points for the plug-in. These ** entry points are used in registering the plug-in with the application and allowing the plug-in to register for ** other plug-in services and offer its own. ** @return true to indicate success, false otherwise (the plug-in will not load). ** */ ACCB1 ASBool ACCB2 PIHandshake(Uns32 handshakeVersion, void *handshakeData) { if (handshakeVersion == HANDSHAKE_V0200) { /* Cast handshakeData to the appropriate type */ PIHandshakeData_V0200 *hsData = (PIHandshakeData_V0200 *)handshakeData; /* Set the name we want to go by */ hsData->extensionName = GetExtensionName(); /* If you export your own HFT, do so in here */ hsData->exportHFTsCallback = ASCallbackCreateProto(PIExportHFTsProcType, &PluginExportHFTs); /* ** If you import plug-in HFTs, replace functionality, and/or want to register for notifications before ** the user has a chance to do anything, do so in here. */ hsData->importReplaceAndRegisterCallback = ASCallbackCreateProto(PIImportReplaceAndRegisterProcType, &PluginImportReplaceAndRegister); /* Perform your plug-in's initialization in here */ hsData->initCallback = ASCallbackCreateProto(PIInitProcType, &PluginInit); /* Perform any memory freeing or state saving on "quit" in here */ hsData->unloadCallback = ASCallbackCreateProto(PIUnloadProcType, &PluginUnload); /* All done */ return true; } /* Each time the handshake version changes, add a new "else if" branch */ /* ** If we reach here, then we were passed a handshake version number we don't know about. ** This shouldn't ever happen since our main() routine chose the version number. */ AVAlertNote("Error in hand shake"); return false; }
static void SetUpToolButton(void) { void *WordPickIcon = GetToolButtonIcon(); AVToolBar toolBar = AVAppGetToolBar(); AVToolButton separator = AVToolBarGetButtonByName (toolBar, ASAtomFromString("Hand")); wordPickToolButton = AVToolButtonNew (ASAtomFromString("ADBE:WordPick"), WordPickIcon, true, false); cbActivateTool = ASCallbackCreateProto(AVExecuteProc, &ActivateWordPickTool); AVToolButtonSetExecuteProc (wordPickToolButton, cbActivateTool, NULL); /* pdPermCopy - permissions required to extract text from a document. In Document Properties this property is named "Content Copy or Extration". To see Document Restrictions go to Main menu -> File -> Document properties -> Security tab. If we ignore permissions here (use 0), PDTextSelectEnumTextUCS throws an exception. We will not get text anyway. */ AVToolButtonSetComputeEnabledProc (wordPickToolButton, cbIsEnabled, (void *)pdPermCopy); AVToolButtonSetComputeMarkedProc (wordPickToolButton, cbIsMarked, NULL); AVToolButtonSetHelpText (wordPickToolButton, "Toggle StarDict"); AVToolBarAddButton(toolBar, wordPickToolButton, true, separator); }