Home » excel » excel – read pdf file in alfresco

excel – read pdf file in alfresco

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’ve added a UI action to alfresco that gets the nodeRef as a parameter i’m trying to read the content with

contentService.getReader(nodeRef, ContentModel.TYPE_CONTENT).getContentString()

But it’s only working with .txt files and not with .pdf , .xlsx , .docx …..
Example when i try to read a pdf File it gets me :

S#?_#3C??  R/Metadata 64 0 R/OCProperties<</D<</Order[]/R .....

with word document and excel its numbers.

Is there a Solution ?

How to&Answers:

Yes. Try contentService.getReader(nodeRef, ContentModel.TYPE_CONTENT).getContentInputStream() instead. If you need to find certain text, feed the data from the stream into a PDF library and use its API to access the content.

Answer:

Actually Alfresco has his own readers, converters and all the staff so to read from any content i used this code and it works

ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
       String content="";
        if (reader != null && reader.exists())
        {
                // get the transformer
                ContentTransformer transformer = contentService.getTransformer(reader.getMimetype(), MimetypeMap.MIMETYPE_TEXT_PLAIN);
                // is this transformer good enough?
                if (transformer != null)
                {
                    // We have a transformer that is fast enough
                    ContentWriter writer = contentService.getTempWriter();
                    writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);

                    try
                    {
                        transformer.transform(reader, writer);
                        // point the reader to the new-written content
                        reader = writer.getReader();
                        // Check that the reader is a view onto something concrete
                        if (!reader.exists())
                        {
                            logging(new ContentIOException("The transformation did not write any content, yet: \n"
                                    + "   transformer:     " + transformer + "\n" + "   temp writer:     " + writer+"")+"");

                            throw new ContentIOException("The transformation did not write any content, yet: \n"
                                    + "   transformer:     " + transformer + "\n" + "   temp writer:     " + writer);

                        }else {
                               content = reader.getContentString();
                               logging("------------------------------------------------------------");
                               logging(content);
                        }
                    }
                    catch (ContentIOException e)
                    {


                    }
                }
            }