You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Evan Stansel <Ev...@Evisions.com> on 2019/10/15 16:42:05 UTC

Assistance upgrading PDFBox from v1.8 to v2.0

Hello,

I am working with an automation script that compares PDF files using PDFBox. The script has been written for v1.8. We'd like to upgrade the script to v2.0. I attempted to do this myself using the migration guide but ran into issues along the way. I've attached the script above (just a function). Hoping to get some input as to what primary changes need to happen. As a side note, this is in Javascript and this is written to be used in TestComplete (full set of scripts can be found here: https://support.smartbear.com/articles/testcomplete/testing-pdf-files-with-testcomplete).

Thank you,
Evan Stansel


  // 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);
    }
  }
}

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;
}

Re: Assistance upgrading PDFBox from v1.8 to v2.0

Posted by Tilman Hausherr <TH...@t-online.de>.
I don't want to learn testcomplete, but assuming this is about 
converting PDFs to images, see here:
https://stackoverflow.com/questions/23326562/apache-pdfbox-convert-pdf-to-images/23327024#23327024

So what you have to do is to change these lines into your language:

|newPDFRenderer(document); ||BufferedImagebim =pdfRenderer.renderImageWithDPI(page,300,ImageType.RGB);|
||


so the first line would probably be

var pdfRenderer = JavaClasses.org.apache.pdfbox.rendering.PDFRenderer.newInstance(fileName);


Tilman

Am 15.10.2019 um 18:42 schrieb Evan Stansel:
> 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;
> }