[英]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);
//逐页读取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);
}
//写word文档
FileOutputStream out = null;
try {
out = new FileOutputStream("myfile.docx");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
doc.write(out);
} catch (IOException e) {
e.printStackTrace();
}
// 关闭所有打开的文件
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
reader.close();
}
}
}
这就是错误
谁能帮助我?如果有人能解决我的问题,我将不胜感激
在类路径中找不到接口Paragraph
。 此故障发生在较旧的 POI 版本中,因为 WL 和 SL 接口位于scratchpad
内,而poi
jar 中不包含该暂存器。
当您使用比 4.1.2 更旧的版本时,您还必须将poi-scrachpad
库作为依赖项添加到您的类路径 / build.gradle
见https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad
正如您在https://github.com/apache/poi/commit/f2dba42227abb4edf0ba0313972762974585693f中看到的那样,WP 和 SL 接口在 2015 年 5 月 28 日的 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 )
如果这仍然是一个问题,请确保您已将 POI 库添加为implementation
依赖项,而不是意外地仅用于test
。
build.gradle
示例:
dependencies {
// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.