简体   繁体   中英

Converting PDF to Word in Android Studio

I want to convert my pdf file to word, i've search & found itext for reading pdf and apache poi for convert word, i have a problem how to use it:D hehe,

public class MainActivity extends AppCompatActivity {

TextView browsefile, filename;
ImageView gambar;
int RequestFile = 2;
Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    browsefile = findViewById(R.id.browsefile);
    filename = findViewById(R.id.filename);
    gambar = findViewById(R.id.gambarfile);
    mContext = MainActivity.this;

    browsefile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startOptionBrowseFile();
        }
    });


}

private void startOptionBrowseFile() {
    Intent openFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
    openFileIntent.setType("*/*");
    startActivityForResult(openFileIntent, RequestFile);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RequestFile && resultCode == RESULT_OK && data != null){
        Uri selectedFile = data.getData();

        
        File path = new File(selectedFile.getPath());
        filename.setText(selectedFile.getPath());

        XWPFDocument doc = new XWPFDocument();
        String pdf = String.valueOf(path);
        PdfReader reader = null;
        try {
            reader = new PdfReader(pdf);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);

// Read the PDF page by page

        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            TextExtractionStrategy strategy = null;
            try {
                strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
            } catch (IOException e) {
                e.printStackTrace();
            }
            // Extract the text
            String text=strategy.getResultantText();
            // Create a new paragraph in the word document, adding the extracted text
            XWPFParagraph p = doc.createParagraph();
            XWPFRun run = p.createRun();
            run.setText(text);
            // Adding a page break
            run.addBreak(BreakType.PAGE);
        }

// Write the word document

        FileOutputStream out = null;
        try {
            out = new FileOutputStream("myfile.docx");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            doc.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }

// Close all open files

        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        reader.close();

    }
}


}

And this is the error

错误图像

Can anyone help me?I am grateful if anyone can solve my problem

Solution

The interface Paragraph is not found in classpath. This failure happend in older POI versions because the WL and SL interfaces were inside scratchpad , which is not included in poi jar.

POI older than V4.1.2

When you are using an older version than 4.1.2 you must also add the poi-scrachpad library as dependency to your classpath / build.gradle

See https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad

Switch to POI V4.1.2

As you can see at https://github.com/apache/poi/commit/f2dba42227abb4edf0ba0313972762974585693f the WP and SL interfaces were moved by this commit on 28 May 2015 for release 4.1.2

So if you are using 4.1.2 you simple need just a dedicated POI library jar in classpath/build.gradle and the Paragraph interface must be found (see https://mvnrepository.com/artifact/org.apache.poi )

If this is still a problem, ensure you have added the POI library as an implementation dependency and not accidently only for test .

build.gradle example:

dependencies {
   // https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
   compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM