最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot各種格式轉(zhuǎn)pdf的實(shí)例代碼

 更新時(shí)間:2021年01月06日 10:51:38   作者:兔老大的胡蘿卜  
這篇文章主要介紹了springboot各種格式轉(zhuǎn)pdf的實(shí)例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

添加依賴(lài)

<!--轉(zhuǎn)pdf-->
    <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>com.itextpdf</groupId>
      <artifactId>itextpdf</artifactId>
      <version>5.5.10</version>
    </dependency>

測(cè)試方法

package com.ruoyi.mlogin.util;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.*;
import java.net.MalformedURLException;

/**
 * @author cai
 * @version 1.0
 * @date 2021/1/4 14:58
 */
public class Topdf {


  /**
   * 轉(zhuǎn)pdf doc docx xls xlsx
   * @param path
   */
  public void docTopdf(String path) {

    File inputWord = new File("C:\\Users\\29934\\Documents\\Tencent Files\\2993481541\\FileRecv\\1111.docx");
    File outputFile = new File("C:\\Users\\29934\\Documents\\Tencent Files\\2993481541\\FileRecv\\1111.pdf");
    try {
      InputStream docxInputStream = new FileInputStream(inputWord);
      OutputStream outputStream = new FileOutputStream(outputFile);
      IConverter converter = LocalConverter.builder().build();
      String fileTyle=path.substring(path.lastIndexOf("."),path.length());//獲取文件類(lèi)型
      if(".docx".equals(fileTyle)){
        converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
      }else if(".doc".equals(fileTyle)){
        converter.convert(docxInputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
      }else if(".xls".equals(fileTyle)){
        converter.convert(docxInputStream).as(DocumentType.XLS).to(outputStream).as(DocumentType.PDF).execute();
      }else if(".xlsx".equals(fileTyle)){
        converter.convert(docxInputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();
      }
      outputStream.close();
      System.out.println("pdf轉(zhuǎn)換成功");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }


  /**
   *
   *      生成pdf文件
   *      需要轉(zhuǎn)換的圖片路徑的數(shù)組
   */
  public static void main(String[] args) {
    try {
      String imagesPath = "C:\\Users\\29934\\Documents\\Tencent Files\\2993481541\\FileRecv\\1111.jpg";
      File file = new File("C:\\Users\\29934\\Documents\\Tencent Files\\2993481541\\FileRecv\\1111.pdf");
      // 第一步:創(chuàng)建一個(gè)document對(duì)象。
      Document document = new Document();
      document.setMargins(0, 0, 0, 0);
      // 第二步:
      // 創(chuàng)建一個(gè)PdfWriter實(shí)例,
      PdfWriter.getInstance(document, new FileOutputStream(file));
      // 第三步:打開(kāi)文檔。
      document.open();
      // 第四步:在文檔中增加圖片。
      if (true) {
        Image img = Image.getInstance(imagesPath);
        img.setAlignment(Image.ALIGN_CENTER);
        // 根據(jù)圖片大小設(shè)置頁(yè)面,一定要先設(shè)置頁(yè)面,再newPage(),否則無(wú)效
        document.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
        document.newPage();
        document.add(img);
        //下面是對(duì)應(yīng)一個(gè)文件夾的圖片
//      File files = new File(imagesPath);
//      String[] images = files.list();
//      int len = images.length;
//
//      for (int i = 0; i < len; i++)
//      {
//        if (images[i].toLowerCase().endsWith(".bmp")
//            || images[i].toLowerCase().endsWith(".jpg")
//            || images[i].toLowerCase().endsWith(".jpeg")
//            || images[i].toLowerCase().endsWith(".gif")
//            || images[i].toLowerCase().endsWith(".png")) {
//          String temp = imagesPath + "\\" + images[i];
//          Image img = Image.getInstance(temp);
//          img.setAlignment(Image.ALIGN_CENTER);
//          // 根據(jù)圖片大小設(shè)置頁(yè)面,一定要先設(shè)置頁(yè)面,再newPage(),否則無(wú)效
//          document.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
//          document.newPage();
//          document.add(img);
//        }
//      }
        // 第五步:關(guān)閉文檔。
        document.close();
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (BadElementException e) {
      e.printStackTrace();
    } catch (DocumentException e) {
      e.printStackTrace();
    }
  }

}

補(bǔ)充:下面看下springboot:擴(kuò)展類(lèi)型轉(zhuǎn)換器

需求:提交一個(gè)字符串到后端的java.sql.Time類(lèi)型,就報(bào)錯(cuò)了:

Failed to convert property value of type [java.lang.String] to required type [java.sql.Time]

正常提交到j(luò)ava.util.Date類(lèi)型是沒(méi)有問(wèn)題的。

所以這里就需要擴(kuò)展內(nèi)置的springmvc的轉(zhuǎn)換器

代碼如下:

WebConfig : 添加新的類(lèi)型轉(zhuǎn)換器

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import com.csget.web.converter.StringToTimeConverter;

@Configuration
public class WebConfig {

 @Autowired
 private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

 @PostConstruct
 public void addConversionConfig() {
  ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) requestMappingHandlerAdapter
    .getWebBindingInitializer();
  if (initializer.getConversionService() != null) {
   GenericConversionService genericConversionService = (GenericConversionService) initializer.getConversionService();
   genericConversionService.addConverter(new StringToTimeConverter());
  }
 }
}

StringToTimeConverter :類(lèi)型轉(zhuǎn)換器的具體實(shí)現(xiàn)

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;

public class StringToTimeConverter implements Converter<String, Time> {
 public Time convert(String value) {
  Time time = null;
  if (StringUtils.isNotBlank(value)) {
   String strFormat = "HH:mm";
   int intMatches = StringUtils.countMatches(value, ":");
   if (intMatches == 2) {
    strFormat = "HH:mm:ss";
   }
   SimpleDateFormat format = new SimpleDateFormat(strFormat);
   Date date = null;
   try {
    date = format.parse(value);
   } catch (Exception e) {
    e.printStackTrace();
   }
   time = new Time(date.getTime());
  }
  return time;
 }

}

到此這篇關(guān)于springboot各種格式轉(zhuǎn)pdf的文章就介紹到這了,更多相關(guān)springboot格式轉(zhuǎn)pdf內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何提高java代碼的重用性

    如何提高java代碼的重用性

    在本篇文章中小編給各位分享了關(guān)于如何提高java代碼的重用性的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們參考下。
    2019-07-07
  • Java Spring @Autowired的這些騷操作,你都知道嗎

    Java Spring @Autowired的這些騷操作,你都知道嗎

    這篇文章主要介紹了徹底搞明白Spring中的自動(dòng)裝配和Autowired注解的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-09-09
  • Java定時(shí)器通信協(xié)議管理模塊Timer詳解

    Java定時(shí)器通信協(xié)議管理模塊Timer詳解

    這篇文章主要介紹了Java定時(shí)器通信協(xié)議管理模塊Timer,?Timer一般指定時(shí)器(通信協(xié)議管理模塊)人類(lèi)最早使用的定時(shí)工具是沙漏或水漏,但在鐘表誕生發(fā)展成熟之后,人們開(kāi)始嘗試使用這種全新的計(jì)時(shí)工具來(lái)改進(jìn)定時(shí)器,達(dá)到準(zhǔn)確控制時(shí)間的目的
    2022-08-08
  • Java實(shí)現(xiàn)超級(jí)實(shí)用的日記本

    Java實(shí)現(xiàn)超級(jí)實(shí)用的日記本

    一個(gè)用Java語(yǔ)言編寫(xiě)的,實(shí)現(xiàn)日記本的基本編輯功能、各篇日記之間的上下翻頁(yè)、查詢(xún)?nèi)沼泝?nèi)容的程序。全部代碼分享給大家,有需要的小伙伴參考下。
    2015-05-05
  • 詳解SpringBoot自動(dòng)配置源碼

    詳解SpringBoot自動(dòng)配置源碼

    今天帶大家來(lái)分析SpringBoot自動(dòng)配置源碼,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06
  • SpringBoot項(xiàng)目yml配置文件不自動(dòng)提示解決方案

    SpringBoot項(xiàng)目yml配置文件不自動(dòng)提示解決方案

    這篇文章主要介紹了SpringBoot項(xiàng)目配置文件.yaml/.yml文件編寫(xiě)時(shí)沒(méi)有自動(dòng)提示的解決方案,文章通過(guò)圖文結(jié)合的方式給大家講解的非常詳細(xì),需要的朋友可以參考下
    2024-06-06
  • springmvc項(xiàng)目使用@Valid+BindingResult遇到的問(wèn)題

    springmvc項(xiàng)目使用@Valid+BindingResult遇到的問(wèn)題

    這篇文章主要介紹了springmvc項(xiàng)目使用@Valid+BindingResult遇到的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 詳解Java并發(fā)編程之volatile關(guān)鍵字

    詳解Java并發(fā)編程之volatile關(guān)鍵字

    這篇文章主要為大家介紹了Java并發(fā)編程之volatile關(guān)鍵字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-11-11
  • Spring-Security實(shí)現(xiàn)登錄接口流程

    Spring-Security實(shí)現(xiàn)登錄接口流程

    Security?是?Spring?家族中的一個(gè)安全管理框架,SpringSecurity的原理其實(shí)就是一個(gè)過(guò)濾器鏈,內(nèi)部包含了提供各種功能的過(guò)濾器,這篇文章主要介紹了Spring-Security實(shí)現(xiàn)登錄接口,需要的朋友可以參考下
    2023-05-05
  • Android開(kāi)發(fā)Kotlin實(shí)現(xiàn)圓弧計(jì)步器示例詳解

    Android開(kāi)發(fā)Kotlin實(shí)現(xiàn)圓弧計(jì)步器示例詳解

    這篇文章主要為大家介紹了Android開(kāi)發(fā)Kotlin繪制圓弧計(jì)步器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06

最新評(píng)論

沅江市| 新乡市| 清水县| 朝阳区| 佳木斯市| 赤壁市| 龙胜| 五原县| 搜索| 尼勒克县| 巧家县| 茂名市| 新昌县| 施秉县| 肃北| 容城县| 永春县| 宾川县| 长岭县| 毕节市| 印江| 蒙城县| 湟源县| 凤冈县| 乐安县| 赤水市| 纳雍县| 保康县| 托里县| 利川市| 武邑县| 达州市| 嘉黎县| 工布江达县| 行唐县| 眉山市| 湘潭县| 沂水县| 平和县| 龙陵县| 新化县|