Java – iText Write/Read PDF File Example – PdfReader + PdfWriter
Java
207
iText
2
PdfReader
1
PdfWriter
1
Male avatar

loveprogramming viết ngày 04/05/2021

https://grokonez.com/java/java-itext-write-read-pdf-file-example

Java – iText Write/Read PDF File Example – PdfReader + PdfWriter

In the tutorial, we show how to Write/Read PDF File with iText library.

Create Maven Project

We create a Maven project with iText dependency:

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

Project structure:

Java-iText-Write-Read-PDF-File-project-structure

Write Text to PDF with iText

We use PdfWriter to write text to PDF file, WriteTextToPdfFile.java ->


package iTextJavaReadWriteTextPdfFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class WriteTextToPdfFile {

    private static final String FILE_PATH_NAME = "./src/main/resources/grokonez-itext.pdf";
    
    public static void main(String[] args) {
        writeTextToPdfFile();
    }
    
    private static void writeTextToPdfFile() {
        Document document = new Document();

        try {

            PdfWriter.getInstance(document, new FileOutputStream(new File(FILE_PATH_NAME)));

            //open
            document.open();
            
            // Paragraph 1 ->
            Paragraph p = new Paragraph("This is a paragraph 1",
                                            FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC));

            document.add(p);

            // Paragraph 2 ->
            Paragraph p2 = new Paragraph();
            p2.add("This is a paragraph 2");
            p2.setAlignment(Element.ALIGN_CENTER);
            
            document.add(p2);

            // Paragraph 3 ->
            Font f = new Font();
            f.setStyle(Font.BOLD);
            f.setSize(30);
            f.setColor(255, 0, 0);
            
            Paragraph p3 = new Paragraph("This is a paragraph 3", f);
            p3.setAlignment(Element.ALIGN_RIGHT);

            document.add(p3);
            
            // Paragraph 4 ->
            Paragraph p4 = new Paragraph("Grokonez",
                                            FontFactory.getFont(FontFactory.HELVETICA, 250, Font.BOLDITALIC));
            document.add(p4);
                    
            // Finish task ->
            document.close();
            
            System.out.println("Finish!");
         
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

We have a grokonez-itext.pdf file with 2 pages ->

Page 1:

Java-iText-Write-Read-PDF-File-write-to-pdf-page-1

https://grokonez.com/java/java-itext-write-read-pdf-file-example

Java – iText Write/Read PDF File Example – PdfReader + PdfWriter

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

451 bài viết.
79 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 5 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/springframework/springboot/kotlinspringboot/itextpdfkotlinspringbootrestapimysql iText PDF + Kotlin SpringBoot RestAPI + MySQ...
loveprogramming viết 1 tháng trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
451 bài viết.
79 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!