Я пробовал использовать pdfbox-android, порт Apache PDFBox для Android. и сумел извлечь простой текст. Однако этот подход предоставляет только неформатированный текст, который теряет структуру, такую как абзацы, маркеры, изображения и заголовки.
Я пробовал использовать этот класс
Код: Выделить всё
import com.tom_roush.pdfbox.pdmodel.PDDocument;
import com.tom_roush.pdfbox.text.PDFTextStripper;
import com.tom_roush.pdfbox.text.TextPosition;
import java.io.File;
import java.io.IOException;
public class PDFToHTMLConverter extends PDFTextStripper {
private StringBuilder html;
public PDFToHTMLConverter() throws IOException {
super();
this.html = new StringBuilder();
}
@Override
protected void startDocument(PDDocument document) {
html.append("");
}
@Override
protected void endDocument(PDDocument document) {
html.append("");
}
@Override
protected void writeString(String text) throws IOException {
html.append("
").append(new StringBuilder(text).reverse()).append("
"); // Wrap each line in a paragraph
}
@Override
protected void processTextPosition(TextPosition text) {
// Append each character and apply any custom HTML styling here if needed
html.append(text.getUnicode());
}
public String getHTMLText(File pdfFile) throws IOException {
try (PDDocument document = PDDocument.load(pdfFile)) {
this.writeText(document, new NullWriter());
}
return html.toString();
}
}
Код: Выделить всё
import java.io.Writer;
import java.io.IOException;
public class NullWriter extends Writer {
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
// Do nothing
}
@Override
public void flush() throws IOException {
// Do nothing
}
@Override
public void close() throws IOException {
// Do nothing
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... -html-form
Мобильная версия