Barcodes infos inside PDF have strange coordinates

Hi.
I’m reading barcodes inside a pdf file (see attachment)
aspose_pdf_test.pdf (61,1 KB)

I’m using these versions of aspose library :

<aspose-barcode.version>20.7</aspose-barcode.version>
<aspose-pdf.version>23.3</aspose-pdf.version>

And this is my Java class example :

public class AsposeTest {

  public static void main(String[] args){
    String file = "C:\\tmp\\aspose_pdf_test.pdf";

    var start = Instant.now();

    // Load a PDF document
    com.aspose.pdf.Document pdfDoc = new com.aspose.pdf.Document(file);

    // Proceed all PDF pages starting from page 1
    for (int i = 1; i <= pdfDoc.getPages().size(); ++i) {
      var startPage = Instant.now();

      // Render PDF page to the stream
      Page page = pdfDoc.getPages().get_Item(i);

      System.out.println("************* Page nr : " + i);

      PageInfo pageInfo = page.getPageInfo();

      System.out.println(
          "pageInfo - getHeight : " + pageInfo.getHeight()
              + " - getWidth : " + pageInfo.getWidth()
              + " - isLandscape : " + pageInfo.isLandscape());

      Rectangle pageRect = page.getPageRect(true);

      System.out.println(
          "pageRect - getHeight : " + pageRect.getHeight()
              + " - getWidth : " + pageRect.getWidth());
      System.out.println();

      byte[] ms = page.convertToPNGMemoryStream();
      InputStream stream = new ByteArrayInputStream(ms);

      // Recognize barcodes from the page stream
      BarCodeReader reader = new BarCodeReader(stream);

      // Show results
      for (BarCodeResult result : reader.readBarCodes()) {
        Quadrangle quadrangle = result.getRegion().getQuadrangle();
        java.awt.Rectangle boundingRectangle = quadrangle.getBoundingRectangle();

        System.out.println(
            "boundingRectangle - getHeight : " + boundingRectangle.getHeight()
                + " - getWidth : " + boundingRectangle.getWidth());

        System.out.println(result);
        System.out.println(quadrangle);
        System.out.println();
      }

      var stopPage = Instant.now();
      var diffPage = ChronoUnit.SECONDS.between(startPage, stopPage);
      System.out.println("Page nr " + i + " - Time to read barcodes : " + diffPage + " seconds");
      System.out.println(("-------------------------------"));
      System.out.println();
    }

    var stop = Instant.now();
    var diff = ChronoUnit.SECONDS.between(start, stop);
    System.out.println("Total time to read pdf barcodes : " + diff + " seconds");
  }
}

I obtain this result :

************* Page nr : 1
pageInfo - getHeight : 842.0 - getWidth : 595.0 - isLandscape : false
pageRect - getHeight : 842.0 - getWidth : 596.0

boundingRectangle - getHeight : 626.0 - getWidth : 626.0
CodeType = QR, CodeText = "https://www.aspose.com/", Confidence = 100, ReadingQuality = 100
LeftTop = java.awt.Point[x=358,y=755], RightTop = java.awt.Point[x=983,y=755], RightBottom = java.awt.Point[x=982,y=1380], LeftBottom = java.awt.Point[x=358,y=1380]

boundingRectangle - getHeight : 164.0 - getWidth : 1237.0
CodeType = Code128, CodeText = "https://www.aspose.com/", Confidence = 80, ReadingQuality = 94.8224029541016
LeftTop = java.awt.Point[x=938,y=2303], RightTop = java.awt.Point[x=2174,y=2303], RightBottom = java.awt.Point[x=2174,y=2466], LeftBottom = java.awt.Point[x=938,y=2466]

Page nr 1 - Time to read barcodes : 15 seconds
-------------------------------

************* Page nr : 2
pageInfo - getHeight : 842.0 - getWidth : 595.0 - isLandscape : false
pageRect - getHeight : 596.0 - getWidth : 842.0

boundingRectangle - getHeight : 1238.0 - getWidth : 164.0
CodeType = Code128, CodeText = "https://www.aspose.com/", Confidence = 80, ReadingQuality = 94.9283905029297
LeftTop = java.awt.Point[x=1206,y=937], RightTop = java.awt.Point[x=1206,y=2174], RightBottom = java.awt.Point[x=1043,y=2174], LeftBottom = java.awt.Point[x=1043,y=937]

boundingRectangle - getHeight : 626.0 - getWidth : 627.0
CodeType = QR, CodeText = "https://www.aspose.com/", Confidence = 100, ReadingQuality = 100
LeftTop = java.awt.Point[x=2755,y=357], RightTop = java.awt.Point[x=2755,y=982], RightBottom = java.awt.Point[x=2129,y=981], LeftBottom = java.awt.Point[x=2130,y=357]

Page nr 2 - Time to read barcodes : 7 seconds
-------------------------------

************* Page nr : 3
pageInfo - getHeight : 842.0 - getWidth : 595.0 - isLandscape : false
pageRect - getHeight : 842.0000000000001 - getWidth : 596.0000000000001

boundingRectangle - getHeight : 165.0 - getWidth : 1238.0
CodeType = Code128, CodeText = "https://www.aspose.com/", Confidence = 80, ReadingQuality = 94.9281463623047
LeftTop = java.awt.Point[x=1547,y=1206], RightTop = java.awt.Point[x=310,y=1206], RightBottom = java.awt.Point[x=310,y=1042], LeftBottom = java.awt.Point[x=1547,y=1042]

boundingRectangle - getHeight : 627.0 - getWidth : 626.0
CodeType = QR, CodeText = "https://www.aspose.com/", Confidence = 100, ReadingQuality = 100
LeftTop = java.awt.Point[x=2128,y=2755], RightTop = java.awt.Point[x=1503,y=2755], RightBottom = java.awt.Point[x=1503,y=2129], LeftBottom = java.awt.Point[x=2128,y=2130]

Page nr 3 - Time to read barcodes : 6 seconds
-------------------------------

Total time to read pdf barcodes : 30 seconds

I have some questions :

  1. Why ‘isLandscape’ is always false? Second page orientation is different
  2. Why LeftTop, RightTop (and so on) are bigger than page width and height? Different unit measure?
  3. Why is so slow? 30 seconds to parse a 3 pages pdf?
  4. Am I doing it right or is there a better way (I mean fast way) to read barcodes?

With result.getRegion().getQuadrangle() then I need to draw a rectangle around barcodes on a browser pdf preview.

Thanks and sorry for any spelling mistakes. English is not my native language

@Luca_Zoffoli

Please note that some page properties are only given for the usage during PDF generation. Which means they would only work at the time of generating new PDF. In case of existing PDF documents, they are not supposed to be used. Therefore, in order to get correct height and width of the PDF page, you can use below code:

double originalWidth = page.getPageRect(true).getWidth();
double originalHeight = page.getPageRect(true).getHeight();

Furthermore, you can draw rectangle on PDF pages using below sample code snippet which you can use by modifying respective values as per your requirements:

About the performance, you are using two different Aspose APIs and it needs to be investigated which API is causing delay in processing. Would you kindly try to use the latest available versions of all APIs and test your program in release mode? If issue still persists, please let us know.

We really hope that our response convers all the concerns and the questions you had. Please feel free to let us know in case you face any issues.

@asad.ali

with these versions is a little faster :

    <aspose-barcode.version>21.6</aspose-barcode.version>
    <aspose-pdf.version>23.10</aspose-pdf.version>

Anyway I need to draw a rectangle around barcodes externally when I made a preview on a browser page.

************* Page nr : 1
pageInfo - getHeight : 842.0 - getWidth : 595.0 - isLandscape : false
pageRect - getHeight : 842.0 - getWidth : 596.0

boundingRectangle - getHeight : 626.0 - getWidth : 626.0
CodeType = QR, CodeText = "https://www.aspose.com/", Confidence = 100, ReadingQuality = 100
LeftTop = java.awt.Point[x=358,y=755], RightTop = java.awt.Point[x=983,y=755], RightBottom = java.awt.Point[x=982,y=1380], LeftBottom = java.awt.Point[x=358,y=1380]

So I need these coordinates (LeftBottom = java.awt.Point[x=358,y=1380]) are compatible with these page sizes (pageInfo - getHeight : 842.0 - getWidth : 595.0)

How to do that? I need a unit conversion class?

Thanks

@Luca_Zoffoli

We need to perform some investigation about this case. Therefore, we have generated an investigation ticket as PDFJAVA-43401 in our issue tracking system for the purpose. We will look into its details and as soon as the ticket is resolved, we will inform you. Please spare us some time.