PDF Rasterisation fails

Hi,

When trying to rasterise this PDF
onlyIMG.pdf (20.1 KB)

using a code such as

        using Document pdfDocument = new (...);
        var outputStream = new MemoryStream();

        PngDevice imageDevice = new();
        imageDevice.Process(pdfDocument.Pages[0], outputStream);

We get a blank page with only the qrcode.

If we strip the clipping commands from the document, the rasterisation succeeds…

Would you have any idea what could be the problem?

Best regards,

Olivier

@Olivier.G

Can you please share the output image that you obtained at your end?

@asad.ali

Sure, here it is
sample_001.png (12.6 KB)

Incidentally, we haven’t encountered any such problem with other PDF tools.

@Olivier.G

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-58919

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@asad.ali thank you.

To avoid this issue until the bug is fixed, we’re constraining the EOClip rectangle coordinates/dimensions that have unexpectedly large values, directly in the page contents collection.

It turns out that this is particularly costly, even when only one object is concerned: would you have any tip to make things run more quickly?

In any case, I suppose the correct way to do this is to invoke SuppressUdpate first in case several objects are modified and call ResumeUpdate at the end?

@Olivier.G

You’re correct that constraining the EOClip rectangle coordinates/dimensions for every object in a collection can be computationally expensive, especially if the constraints require recalculations or rendering updates for each change. You can use SuppressUpdate and ResumeUpdate to minimize the overhead along with using minimal iterations of the object while precomputing. Nevertheless, you can further optimize your approach through browsing the same topics on the internet. In case you have any inquiry specific to Aspose.PDF, please let us know.

@asad.ali

I think our code is already pretty optimised, possibly more so than by trying to use the visitor pattern:

double min = -100000;
double max = 1000000;
double epsilon = 0.1;
bool isModified = false;

foreach (int endOfClipIndex in pdfPageContents.Where(op => op is EOClip).Select(op => op.Index))
{
    if (pdfPageContents[endOfClipIndex - 1] is Re clipRectangle)
    {
        double recX = Math.Clamp(clipRectangle.X, min, max);
        double recY = Math.Clamp(clipRectangle.Y, min, max);
        double recWidth = Math.Clamp(clipRectangle.Width, min, max);
        double recHeight = Math.Clamp(clipRectangle.Height, min, max);

        if (Math.Abs(recX - clipRectangle.X) > epsilon
         || Math.Abs(recY - clipRectangle.Y) > epsilon
         || Math.Abs(recWidth - clipRectangle.Width) > epsilon
         || Math.Abs(recHeight - clipRectangle.Height) > epsilon)
        {
            if (!isModified)
            {
                isModified = true;
                pdfPageContents.SuppressUpdate();
            }
            pdfPageContents[endOfClipIndex - 1] = new Re(recX, recY, recWidth, recHeight);
        }
    }
}

if (isModified)
{
    pdfPageContents.ResumeUpdate();
}

could you please confirm?

My understanding is that updating the content stream is when most of the work is done, hence the need for SuppressUpdate and so I was wondering whether content stream updates could be suspended while modifying several pages of the document, as opposed to one page at a time.

Thank you for your help.

@Olivier.G

The code look well optimized and do not see any improvements if can be made. Furthermore, as requested earlier, you can browse more options to achieve your requirements on the internet about capturing the rectangle by user and if you face some issues related to Aspose.PDF, please let us know.

@asad.ali

One last thing regarding the final question in my previous message: I was wondering whether content stream updates could be suspended while modifying several pages of the document, as opposed to one page at a time. I haven’t found how this could be achieved.

@Olivier.G

SuppressUpdates is applied to one page only, i.e. it should be called for every page which processed.