package com.melon;

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

import java.io.IOException;

/**
 * Hello world!
 */
public class PdfDemo {

    @Test
    public void test001() throws IOException {
        //E:\Repository\Git\melon\melon-sample-pdf\src\main\resources\HTTP权威指南.pdf
        System.out.println("Hello World!");

        PDDocument pdDocument = new PDDocument();
        PDPage pdPage = new PDPage();
        pdDocument.addPage(pdPage);

        PDFont pdFont = PDType1Font.HELVETICA_BOLD;

        PDPageContentStream contentStream = new PDPageContentStream(pdDocument, pdPage);
        contentStream.beginText();
        contentStream.setFont(pdFont, 14);
        contentStream.newLineAtOffset(100, 700);
        contentStream.showText("Hello World");
        contentStream.endText();
        contentStream.close();

        String directory = PdfDemo.class.getClassLoader().getResource("").getPath();
        String fileName = "text.pdf";

        pdDocument.save(directory + fileName);
        pdDocument.close();
    }
}