How to import the jar package into the project in IDEA

The big job is to generate a pdf. After searching for information, it is found that it can be called through pdfbo related functions, but this file is missing locally. Take this file as an example.

1. Download

Download to Apache to download, Apache PDFBox | Download , just download with your own java version.

I am jdk1.8, I downloaded 2.0.28, address: Apache Downloads . What library is missing can be downloaded on apache.

2. Deployment

Enter IDEA, right-click the root directory, you can create a folder lib specially used to store jar packages.

 Save the downloaded jar file under lib, there is no small triangle on the left at the beginning

 Then click File in the upper left corner, and then click project structure

 Click Module, then click Dependencies, and then click the + sign on the right; click JARS or... that, ready to import the jar package

 Import the jar package, choose the path of your own file, and select the jar under lib.

 After the loading is complete, check it, click apply in the lower right corner, and finally click ok to exit.

 3. Test

You can write a code to test it

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import java.io.IOException;

public class test_pdf {

    public static void main(String[] args) {
        String storyboardContent = "This is a sample storyboard content.";

        try {
            // 创建PDF文档
            PDDocument document = new PDDocument();
            PDPage page = new PDPage();
            document.addPage(page);

            // 创建页面内容流
            PDPageContentStream contentStream = new PDPageContentStream(document, page);

            // 设置字体和字号
            contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);

            // 添加文本内容到页面
            contentStream.beginText();
            contentStream.newLineAtOffset(25, 700); // 设置文本起始位置
            contentStream.showText(storyboardContent);
            contentStream.endText();

            // 关闭内容流
            contentStream.close();

            // 保存PDF文件
            document.save("storyboard.pdf");

            // 关闭PDF文档
            document.close();

            System.out.println("PDF created successfully.");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 It was successfully generated (I dragged it to the desktop), indicating that there is no problem

Guess you like

Origin blog.csdn.net/m0_62237233/article/details/130774082