如何将DOCX转换为PDF使用Java(韩国)

发布于 2025-02-11 16:11:20 字数 1403 浏览 3 评论 0原文

我尝试将DOCX转换为PDF。

DOCX文件有韩语 也许您认为BTS歌曲的歌词,

例如。 朋友们 BTS 유난히도유난히도! 처음보는보는다른 땀에땀에밴채만난 뭔가이상했었던 난,넌넌 우리대화는숙제 하루는,하루는하루는 我只是想明白 你好我的外星人 우린우린神秘 그래서더특별한 언젠가언젠가이때때때때 내옆에함께 영원히계속이곳에逗留,嘿 네작은 일곱번의번의추운 오래 这是

我的转换代码。

  String k=null;
        OutputStream fileForPdf =null;
    try {

        String fileName=w;
        //Below Code is for .doc file
        if(fileName.endsWith(".doc"))
        {
            HWPFDocument doc = new HWPFDocument(new FileInputStream(
                    fileName));
            WordExtractor we=new WordExtractor(doc);
            k = we.getText();

            fileForPdf = new FileOutputStream(new File(
                    p));
            we.close();
        }

        //Below Code for

        else if(fileName.endsWith(".docx"))
        {
            XWPFDocument docx = new XWPFDocument(new FileInputStream(
                    fileName));
            // using XWPFWordExtractor Class
            XWPFWordExtractor we = new XWPFWordExtractor(docx);
            k = we.getText();

            fileForPdf = new FileOutputStream(new File(p));
            we.close();
        }



        Document document = new Document();
        PdfWriter.getInstance(document, fileForPdf);

        document.open();

        document.add(new Paragraph(k));

        document.close();
        fileForPdf.close();

此代码制作PDF文件,但此文件没有韩国歌词

I try convert docx to pdf.

Docx file has korean
maybe you think BTS song's lyrics

Eg.
Friends
BTS
유난히도 반짝였던 서울!
처음 보는 또 다른 세상
땀에 잔뜩 밴 채 만난 넌
뭔가 이상했었던 아이
난 달에서, 넌 별에서
우리 대화는 숙제 같았지
하루는 베프, 하루는 웬수
I just wanna understand
Hello my alien
우린 서로의 mystery
그래서 더 특별한 걸까
언젠가 이 함성 멎을 때 stay, hey
내 옆에 함께 있어줘
영원히 계속 이곳에 stay, hey
네 작은 새끼손가락처럼
일곱 번의 여름과 추운 겨울보다
오래
수많은 약속과 추억들보다

this is my convering code.

  String k=null;
        OutputStream fileForPdf =null;
    try {

        String fileName=w;
        //Below Code is for .doc file
        if(fileName.endsWith(".doc"))
        {
            HWPFDocument doc = new HWPFDocument(new FileInputStream(
                    fileName));
            WordExtractor we=new WordExtractor(doc);
            k = we.getText();

            fileForPdf = new FileOutputStream(new File(
                    p));
            we.close();
        }

        //Below Code for

        else if(fileName.endsWith(".docx"))
        {
            XWPFDocument docx = new XWPFDocument(new FileInputStream(
                    fileName));
            // using XWPFWordExtractor Class
            XWPFWordExtractor we = new XWPFWordExtractor(docx);
            k = we.getText();

            fileForPdf = new FileOutputStream(new File(p));
            we.close();
        }



        Document document = new Document();
        PdfWriter.getInstance(document, fileForPdf);

        document.open();

        document.add(new Paragraph(k));

        document.close();
        fileForPdf.close();

this code make pdf file But this file didn't have korea lyrics

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小镇女孩 2025-02-18 16:11:21

您可以尝试以下方法将.docx文件转换为.pdf文件。

https://simples.dev/simples.dev/java-convert -docx-file-to-pdf-file-using-xdocReport/

import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FileConverter {

    public void convertWordToPdf(String docxFileName, String pdfFileName) {
        try(InputStream inputStream = new FileInputStream(docxFileName);
            OutputStream outputStream = new FileOutputStream(pdfFileName)) {
            XWPFDocument document = new XWPFDocument(inputStream);
            PdfOptions options = PdfOptions.create();
            // Convert .docx file to .pdf file
            PdfConverter.getInstance().convert(document, outputStream, options);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
String docxFileName = "D:\\SimpleSolution\\Data\\Document.docx";
String pdfFileName = "D:\\SimpleSolution\\Data\\Document.pdf";

FileConverter fileConverter = new FileConverter();
fileConverter.convertWordToPdf(docxFileName, pdfFileName);

You can try the following approach to convert a .docx file to .pdf file.

https://simplesolution.dev/java-convert-docx-file-to-pdf-file-using-xdocreport/

import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FileConverter {

    public void convertWordToPdf(String docxFileName, String pdfFileName) {
        try(InputStream inputStream = new FileInputStream(docxFileName);
            OutputStream outputStream = new FileOutputStream(pdfFileName)) {
            XWPFDocument document = new XWPFDocument(inputStream);
            PdfOptions options = PdfOptions.create();
            // Convert .docx file to .pdf file
            PdfConverter.getInstance().convert(document, outputStream, options);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
String docxFileName = "D:\\SimpleSolution\\Data\\Document.docx";
String pdfFileName = "D:\\SimpleSolution\\Data\\Document.pdf";

FileConverter fileConverter = new FileConverter();
fileConverter.convertWordToPdf(docxFileName, pdfFileName);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文