OpenPDF icon indicating copy to clipboard operation
OpenPDF copied to clipboard

Add Image rendering callback

Open andreasrosdal opened this issue 6 years ago • 10 comments

A OpenPDF user has requested support for an Image rendering callback function.

"I've been trying to migrate from iText v5.5.11(License version) library to OpenPDF v1.2.8(Open Source) library.I think that both are having similar functionalities. But,the renderImage function from iText library is missing in OpenPDF library and I'm in need of an alternative function in OpenPDF library" Source: https://stackoverflow.com/questions/54408620/itextpdf-to-openpdf-integration

What is the use-case? what does this user want to do, which is currently not possible in OpenPDF?

andreasrosdal avatar Apr 26 '19 07:04 andreasrosdal

There is note in iText we don't call invokeOperator for embedded images - this is one area of the PDF spec that is particularly nasty and inconsistent. For inline images they do not call operator to draw, rather this callback only.

I think that is a major change to existing pdf processing, as it differs much from what is in iText. How much can we copy/move from their codebase?

com.itextpdf.text.pdf.parser

try {
            PRTokeniser tokeniser = new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().createSource(contentBytes)));
            PdfContentParser ps = new PdfContentParser(tokeniser);
            ArrayList<PdfObject> operands = new ArrayList<PdfObject>();
            while (ps.parse(operands).size() > 0){
                PdfLiteral operator = (PdfLiteral)operands.get(operands.size()-1);
                if ("BI".equals(operator.toString())){
                    // we don't call invokeOperator for embedded images - this is one area of the PDF spec that is particularly nasty and inconsistent
                    PdfDictionary colorSpaceDic = resources != null ? resources.getAsDict(PdfName.COLORSPACE) : null;
                    handleInlineImage(InlineImageUtils.parseInlineImage(ps, colorSpaceDic), colorSpaceDic);
                } else {
                    invokeOperator(operator, operands);
                }
            }

        }

While in OpenPdf it is in PdfContentByte#addImage

Kindrat avatar Jun 05 '19 16:06 Kindrat

Ok, actually we cam mimic same behaviour by changing only private class Do implements ContentOperator in PdfContentStreamHandler.

Kindrat avatar Jun 05 '19 17:06 Kindrat

@Kindrat Thanks for looking at this. Remember, all contributions to OpenPDF must be developed independently, and must not be copied from other projects.

andreasrosdal avatar Jun 06 '19 17:06 andreasrosdal

yeah, but in this case entrypoint would be exactly the same, however processing and callback signature would be different as in iText they introduced lots of new entities to work with these callbacks.

we can expose similar callback with same info but different representation

Kindrat avatar Jun 06 '19 19:06 Kindrat

Good plan. Pull requests welcome

andreasrosdal avatar Jun 10 '19 17:06 andreasrosdal

Where are we with this?

testn avatar Apr 22 '20 04:04 testn

Pull requests welcome, still :)

andreasrosdal avatar Apr 22 '20 07:04 andreasrosdal

@andreasrosdal are you ok with api exactly the same as itextpdf?

testn avatar Apr 23 '20 04:04 testn

The idea is that we make an API which solves the problem reported by this user. The API doesn't have to be exactly like anything else. There could be something different with a different name, and we could tell users of OpenPDF to use this instead.

@testn @Kindrat

andreasrosdal avatar Mar 17 '22 04:03 andreasrosdal

Pull requests are welcome!

asturio avatar Mar 08 '24 19:03 asturio