centic9 / poi-on-android

A sample project that shows how Apache POI can be used in an Android application
Apache License 2.0
357 stars 89 forks source link

Document Format Issue #119

Open ghost opened 5 months ago

ghost commented 5 months ago

Hi, I hope you are doing well. I am working on the creation of document docx file but it does not open on different apps. So i found your repo. Your repo is working good. But you are using docx file lorem ipsum which has different format than the other one document i create. I tried to make a document but it was not helpful. My Issue is that if we do not give it the inputstream of raw file docx or we create another docx file (Which has different format from the file you placed in raw directory). It does not able to open file in other apps. And how you created this docx File

centic9 commented 5 months ago

I have very limited knowledge of developing Android apps and thus am not sure what exactly you are trying to do.

If you can share some code and the document which causes issues, I may be able to take a quick look, but can't guarantee as this project is just an example of how it can work, but your mileage may vary, especially with docx as there support in Apache POI itself is fairly limited.

ghost commented 5 months ago

I solved the problem but yet i do not know about the document format. Actually the main problem is about the document with the name lorem_ipsum in raw directory. try { FileInputStream(inputFile).use { fis -> val document = XWPFDocument(fis)

            // Collect paragraphs to remove
            val paragraphsToRemove = document.paragraphs.toList()

            // Remove paragraphs
            paragraphsToRemove.forEach {
                document.removeBodyElement(document.getPosOfParagraph(it))
            }

            // Add new content
            val paragraph = document.createParagraph()
            val run = paragraph.createRun()
            run.setText(context)

            // Write the modified document to a new file
            outputFile?.let {
                FileOutputStream(outputFile).use { fos ->
                    document.write(fos)
                }
            }
            streamOutputStream?.use { fos ->
                document.write(fos)
            }

        }
    } catch (e: IOException) {
        e.printStackTrace()
    }                                                                                                                                                                                                                     here at start we give the construct a stream                                                                                         

FileInputStream(inputFile).use { fis -> val document = XWPFDocument(fis) if we do not give him the constructor parameter which is allowed means constructor without parameter and then we create our own docx file. The file creates successfully but when we try to open it on another apps the file page shows infinity which is due to the document format which i told you i do not know the difference between your document you are providing in raw directory. I am attaching two docx files see the difference in the both files lorem_ipsum.docx test.docx and if i give it my created file which is with the name test.docx it again show infinity pages problem during opeing with third party apps.

centic9 commented 5 months ago

Sorry, I don't think I can help much, as said, support for this format in Apache POI itself is not very good and this project only uses whatever is provided there. I do not remember how I created the included document, but it seems to work fine on LibreOffce, so I expected it to be in a valid format.