2018년 8월 31일 금요일

[Java] PDF Stamp


1. PDF Stamp Text with Apache PDFBox 2.0.x
1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.awt.Color;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class PdfStamp {

    PDDocument document;
    
    public PdfStamp() {
    }
    
    public PdfStamp(String fileName) throws InvalidPasswordException, IOException {
        load(fileName);
    }
    
    public void load(String fileName) throws InvalidPasswordException, IOException {
        document = PDDocument.load(new File(fileName));
    }
    
    public void close() throws IOException {
        document.close();
    }
    
    public void save(String fileName) {
        try {
            this.document.save(fileName);
            this.document.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }   
    
    public PDFont loadFont(String fontFile) {
        PDFont font = PDType1Font.HELVETICA_BOLD; 
        if( new File(fontFile).exists() ) {
            try {
                return PDType0Font.load(document, new File(fontFile));
            } catch (IOException e) {
            }
        }
        return font;
    }
        
    private int convY(PDPage page, int y) {
        return (int)page.getMediaBox().getHeight() - y - 1;
    }
    
    public int getTextWidth( String text, PDFont font, int fontSize ) {
        try {
            return (int)((font.getStringWidth(text)/1000)*fontSize);
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
    }
    
    public void drawText( PDPage page, int x, int y, String text, Color color, Color bgColor, PDFont font, int fontSize ) throws IOException {
        if( font == null ) {
            font = PDType1Font.HELVETICA;
        }
        y += fontSize * 1.5;
        if( bgColor != null ) {
            int width = getTextWidth(text, font, fontSize);
            drawRect( page, new Rectangle(x,y+3,width,fontSize), bgColor, true );
        }
        PDPageContentStream stream = new PDPageContentStream( this.document, page, AppendMode.APPEND, true );
        stream.beginText();
        stream.setFont(font, fontSize);
        stream.setNonStrokingColor( color.getRed(), color.getGreen(), color.getBlue());
        stream.newLineAtOffset( x, convY(page,y) );
        stream.showText(text);
        stream.endText(); 
        stream.close();
    }
    
    public void drawText( PDPage page, int x, int y, String text, Color color ) throws IOException {
        drawText( page, x, y, text, color, null, PDType1Font.HELVETICA_BOLD, 12);
    }
    
    public void drawLine( PDPage page, int x1, int y1, int x2, int y2, int width, Color color ) {
        try {
            PDPageContentStream stream = new PDPageContentStream( this.document, page, AppendMode.APPEND, true );
            stream.setLineWidth(width);
            stream.setStrokingColor( color.getRed(), color.getGreen(), color.getBlue());
            stream.moveTo( x1, convY(page, y1) );
            stream.lineTo( x2, convY(page, y2) );
            stream.stroke();
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }  
    }
    
    public void drawRect( PDPage page, Rectangle rect, Color color, boolean fill) throws IOException {
        PDPageContentStream stream = new PDPageContentStream( this.document, page, AppendMode.APPEND, true );
        stream.addRect(rect.x, convY(page,(int)rect.y), rect.width, rect.height);
        if (fill) {
            stream.setNonStrokingColor(color);
            stream.fill();
        } else {
            stream.setStrokingColor(color);
            stream.stroke();
        }
        stream.close();
    }
    
    public void stampTextAllPages(String fileName, int x, int y, String text, Color color, Color bgColor) throws IOException {
        load(fileName);
        PDFont font = loadFont("malgunbd.ttf");
        int fontSize = 12;
        for( PDPage page : this.document.getPages() ) {         
            drawText( page, x, y, text, color, bgColor, font, fontSize );
        }
    }
    
    public static void main(String[] args) throws IOException {
        PdfTools pdf = new PdfTools();
        pdf.stampTextAllPages("D:/document.pdf",20,20,"[Confidential]",Color.RED,Color.YELLOW);
        pdf.save("D:/stamp.pdf");
    }
}




Input & output:





[Java] PDF to Image Convert


1. PDF To Image Convert with Apache PDFBox 2.0.x


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;

public class PdfTools {

    PDDocument document;
    
    public PdfTools() {
    }
    
    public PdfTools(String fileName) throws InvalidPasswordException, IOException {
        load(fileName);
    }
    
    public void load(String fileName) throws InvalidPasswordException, IOException {
        document = PDDocument.load(new File(fileName));
    }
    
    public void close() throws IOException {
        document.close();
    }
    
    public void convertPagesToPng(File folder) throws IOException {
        if( folder.exists() == false ) {
            folder.mkdirs();
        }
        PDFRenderer pdfRenderer = new PDFRenderer(document);
        for( int page = 0; page < document.getNumberOfPages(); page++ ) { 
            BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);   
            ImageIOUtil.writeImage( bim, 
                String.format( folder + "/image_%04d.png", page), 300);
        }
    }
    
    public void convertPagesToJpeg(File folder) throws IOException {
        if( folder.exists() == false ) {
            folder.mkdirs();
        }
        PDFRenderer pdfRenderer = new PDFRenderer(document);
        for( int page = 0; page < document.getNumberOfPages(); page++ ) { 
            BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);   
            ImageIOUtil.writeImage( bim, 
                String.format( folder + "/image_%04d.jpg", page), 300);
        }
    }

    public static void main(String[] args) throws IOException {
        PdfTools pdfTools = new PdfTools("D:/Ebook/python.pdf");
        pdfTools.convertPagesToPng(new File("D:/Temp/page_png"));
        pdfTools.convertPagesToJpeg(new File("D:/Temp/page_jpg"));
    }
}

2018년 8월 30일 목요일

[Java] PDF Image Extract


1. PDF Image Extract with Apache PDFBox 2.0.x

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

public class PdfTools {

    PDDocument document;
    
    public PdfTools() {
    }
    
    public PdfTools(String fileName) throws InvalidPasswordException, IOException {
        load(fileName);
    }
    
    public void load(String fileName) throws InvalidPasswordException, IOException {
        document = PDDocument.load(new File(fileName));
    }
    
    public void extractImagesToPng(File folder) throws IOException {
        if( folder.exists() == false ) {
            folder.mkdirs();
        }
        List<RenderedImage> images = getAllImages();
        for( int i = 0; i < images.size(); i++ ) {
            ImageIO.write( images.get(i), "png", 
                new File(folder, String.format( "image_%04d.png", i)));
        }
    }
    
    public void extractImagesToJpeg(File folder) throws IOException {
        if( folder.exists() == false ) {
            folder.mkdirs();
        }
        List<RenderedImage> images = getAllImages();
        for( int i = 0; i < images.size(); i++ ) {
            ImageIO.write( images.get(i), "jpeg", 
                new File(folder, String.format( "image_%04d.jpg", i)));
        }
    }
    
    public List<RenderedImage> getAllImages() throws IOException {
        List<RenderedImage> images = new ArrayList<>();
        for( PDPage page : document.getPages() ) {
            images.addAll(getImagesFromResources(page.getResources()));
        }
        return images;
    }
    
    private List<RenderedImage> getImagesFromResources(PDResources resources) throws IOException {
        List<RenderedImage> images = new ArrayList<>();
        for( COSName name : resources.getXObjectNames() ) {
            PDXObject obj = resources.getXObject(name);
            if (obj instanceof PDFormXObject) {
                images.addAll(getImagesFromResources(((PDFormXObject) obj).getResources()));
            } else if (obj instanceof PDImageXObject) {
                images.add(((PDImageXObject) obj).getImage());
            }
        }
        return images;
    }

    public static void main(String[] args) throws InvalidPasswordException, IOException {
        PdfTools pdfTools = new PdfTools("D:/Ebook/python.pdf");
        pdfTools.extractImagesToPng(new File("D:/Temp/png"));
        pdfTools.extractImagesToJpeg(new File("D:/Temp/jpg"));
    }
}