原创

itext7操作pdf

1、引入pom

<itextpdf.version>7.1.9</itextpdf.version>

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>kernel</artifactId>
    <version>${itextpdf.version}</version>
</dependency>

<!-- always needed -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>io</artifactId>
    <version>${itextpdf.version}</version>
</dependency>

<!-- always needed -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>layout</artifactId>
    <version>${itextpdf.version}</version>
</dependency>

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>forms</artifactId>
    <version>${itextpdf.version}</version>
</dependency>

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>font-asian</artifactId>
    <version>${itextpdf.version}</version>
</dependency>

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>html2pdf</artifactId>
    <version>${itexthtml2pdf.version}</version>
</dependency>

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-licensekey</artifactId>
    <version>3.0.5</version>
</dependency>

2、基本的使用demo

package net.gzcx.pdf;

import com.google.common.collect.Lists;
import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.Style;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import org.junit.Test;

/**
 * 功能说明:测试itext
 *
 * @author 
 * @since 2023-10-31 11:35
 */
public class ItextTest {

    /**
     * 测试pdf
     */
    @Test
    public void testItext() throws IOException {
// Creating a PdfWriter
        String dest = "D:/fengjiu.pdf";
        PdfWriter writer = new PdfWriter(dest);

        // Creating a PdfDocument
        PdfDocument pdf = new PdfDocument(writer);

        List<Float> fontSize = Lists.newArrayList(5F, 5.5F, 6.5F, 7.5F, 8F, 9F, 10F, 10.5F, 11F,
            12F, 14F, 15F, 16F, 18F, 20F, 22F, 24F, 26F, 28F, 36F, 42F, 48F, 56F, 72F);

        // Creating a Document
        Document document = new Document(pdf);
        String str = "我是来测试的%s";

        // Setting font of the text
        PdfFont font = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", true);


        fontSize.stream().forEach(size -> {
            String content = String.format(str, size.toString());
            Style style = new Style();
            // 设置字体和字体大小
            style.setFontSize(size);

            Text text = new Text(content);
            text.setFont(font);
            text.setFontSize(size);

            // Creating Paragraphs
            Paragraph paragraph = new Paragraph(text);

            paragraph.addStyle(style);
            paragraph.setBorder(new SolidBorder(0.2f));
            paragraph.setFixedLeading(size);
            // Adding paragraphs to document
            document.add(paragraph);
        });

        // Closing the document
        document.close();
        System.out.println("Paragraph added");
    }
}

3、更多的参考

https://www.tutorialspoint.com/itext/itext_adding_paragraph.htm

正文到此结束
本文目录