MultipartFile 格式图片转pdf


public File ImageToPdf(MultipartFile file) throws IOException, DocumentException {
    String fileName = file.getOriginalFilename();
    String pdfFileName = fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf";
    Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
    PdfWriter.getInstance(doc, new FileOutputStream(pdfFileName));
    doc.open();
    doc.newPage();
    Image image = Image.getInstance(file.getBytes());
    float height = image.getHeight();
    float width = image.getWidth();
    int percent = getPercent(height, width);
    image.setAlignment(Image.MIDDLE);
    image.scalePercent(percent);
    doc.add(image);
    doc.close();
    File pdfFile = new File(pdfFileName);
    return pdfFile;
}

/**
 * @Description: 等比压缩,获取压缩百分比
 * @param height 图片的高度
 * @param weight 图片的宽度
 * @return 压缩百分比
 */
private static int getPercent(float height, float weight) {
    float percent = 0.0F;
    if (height > weight) {
        percent = PageSize.A4.getHeight() / height * 100;
    } else {
        percent = PageSize.A4.getWidth() / weight * 100;
    }
    return Math.round(percent);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Apache POI库将docx文件换为pdf。以下是一个使用MultipartFile对象的示例代码: ```java import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.apache.poi.xwpf.usermodel.XWPFPicture; import org.apache.poi.xwpf.usermodel.XWPFPictureData; import org.apache.poi.xwpf.converter.pdf.PdfConverter; import org.apache.poi.xwpf.converter.pdf.PdfOptions; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class DocxToPdfConverter { public static void convert(MultipartFile docxFile, String pdfFilePath) throws IOException { XWPFDocument document = new XWPFDocument(docxFile.getInputStream()); PdfOptions options = PdfOptions.create(); FileOutputStream pdfOutputStream = new FileOutputStream(new File(pdfFilePath)); PdfConverter.getInstance().convert(document, pdfOutputStream, options); document.close(); pdfOutputStream.close(); } } ``` 您需要在项目中添加以下依赖项,以使用Apache POI和相关的PDF换器: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.converter.pdf</artifactId> <version>2.0.1</version> </dependency> ``` 请确保您已正确配置您的项目以使用这些依赖项,并按照您的需求调整代码。此示例假设您已提供docx文件的MultipartFile对象以及pdf文件的输出路径。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值