Forum Discussion

AMITESH's avatar
AMITESH
Contributor
10 years ago

Facing Problem with 'Testing PDF Files With TestComplete'

Hi,

 

I referred article 'https://support.smartbear.com/articles/testcomplete/testing-pdf-files-with-testcomplete/' to compare two pdf files.

 

from here I used:

function compareDocsAsImg(pdfFile_1, pdfFile_2, maskImg)
{
  var imgFile_1, imgFile_2, docObj_1, docObj_2, totalPages_1, totalPages_2;

  // Specify the fully-qualified name of the temporary image files
  imgFile_1 = "C:\\Temp\\page_doc_1.png";
  imgFile_2 = "C:\\Temp\\page_doc_2.png";

  // Load specified documents
  docObj_1 = JavaClasses.org_apache_pdfbox_pdmodel.PDDocument.load_3(pdfFile_1);
  docObj_2 = JavaClasses.org_apache_pdfbox_pdmodel.PDDocument.load_3(pdfFile_2);

  // Get the total number of pages in both documents
  totalPages_1 = docObj_1.getNumberOfPages();
  totalPages_2 = docObj_2.getNumberOfPages();

  // Check whether the documents contain the same number of the pages
  if (totalPages_1 != totalPages_2)
  {
    Log.Message("The documents contain different number of pages.");
  } else
  {
    for (i = 0; i < totalPages_1; i++)
    {
      // Call a routine that converts the specified page to an image
      pic_1 = convertPageToPicture(docObj_1, i, imgFile_1);
      pic_2 = convertPageToPicture(docObj_2, i, imgFile_2);

      // Compare two images
      if (!pic_1.Compare(pic_2, false, 5, false, 5, maskImg))
      {
        // If the images are different...
        // Post image differences to the log
        Log.Picture(pic_1.Difference(pic_2, false, 5, false, 5, maskImg));

        // Post a warning message
        Log.Warning("Pages " + aqConvert.IntToStr(i+1) + " are different. Documents are different.");

        // Break the loop
        break;
      } else
      {
        // Post a message that the pages are equal
        Log.Message("Pages " + aqConvert.IntToStr(i+1) + " are equal.")
      }
      // Delete the temporary image files
      aqFile.Delete(imgFile_1);
      aqFile.Delete(imgFile_2);
    }
  }
}

on lines

// Call a routine that converts the specified page to an image
      pic_1 = convertPageToPicture(docObj_1, i, imgFile_1);
      pic_2 = convertPageToPicture(docObj_2, i, imgFile_2);

pointing to subroutine:

function convertPageToPicture(docObj, pageIndex, fileName)
{
  var pageObj, imgBuffer, imgFile, imgFormat, pictureObj;

  // Get the desired page
  pageObj = getPage(docObj, pageIndex);

  // Convert the page to image data
  imgBuffer = pageObj.convertToImage();

  // Create a new file to save
  imgFile = JavaClasses.java_io.File.newInstance(fileName);

  // Get the image format from the name
  imgFormat = aqString.SubString(fileName, aqString.GetLength(fileName)-3, 3);

  // Save the image to the created file
  JavaClasses.javax_imageio.ImageIO.write(imgBuffer, imgFormat, imgFile);

  // Create a Picture object
  pictureObj = Utils.Picture;

  // Load the image as a picture
  pictureObj.LoadFromFile(fileName);

  // Return the picture object
  return pictureObj; 
}

I am getting error:

Please find the attached snapshot of the error message.

 

Can anyone please help me to resolve this Issue.

 

Thanks,

Amitesh

1 Reply

  •  

    In '

    function convertPageToPicture(docObj, pageIndex, fileName)
    {

    '

    The issue is on line:

    // Convert the page to image data
    imgBuffer = pageObj.convertToImage();