Java 在Android Studio中将PDF转换为Word
我想将我的pdf文件转换为word,我已经搜索并找到了用于阅读pdf的itext和用于转换word的apache poi,我有一个问题如何使用它:呵呵Java 在Android Studio中将PDF转换为Word,java,android-studio,itext,apache-poi,converters,Java,Android Studio,Itext,Apache Poi,Converters,我想将我的pdf文件转换为word,我已经搜索并找到了用于阅读pdf的itext和用于转换word的apache poi,我有一个问题如何使用它:呵呵 public class MainActivity extends AppCompatActivity { TextView browsefile, filename; ImageView gambar; int RequestFile = 2; Context mContext; @Override protected void onCre
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);
//一页一页地阅读PDF
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);
}
//关闭所有打开的文件
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
reader.close();
}
}
}
这就是错误所在
有人能帮我吗?如果有人能解决我的问题,我将不胜感激
在类路径中找不到接口段落
。此故障发生在较旧的POI版本中,因为WL和SL接口位于scratchpad
内,而POI
jar中不包含该接口
早于V4.1.2的POI
当使用4.1.2以上的旧版本时,还必须将poi scrachpad
库作为依赖项添加到classpath/build.gradle中
看
切换到POI V4.1.2
正如你在
本次提交于2015年5月28日将WP和SL接口移至4.1.2版
因此,如果您使用的是4.1.2,您只需要在classpath/build.gradle中有一个专用的POI库jar,并且必须找到段落接口(请参阅)
如果这仍然是一个问题,请确保已将POI库添加为实现
依赖项,而不是仅针对测试
build.gradle
示例:
dependencies {
// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
}
dependencies {
// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
}