开发者社区 > 云效DevOps > 正文

请问云效中怎么使用java做过将pdf(内容含有文字,图片)文档的内容插入到word文档中?word

请问云效中怎么使用java做过将pdf(内容含有文字,图片)文档的内容插入到word文档中?word文档的一个表格。

展开
收起
十一0204 2023-07-05 09:38:11 98 0
7 条回答
写回答
取消 提交回答
  • 公众号:网络技术联盟站,InfoQ签约作者,阿里云社区签约作者,华为云 云享专家,BOSS直聘 创作王者,腾讯课堂创作领航员,博客+论坛:https://www.wljslmz.cn,工程师导航:https://www.wljslmz.com

    您可以使用Apache POI库来实现Java代码中的PDF转Word功能。首先,您需要将PDF文件读入到内存中,然后使用Apache PDFBox提供的功能将其转换为文本格式。接着,使用POI库读入Word文件,然后将转换后的文本写入Word文件即可 。

    2023-08-07 23:15:49
    赞同 展开评论 打赏
  • 在云效中使用Java将PDF文档的内容插入到Word文档中,可以使用Apache PDFBox和Apache POI这两个开源库进行操作。

    首先,需要导入以下依赖:

    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>2.0.26</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.0.0</version>
    </dependency>
    

    然后,可以使用以下代码将PDF文档的内容插入到Word文档中的表格:

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.text.PDFTextStripper;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFTable;
    import org.apache.poi.xwpf.usermodel.XWPFTableRow;
    import org.apache.poi.xwpf.usermodel.XWPFTableCell;
    
    public class PDFToWord {
        public static void main(String[] args) {
            try {
                // 加载PDF文档
                PDDocument document = PDDocument.load(new FileInputStream("input.pdf"));
    
                // 创建一个空的Word文档
                XWPFDocument wordDocument = new XWPFDocument();
    
                // 获取PDF文档的内容
                PDFTextStripper stripper = new PDFTextStripper();
                String pdfContent = stripper.getText(document);
    
                // 创建一个表格
                XWPFTable table = wordDocument.createTable();
    
                // 将PDF文档的内容按行插入到表格中
                String[] lines = pdfContent.split("\\r?\\n");
                for (String line : lines) {
                    XWPFTableRow row = table.createRow();
                    XWPFTableCell cell = row.getCell(0);
                    cell.setText(line);
                }
    
                // 保存Word文档
                FileOutputStream out = new FileOutputStream("output.docx");
                wordDocument.write(out);
                out.close();
    
                // 关闭文档
                document.close();
                wordDocument.close();
    
                System.out.println("转换完成!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    上述代码中,需要将input.pdf替换为实际的PDF文件路径,转换后的Word文档将保存为output.docx。

    2023-07-31 15:44:07
    赞同 展开评论 打赏
  • 天下风云出我辈,一入江湖岁月催,皇图霸业谈笑中,不胜人生一场醉。

    Java SDK 使用说明:https://help.aliyun.com/document_detail/66496.html

    使用 Maven 引入 SDK:


    com.aliyun
    aliyun-java-sdk-core
    4.5.0


    com.aliyun
    aliyun-java-sdk-codeup
    0.0.8

    调用 Codeup API
    以 CreateRepository 为例:

    创建AK、SK见 https://usercenter.console.aliyun.com/manage/ak

    创建个人访问令牌见 个人访问令牌

    可以使用 Codeup Java SDK 或者阿里云 OpenAPI SDK 调用接口,区别在于使用阿里云OpenAPI SDK 需要手动设置接口信息。

    使用 Codeup Java SDK

    package com.alibaba.openapitest.demo;

    import com.alibaba.fastjson.JSON;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.codeup.model.v20200414.CreateRepositoryRequest;
    import com.aliyuncs.codeup.model.v20200414.CreateRepositoryResponse;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.profile.DefaultProfile;

    public class CreateRepository {

    private String accessKeyId = "<AccessKey>";
    private String accessSecret = "<AccessSecret>";
    
    /**
     * 个人访问令牌;使用AK&SK或STS 临时授权方式不传该字段
     */
    private String personalAccessToken = "<PersonalAccessToken>";
    
    private String regionId = "cn-hangzhou";
    private String endPoint = "codeup.cn-hangzhou.aliyuncs.com";
    
    /**
     * 企业 ID
     */
    private String organizationId = "<OrganizationId>";
    
    public void createRepository() {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
    
        CreateRepositoryRequest request = new CreateRepositoryRequest();
        request.setEndpoint(endPoint);
        request.setOrganizationId(organizationId);
        request.setAccessToken(personalAccessToken);
    
        // 请求体参数,参考API文档
        String body = "{\"name\": \"repoName\", \"path\": \"repoPath\", \"visibility_level\": 10, \"namespace_id\": 123}";
    
        request.setHttpContent(JSON.toJSONString(body).getBytes(), "Utf-8", FormatType.JSON);
        try {
            CreateRepositoryResponse response = client.getAcsResponse(request);
            logInfo(String.valueOf(response.getResult().getId()));
        } catch (ServerException e) {
            logInfo(String.format("Fail. Something with your connection with Aliyun go incorrect. ErrorCode: %s",
                    e.getErrCode()));
        } catch (ClientException e) {
            logInfo(String.format("Fail. Business error. ErrorCode: %s, RequestId: %s",
                    e.getErrCode(), e.getRequestId()));
        }
    }
    
    private static void logInfo(String message) {
        System.out.println(message);
    }
    
    public static void main(String[] args) {
        new CreateRepository().createRepository();
    }
    

    }
    使用阿里云 OpenAPI SDK

    import com.aliyuncs.CommonRequest;
    import com.aliyuncs.CommonResponse;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.MethodType;
    import com.aliyuncs.profile.DefaultProfile;
    /*
    pom.xml


    com.aliyun
    aliyun-java-sdk-core
    4.0.3

    */
    public class CreateRepository {
    public static void main(String[] args) {
    DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "", "");
    IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("codeup.cn-hangzhou.aliyuncs.com");
        request.setVersion("2020-04-14");
        request.setUriPattern("/api/v3/projects");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("OrganizationId", "<OrganizationId>");
        request.putHeadParameter("Content-Type", "application/json");
        String requestBody = "" +
                "{" +
                "    \"name\": \"<RepoName>\"," +
                "    \"path\": \"<RepoPath>\"," +
                "    \"visibility_level\": 10," +
                "    \"namespace_id\": 123" +
                "}";
        request.setHttpContent(requestBody.getBytes(), "utf-8", FormatType.JSON);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
    

    }

    2023-07-31 12:07:52
    赞同 展开评论 打赏
  • 在云效中使用 Java 将 PDF 文档的内容插入到 Word 文档中,可以通过使用一些开源的 Java 库来实现。以下是一个简单的示例代码,演示了如何使用 Apache PDFBox 和 Apache POI 库来实现此功能:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.text.PDFTextStripper;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFTable;
    import org.apache.poi.xwpf.usermodel.XWPFTableRow;
    import org.apache.poi.xwpf.usermodel.XWPFTableCell;
    
    public class PdfToWordConverter {
        public static void main(String[] args) {
            try {
                // 读取 PDF 文档
                PDDocument pdfDoc = PDDocument.load(new FileInputStream("input.pdf"));
    
                // 提取 PDF 文档的文本内容
                PDFTextStripper stripper = new PDFTextStripper();
                String pdfContent = stripper.getText(pdfDoc);
    
                // 创建 Word 文档
                XWPFDocument wordDoc = new XWPFDocument();
                XWPFTable table = wordDoc.createTable();
    
                // 将 PDF 内容添加到 Word 表格中
                String[] lines = pdfContent.split("\\r?\\n");
                for (String line : lines) {
                    XWPFTableRow row = table.createRow();
                    XWPFTableCell cell = row.getCell(0);
                    cell.setText(line);
                }
    
                // 保存 Word 文档
                FileOutputStream out = new FileOutputStream(new File("output.docx"));
                wordDoc.write(out);
                out.close();
    
                // 关闭文档
                pdfDoc.close();
                wordDoc.close();
    
                System.out.println("PDF 文档转换为 Word 文档成功!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    2023-07-26 22:33:06
    赞同 展开评论 打赏
  • 你可以使用Apache POI和iText库来实现将PDF文档内容插入到Word文档中。具体步骤如下:

    1. 使用iText库读取PDF文档,获取文档中的文字和图片等内容;
    2. 使用Apache POI库创建一个Word文档,并在其中创建一个表格;
    3. 将获取的PDF文档内容插入到表格中。

    以下是一个简单的Java代码示例:

    import java.io.*;
    import java.util.List;
    
    import org.apache.poi.xwpf.usermodel.*;
    import com.itextpdf.text.pdf.*;
    import com.itextpdf.text.*;
    
    public class PdfToWord {
        public static void main(String[] args) throws IOException, DocumentException {
            // 读取PDF文档
            PdfReader reader = new PdfReader("input.pdf");
            StringWriter output = new StringWriter();
            List<TextRenderInfo> textRenderInfos = PdfTextExtractor.getTextFromPage(reader, 1).get(0).getCharacterRenderInfos();
            for (TextRenderInfo textRenderInfo : textRenderInfos) {
                output.write(textRenderInfo.getText());
            }
            // 获取PDF文档中的图片
            PdfDictionary pageDict = reader.getPageN(1);
            PdfDictionary resourcesDict = pageDict.getAsDict(PdfName.RESOURCES);
            PdfDictionary xObjectDict = resourcesDict.getAsDict(PdfName.XOBJECT);
            for (PdfName name : xObjectDict.getKeys()) {
                PdfObject object = xObjectDict.get(name);
                if (object.isIndirect()) {
                    PdfDictionary xObject = (PdfDictionary) PdfReader.getPdfObject(object);
                    PdfName subType = (PdfName) xObject.get(PdfName.SUBTYPE);
                    if (subType != null && subType.equals(PdfName.IMAGE)) {
                        int width = xObject.getAsNumber(PdfName.WIDTH).intValue();
                        int height = xObject.getAsNumber(PdfName.HEIGHT).intValue();
                        Image image = Image.getInstance(object);
                        // 在Word文档中插入图片
                        XWPFDocument doc = new XWPFDocument();
                        XWPFTable table = doc.createTable();
                        XWPFTableRow row = table.getRow(0);
                        XWPFTableCell cell = row.getCell(0);
                        cell.addParagraph().createRun().addPicture(image.getData(), XWPFDocument.PICTURE_TYPE_PNG, "image.png", Units.toEMU(width), Units.toEMU(height));
                        // 保存Word文档
                        FileOutputStream out = new FileOutputStream("output.docx");
                        doc.write(out);
                        out.close();
                    }
                }
            }
            reader.close();
        }
    }
    

    需要注意的是,以上代码只是一个简单的示例,实际使用时可能需要根据实际情况进行修改和优化。

    2023-07-26 10:02:39
    赞同 1 展开评论 打赏
  • 北京阿里云ACE会长

    使用 Apache POI 和 iText 库来实现。这些库提供了丰富的 API,可以让您轻松地操作 Word 和 PDF 文档。

    以下是实现的大致步骤:

    使用 iText 库读取 PDF 文档,提取其中的文本和图像等内容。

    使用 Apache POI 库创建 Word 文档,并在其中插入表格。

    将 PDF 文档中提取出的文本和图像等内容插入到 Word 文档中的表格中。

    下面是一个示例代码,演示如何将 PDF 文档中的内容插入到 Word 文档中的表格中:

    stylus
    Copy
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.util.List;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import com.itextpdf.text.Document;
    import com.itextpdf.text.pdf.PdfReader;
    import com.itextpdf.text.pdf.parser.PdfTextExtractor;
    import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
    import com.itextpdf.text.pdf.parser.TextExtractionStrategy;

    public class PdfToWord {
    public static void main(String[] args) throws IOException {
    // 从 URL 中读取 PDF 文件
    URL url = new URL("https://example.com/myfile.pdf");
    InputStream in = url.openStream();
    PdfReader reader = new PdfReader(in);

        // 创建 Word 文档和表格
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("My Sheet");
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
    
        // 从 PDF 文档中提取文本和图像等内容
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            TextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
            String text = PdfTextExtractor.getTextFromPage(reader, i, strategy);
            sb.append(text);
        }
        List<byte[]> images = reader.getPdfObjectRelease();
    
        // 将文本和图像等内容插入到 Word 文档中的表格中
        cell.setCellValue(sb.toString());
        // 将图像插入到单元格中,具体实现方式可以根据需要自行调整
        // ...
    
        // 保存 Word 文档
        FileOutputStream out = new FileOutputStream("output.docx");
        workbook.write(out);
        out.close();
        workbook.close();
    }
    

    }
    需要注意的是,本示例代码仅提供了基本框架,具体实现方式可能需要根据您的具体需求进行调整。

    https://help.aliyun.com/document_detail/107313.html?spm=a2c4g.460489.0.i7

    image.png

    image.png

    2023-07-25 20:08:19
    赞同 展开评论 打赏
  • 全栈JAVA领域创作者

    您可以使用Apache PDFBox和Apache POI库来实现将PDF文档的内容插入到Word文档中。以下是一个简单的示例,演示如何使用这些库将PDF文档的内容插入到Word文档中的表格中:

    1. 首先,您需要添加以下依赖项到您的项目中:
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>2.0.24</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.0.0</version>
    </dependency>
    
    1. 然后,您可以使用以下代码将PDF文档的内容插入到Word文档中的表格中:
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.text.PDFTextStripper;
    import org.apache.poi.xwpf.usermodel.*;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.List;
    
    public class PdfToWordTableExample {
        public static void main(String[] args) throws Exception {
            // Load the PDF document and extract text using PDFTextStripper
            PDDocument document = PDDocument.load(new File("input.pdf"));
            PDFTextStripper pdfStripper = new PDFTextStripper();
            String text = pdfStripper.getText(document);
            document.close();
    
            // Create a new Word document and add a table to it
            XWPFDocument wordDocument = new XWPFDocument();
            XWPFTable table = wordDocument.createTable(3, 3); // Create a table with three rows and three columns
    
            // Split the text into lines and add them to the table row by row
            List<String> lines = Arrays.asList(text.split("\\r?
    ")); // Split the text into lines using line breaks as delimiter
            for (int i = 0; i < lines.size(); i++) {
                XWPFTableRow row = table.getRow(i); // Get the current row
                XWPFTableCell cell = row.getCell(0); // Get the first cell in the row (column index starts from 0)
                cell.setText(lines.get(i)); // Set the cell text to the current line of text
            }
    
            // Save the Word document to disk
            FileOutputStream out = new FileOutputStream("output.docx");
            wordDocument.write(out);
            out.close();
            wordDocument.close();
        }
    }
    
    2023-07-24 19:58:11
    赞同 展开评论 打赏
滑动查看更多
问答分类:
问答地址:

云效,企业级一站式研发协同平台,数十万企业都在用。支持公共云、专有云和混合云多种部署形态,通过云原生新技术和研发新模式,助力创新创业和数字化转型企业快速实现研发敏捷和组织敏捷,打造“双敏”组织,实现多倍效能提升。

热门讨论

热门文章

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载