png\word\pdf相关

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>pdf</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/>
    </parent>
    <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.16</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-local</artifactId>
            <version>1.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-transformer-msoffice-word</artifactId>
            <version>1.0.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.4</version>
        </dependency>
        <dependency>
            <groupId>net.coobird</groupId>
            <artifactId>thumbnailator</artifactId>
            <version>0.4.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency><!--
        <dependency>
            <groupId>ework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>3.2.3</version>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>3.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>3.0.0</version>
        </dependency>

    </dependencies>

</project>
package org.example.tool;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.beans.factory.annotation.Value;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class pdf {
    @Value("${png.path}")
    String pngPath;

    public static void main(String[] args) throws Exception {
        replace("${123}", "替换替换", "D:/111.docx");
        wordToPdf("D:/data/111.docx");
        pdfToPng("D:/data/111.pdf");
    }

    //替换docx中文件内容   oldString不能是中文
    public static void replace(String oldString, String newString, String path) {
        try {
            File file = new File(path);
            BufferedReader br = new BufferedReader(new FileReader(file));
            ArrayList<String> strings = new ArrayList<String>();
            String s;//读取的每一行数据
            while ((s = br.readLine()) != null) {
                if (s.contains(oldString)) {
                    s = s.replace(oldString, newString);
                    System.out.println("替换成功");
                }
                strings.add(s);//将数据存入集合
            }
            BufferedWriter bw = new BufferedWriter(new FileWriter(path));
            for (String string : strings) {
                bw.write(string);//一行一行写入数据
                bw.newLine();//换行
            }
            bw.close();
        } catch (Exception e) {
            e.getMessage();
        }
    }



    //pdf转docx
    public static void wordToPdf(String wordPath) {
        File inPath = new File(wordPath);
        File outPath = new File(wordPath.replace(".docx", ".pdf"));
        try {
            InputStream docxInputStream = new FileInputStream(inPath);
            OutputStream outputStream = new FileOutputStream(outPath);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
            System.out.println("success");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //pdf转png
    public static void pdfToPng(String pdfPath) {
        try {
            // PDF文件路径
            String pdfFilePath = pdfPath;
            // 输出目录路径
            String outputDirPath = pdfPath.substring(0, pdfPath.lastIndexOf("/"));
            // 加载PDF文档
            PDDocument document = PDDocument.load(new File(pdfFilePath));
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            // 遍历PDF的每一页
            for (int page = 0; page < document.getNumberOfPages(); ++page) {
                // 渲染PDF页面为图像
                BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300); // 设置DPI为300
                // 将图像保存为PNG文件
                String pngFileName = outputDirPath + "/pdf" + "/page-" + (page + 1) + ".png";
                ImageIO.write(bim, "png", new File(pngFileName));
            }
            // 关闭文档
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
package org.example.tool;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

import static java.lang.System.in;

public class wordAddPng {
    public static void main(String[] args) throws Exception {
        wordAddPng w = new wordAddPng();
        String filePath = "D:/data/111.docx";
        if (!Files.exists(Paths.get(filePath))){
            Files.createFile(Paths.get(filePath));
        }
        InputStream docis = new FileInputStream("D:/data/111.docx");
        //转成word
        CustomXWPFDocument document = new CustomXWPFDocument(docis);
        //写入图片
        String s = "D:/data/111.png,D:/data/123.jpg";
        String[] split = s.split(",");
        List<String> list = Arrays.asList(split);
        for (String s1 : list) {
            w.insertImage(document, s1,filePath);
        }
    }


    private void insertImage(CustomXWPFDocument document, String s,String filePath) throws IOException, InvalidFormatException {
        //图片
        System.out.println(s);
        FileInputStream in = new FileInputStream(s);
        //段落集合
        List<XWPFParagraph> paragraphs = document.getParagraphs();
        System.out.println(paragraphs.get(paragraphs.size() - 1).getText());
        byte[] ba = new byte[in.available()];
        int len = in.read(ba);
        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(ba, 0, len);
        //设置图片
        if (s.contains("png")) {
            document.addPictureData(byteInputStream, XWPFDocument.PICTURE_TYPE_PNG);
        } else {
            document.addPictureData(byteInputStream, XWPFDocument.PICTURE_TYPE_JPEG);
        }
        //创建一个word图片,并插入到文档中-->像素可改
        document.createPicture(document.getAllPictures().size() - 1, 240, 240, paragraphs.get(paragraphs.size() - 1));
        System.out.println(document.getAllPictures().get(0));
        OutputStream out = Files.newOutputStream(Paths.get(filePath));
        document.write(out);
        out.flush();
        out.close();

    }


}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用中的描述,直接在Word中插入SVG格式的图片可以保持清晰度,但是导出为PDF后会变得模糊。然而,根据引用中的描述,使用Inkscape转换为EMF文件也可能会导致颜色变化。因此,为了实现WordPDF的无损转换,可能需要采取以下步骤: 1. 将Word文档保存为PDF格式:在Word中,选择文件->另存为->PDF格式。这样可以确保原始文档的格式和布局得以保留。 2. 使用专业的PDF转换工具:寻找并使用可信的专业PDF转换工具,可以确保将Word文档转换为PDF时保持无损。这些工具通常提供更高质量的转换和更好的图像保留性能,以确保生成的PDF文件与原始Word文档保持一致。 3. 考虑使用其他格式:如果WordPDF的无损转换仍然存在问题,您可以尝试将Word文档保存为其他格式,如PNG或TIFF,然后使用专业的图像编辑软件将其转换为PDF格式。这样可以确保图像保持清晰度,并且不会出现颜色变化或模糊。 总结起来,为了实现WordPDF的无损转换,您可以尝试在Word中直接保存为PDF格式,或使用专业的PDF转换工具进行转换。如果问题仍然存在,您可以考虑将Word文档保存为其他图像格式,然后使用图像编辑软件将其转换为PDF。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [如何让word中清晰的图片无损导出为pdf?](https://blog.csdn.net/weixin_43820665/article/details/121877663)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [PDFWord(互转)](https://download.csdn.net/download/leuxan110/12896931)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值