Saving small PDF with graph shape is causing system to freeze

CIB letter.pdf (8.7 KB)

Hello,
The following issue is happening with this specific file only:
When we add a shape to a page graph, saving the file will freeze the process and make the CPU usage 100%.

@evrrnd

What type of edits you are performing on this PDF? Could you please share a sample code snippet so that the case can be tested accordingly and addressed as well.

Hello Asad,

Thank you for your prompt response!
We are adding a shape to the PDF page.
Here is a code sample (Aspose JAVA 19.3)

Document pdf = new Document("CIB letter.pdf");

Page page = pdf.getPages().get_Item(1);
Graph graph = new Graph((float) page.getMediaBox().getWidth(), (float) page.getMediaBox().getHeight());
graph.setLeft(0.0D);
graph.setTop(0.0D);
graph.setZIndex(0);
page.getPageInfo().setMargin(new MarginInfo(0.0D, 0.0D, 0.0D, 0.0D));
page.getParagraphs().add(graph);

com.aspose.pdf.drawing.Rectangle rectangle = new com.aspose.pdf.drawing.Rectangle((float) 0, (float) 0, (float) 20, (float) 20);
rectangle.getGraphInfo().setFillColor(Color.getGreen());
rectangle.getGraphInfo().setColor(Color.getGreen());
graph.getShapes().add(rectangle);

pdf.save("result.pdf");

@evrrnd

We have been able to observe the similar issue in our environment while testing the scenario with 21.3v of the API. Hence, an issue as PDFJAVA-40316 has been logged in our issue tracking system for further investigation. We will further look into its details and keep you posted with the status of its correction. Please be patient and give us some time.

We are sorry for the inconvenience.

1 Like

Hello Asad,

Thanks again for your support.
We noticed the issue was caused by Graph constructor params (page width and page height).
If we change these parameters to pageWidth - 1 and pageHeight - 1, the process won’t freeze but the drawing will be added in wrong place.

@evrrnd

Can you please share the generated and expected output PDF? We will further proceed to assist you accordingly.

Hello,

It should be the same PDF with a rectangle shape on left bottom.

@evrrnd

We further checked that the page was rotated. So we again tried to add graph shape by resetting the page rotation but it did not work: The shape was added at the wrong position.

Document pdf = new Document(dataDir + "CIB letter.pdf");

Page page = pdf.getPages().get_Item(1);
com.aspose.pdf.drawing.Graph graph = new com.aspose.pdf.drawing.Graph((float) page.getMediaBox().getWidth() - 1, (float) page.getMediaBox().getHeight() - 1);
graph.setLeft(0.0D);
graph.setTop(0.0D);
graph.setZIndex(0);
int pageRotation = page.getRotate();
page.setRotate(1);
page.getPageInfo().setMargin(new MarginInfo(0.0D, 0.0D, 0.0D, 0.0D));
page.getParagraphs().add(graph);

com.aspose.pdf.drawing.Rectangle rectangle = new com.aspose.pdf.drawing.Rectangle((float) 0, (float) 0, (float) 20, (float) 20);
rectangle.getGraphInfo().setFillColor(Color.getGreen());
rectangle.getGraphInfo().setColor(Color.getGreen());
graph.getShapes().add(rectangle);
page.setRotate(pageRotation);
pdf.save(dataDir  + "result.pdf");

The information of the logged ticket has been updated accordingly. We will inform you as soon as it is resolved. Please give us some time.

We apologize for the inconvenience.

Hello,

Any updates?

Thanks!

@evrrnd

We are afraid that earlier logged ticket is not yet resolved. We will definitely investigate and resolve it on a first come first serve basis. You will be informed as soon as we make some certain progress towards it resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hello Asad,

Any updates regarding this? Issue status has changed to “Resolved” in Issues Status but we found no info. Please advise.

Regards

@evrrnd

Your document is rotated on 270 degrees clockwise. That is why coordinates x and y for the current page changed (look at coordinates.png). And so actual page Width value is bigger than Height value. That is why when you tried to create Graph object and used actual height instead of width - that graphic object was bigger than actual page size.

In this case algorithm is thinking that content cannot be added on current page and creating new page, but content cannot be placed again because it is too large, and the same continues endlessly for every new page.

The following code snippet will take into account rotated pages on 270 degrees:

        Document pdf = new Document(dataDir+"CIB letter.pdf");
        Page page = pdf.getPages().get_Item(1);
        int pageRotation = page.getRotate();
        float width = (float) page.getMediaBox().getWidth();
        float height = (float) page.getMediaBox().getHeight();
        Graph graph = (pageRotation==Rotation.on90||pageRotation==Rotation.on270)
                ?new Graph(height, width)
                :new Graph(width, height);
        graph.setLeft(0);
        graph.setTop(0);
        graph.setZIndex(0);
page.getPageInfo().setMargin(new MarginInfo(0.0D, 0.0D, 0.0D, 0.0D));
        page.getParagraphs().add(graph);

        //to test how will be displayed text added into that rotated document use the following uncommented line
//        page.getParagraphs().add(new TextFragment("texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext"));
        //

com.aspose.pdf.drawing.Rectangle rectangle;
        if(pageRotation==Rotation.on270) {
            int rectangleHeight = 20;
            int rectangleWidth = 20;
            //position that we want to see as Bottom:Left will be actually Top:Left on page with rotated display
            //so height will be counted as width.
            rectangle =new com.aspose.pdf.drawing.Rectangle(
                    (float) 0, (float) width-rectangleWidth, (float) rectangleHeight, (float) rectangleWidth);

//            //Whole visible page rectangle:
//            int shift = 1;
//            com.aspose.pdf.drawing.Rectangle rectangle2 =new com.aspose.pdf.drawing.Rectangle(
//                    (float) 0+shift, (float) width-height+shift, (float) width-shift*2, (float) height-shift*2);
//            graph.getShapes().add(rectangle2);
        }else{
            rectangle=new com.aspose.pdf.drawing.Rectangle(
                    (float) 0, (float) 0, (float) 20, (float) 20);
        }
        rectangle.getGraphInfo().setFillColor(Color.getGreen());
        rectangle.getGraphInfo().setColor(Color.getGreen());
        graph.getShapes().add(rectangle);
        graph.setChangePosition(false);

        pdf.save(dataDir  + "CIB_letter_result_21_5__Graph_fixed.pdf"); 

coordinates.png (14.9 KB)
CIB_letter_result_21_5__Graph_fixed.pdf (9.0 KB)

Dear Asad,

Thank you for your response, rotation is causing other inconsistencies.
Can we reset the rotation while keeping the page same as is?

Best,

@evrrnd

Can you please share the details of inconsistencies by sharing some screenshots so that we can further check the case accordingly.

Hello,

In case of adding images, the mentioned axle is not the same for image rectangle.
Also, in case of adding textFragment, the text appears rotated.

So instead of adjusting all these things to rotated page, can we reset the rotation of the page while keeping the orientation rotated?

Regards

@evrrnd

We have updated the ticket information and will further check it against your feedback. We will inform you once we have more updates in this regard.