Back to Blog

    Developer Blog

    How to get the text object from a position in a pdf file and change the content of the text object?

    Foxit PDF SDK 5.3

    To get text object from a position in a PDF file using Foxit PDF SDK, follow the steps outlined below:

    1. Read the PDF file.
    2. Load the page and get the page objects in that page.
    3. Use FSPDF_PageObjects_GetObjectAtPos to get the text object in a certain position. Note: use page object get rectangle to see the position of a text object.
    4. Change the string in the pdf file and save the document.Here is a simple example:
    FS_RESULT ChangeTextObjectContent()
    
    {
    
                   string strFile = "AboutFoxit.pdf";
    
                   FSCRT_FILE file = FSDK_OpenFile(strFile.c_str(), "rb"); //1. Read the PDF file.
    
     
    
                   FSCRT_DOCUMENT doc;
    
                   FS_RESULT ret  = FSPDF_Doc_StartLoad(file, NULL, &Doc, NULL); //2. Load the page.
    
                   if (FSCRT_ERRCODE_SUCCESS != ret)
    
                   {
    
                                  printf("Failed to load PDF documentn");
    
                                  return ret;
    
                   }
    
     
    
                   FSCRT_PAGE page = NULL;
    
                   ret = LoadPage(doc, 0, &page);
    
                   if (FSCRT_ERRCODE_SUCCESS != ret)
    
                   {
    
                                  printf("Failed to load a given page.n");
    
                                  return ret;
    
                   }
    
     
    
                   FSPDF_PAGEOBJECTS pageObjs = NULL;
    
                   ret = FSPDF_Page_GetPageObjects(page, &pageObjs); //2. gets the page objects
    
                   FSPDF_PAGEOBJECT textobject= NULL;
    
                   ret = FSPDF_PageObjects_GetObjectAtPos(page,pageObjs,FSPDF_PAGEOBJECT_TEXT, 92.0, 762.0, 10.00, &textobject); //3. Uses FSPDF_PageObjects_getObjectAtPos
    
                   FSCRT_BSTR unicodeStr;
    
                   FSCRT_BStr_Init(&unicodeStr);
    
                   unicodeStr.str = "Foxit Test";
    
                   unicodeStr.len = 10;
    
                   ret = FSPDF_TextObject_SetUnicodeString(page, textobject,&unicodeStr); //4. Change the string.
    
                   ret = FSPDF_PageObjects_GenerateContents(page, pageObjs);
    
                   const char strEncryptFile[] = "After_revise.pdf";
    
                   FS_BOOL bRet = FSDK_SavePDFFile(doc, strEncryptFile,  FSPDF_SAVEFLAG_INCREMENTAL);          
    
                   FSPDF_Doc_Close(doc);
    
                   return ret;
    
    }
    
     
    
    FS_RESULT LoadPage(FSCRT_DOCUMENT doc, FS_INT32 index, FSCRT_PAGE *page ) //used in the code above
    
    {
    
                   FS_RESULT ret =  FSPDF_Doc_GetPage(doc, index, page);
    
                   if(FSCRT_ERRCODE_SUCCESS != ret)
    
                   {
    
                                  printf("Failed to get the page %dn", index+1);
    
                                  return ret;
    
                   }
    
     
    
                   FSCRT_PROGRESS progress;
    
                   ret = FSPDF_Page_StartParse(*page, FSPDF_PAGEPARSEFLAG_NORMAL, &progress);
    
                   if (FSCRT_ERRCODE_SUCCESS != ret)
    
                   {
    
                                  printf("Failed to parse pages.n");
    
                                  return ret;
    
                   }
    
     
    
                   //Continue to parse page, without pausing.
    
                   ret = FSCRT_Progress_Continue(progress, NULL);
    
                   if (FSCRT_ERRCODE_FINISHED != ret)
    
                   {
    
                                  printf("Failed to finish parsing progress! n", ret);
    
                   } else {
    
                                  ret = FSCRT_ERRCODE_SUCCESS;
    
                                  printf("Succeed to Parse a given PDF page! n");
    
                   }
    
     
    
                   FSCRT_Progress_Release(progress);
    
                   return ret;
    
    }

    Note: This article refers to a deprecated version of a Foxit Product. If you are still using Foxit PDF SDK 5.3 or older, please refer to your download package documents for Developer Guide and API Reference.

    Get a trial version of the new Foxit PDF SDK and see our latest generation SDK’s brand new features!