Mengonversi PDF ke EPUB, teks, XPS, dan lainnya dalam Java
Contents
[
Hide
]
Aspose.PDF for Java dapat mengekspor dokumen PDF ke dalam format output teks, ebook, cetak, dan berorientasi markup.
Mengubah PDF ke EPUB
Gunakan contoh ini ketika dokumen PDF harus diekspor ke format ebook EPUB.
- Buka PDF sumber di instans
Document. - Buat
EpubSaveOptionsdan atur mode pengenalan menjadiFlow. - Panggil
document.save(outputFile.toString(), saveOptions)sehingga konten PDF diekspor sebagai markup EPUB yang dapat di‑reflow. - Simpan file EPUB yang telah dikonversi.
public static void convertPdfToEpub(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
EpubSaveOptions saveOptions = new EpubSaveOptions();
saveOptions.setContentRecognitionMode(EpubSaveOptions.RecognitionMode.Flow);
document.save(outputFile.toString(), saveOptions);
}
System.out.println(inputFile + " converted into " + outputFile);
}
Mengonversi PDF ke TeX
Gunakan contoh ini ketika konten PDF harus diekspor ke markup TeX.
- Buka PDF sumber di instans
Document. - Buat
TeXSaveOptionsuntuk serialisasi TeX. - Panggil
document.save(outputFile.toString(), saveOptions)sehingga konten PDF dikeluarkan sebagai markup TeX. - Simpan file TeX yang dihasilkan.
public static void convertPdfToTex(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
document.save(outputFile.toString(), new TeXSaveOptions());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Mengonversi PDF ke teks biasa
Gunakan contoh ini ketika dokumen PDF harus diekspor sebagai file teks.
- Buka PDF sumber di instans
Document. - Buat
TextDeviceuntuk mengekstrak konten teks dari halaman PDF. - Panggil
device.process(document.getPages().get_Item(1), outputFile.toString())menulis halaman pertama sebagai teks biasa. - Simpan file output teks.
public static void convertPdfToTxt(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
TextDevice device = new TextDevice();
device.process(document.getPages().get_Item(1), outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Mengubah PDF ke XPS
Gunakan contoh ini ketika dokumen PDF harus dikonversi ke format XPS.
- Buka PDF sumber di instans
Document. - Buat
XpsSaveOptionsdan aktifkan font TrueType tersemat. - Panggil
document.save(outputFile.toString(), saveOptions)sehingga PDF diserialkan sebagai XPS dengan sumber daya font yang disematkan. - Simpan file XPS yang telah dikonversi.
public static void convertPdfToXps(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
XpsSaveOptions saveOptions = new XpsSaveOptions();
saveOptions.setUseEmbeddedTrueTypeFonts(true);
document.save(outputFile.toString(), saveOptions);
}
System.out.println(inputFile + " converted into " + outputFile);
}
Mengonversi PDF ke Markdown
Gunakan contoh ini ketika konten PDF harus diekspor sebagai Markdown.
- Buka PDF sumber di instans
Document. - Buat
MarkdownSaveOptionsdan konfigurasikan direktori sumber daya gambar plus output tag gambar HTML. - Panggil
document.save(outputFile.toString(), saveOptions)sehingga konten PDF dihasilkan sebagai Markdown dengan sumber gambar eksternal. - Simpan file Markdown yang dihasilkan.
public static void convertPdfToMd(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setResourcesDirectoryName("images");
saveOptions.setUseImageHtmlTag(true);
document.save(outputFile.toString(), saveOptions);
}
System.out.println(inputFile + " converted into " + outputFile);
}
Mengonversi PDF ke Mobi XML
Gunakan contoh ini ketika konten PDF harus diekspor ke XML yang kompatibel dengan Mobi.
- Buka PDF sumber di instans
Document. - Pilih
SaveFormatMobiXmlsebagai format serialisasi target. - Panggil
document.save(outputFile.toString(), SaveFormat.MobiXml)sehingga PDF diekspor sebagai XML yang kompatibel dengan Mobi. - Simpan file yang telah dikonversi.
public static void convertPdfToMobiXml(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
document.save(outputFile.toString(), SaveFormat.MobiXml);
}
System.out.println(inputFile + " converted into " + outputFile);
}