java realizes picture to pdf file

Happy New Year 2021! I wish everyone no bugs in the code~~~

Since I am a resident developer, some time ago I asked for a PDF file of a scanned copy of the certificate. I thought, now that it costs money to convert the PDF file, is this something I can bear! ! !

The answer is, of course, no. How can my poor and low-ranking let me spend this money~~~ Suddenly my brain lit up, and I cut a picture and directly converted the picture into a pdf file. Isn’t it all done? Just do it:

Today, I will record a small knowledge point, mainly to convert pictures into pdf documents.
First introduce dependencies:

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.13</version>
    </dependency>

You can see and use the dependent version for yourself, and you haven't done much to understand it.
code show as below:

public static void main(String[] arg){
      imgOfPdf();
}
public static void imgOfPdf() {
	  try {
	  		//创建个存放图片地址的集合
	       List<String> imageUrlList = new ArrayList();
	       //添加图片地址到集合
	       imageUrlList.add("C:\\Users\\Lenovo\\Pictures\\Saved Pictures\\123.jpg");
	       //存放pdf文件的路径
	       String pdfUrl = "D:\\123.pdf";
	       File file = PdfUtilImg.pdf(imageUrlList, pdfUrl);//生成pdf
	       file.createNewFile();
	   } catch (IOException e) {
	       e.printStackTrace();
	   }
}

In the above code, the collection and storage pdf addresses can be passed in as parameters. Here I wrote it directly, so there is no need to check whether the picture exists. The verification step can be completed before using this method.

OK, next is our main course:

public static File pdf(List<String> imageUrllist, String pdfUrl) {
	//new一个pdf文档
    Document doc = new Document(PageSize.A4, 20, 20, 20, 20); 
    try {
        //pdf写入
        PdfWriter.getInstance(doc, new FileOutputStream(pdfUrl));
        //打开文档
        doc.open();
        //遍历集合,将图片放在pdf文件
        for (int i = 0; i < imageUrllist.size(); i++) {
        	//在pdf创建一页   主:此处为每一张图片是pdf文件的一页
            doc.newPage();  
            //通过文件路径获取image
            Image png1 = Image.getInstance(imageUrllist.get(i));
            float heigth = png1.getHeight();
            float width = png1.getWidth();
            int percent = getPercent2(heigth, width);
            png1.setAlignment(Image.MIDDLE);
            // 表示是原来图像的比例;
            png1.scalePercent(percent+3);
            doc.add(png1);
        }
        doc.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
	//输出流
    File mOutputPdfFile = new File(pdfUrl); 
    if (!mOutputPdfFile.exists()) {
        mOutputPdfFile.deleteOnExit();
        return null;
    }
    //反回文件输出流
    return mOutputPdfFile;
}

public static int getPercent(float h, float w) {
    int p = 0;
    float p2 = 0.0f;
    if (h > w) {
        p2 = 297 / h * 100;
    } else {
        p2 = 210 / w * 100;
    }
    p = Math.round(p2);
    return p;
}

public static int getPercent2(float h, float w) {
    int p = 0;
    float p2 = 0.0f;
    p2 = 530 / w * 100;
    p = Math.round(p2);
    return p;
}

This is the end of the code, to show you the effect of the conversion:
Insert picture description here

This article is borrowed from: https://www.cnblogs.com/sky-zky/p/9639256.html

Guess you like

Origin blog.csdn.net/xaiobaicai/article/details/112182147