JAvaはitextを用いてpdfを生成する

64690 ワード

ここでは、自分が実践したjavaを使用してpdfの生成を実現し、多くの資料を参考にしています.
開始前にパッケージを導入する必要があります.
 
        
            com.itextpdf
            itextpdf
            5.5.13
        


        
            com.itextpdf
            itext-asian
            5.2.0
        

        
        
            com.itextpdf.tool
            xmlworker
            5.5.13
        

        
        
            org.bouncycastle
            bcprov-jdk15on
            1.60
        

 
 
demo:
package com.jack.studytest.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
import com.itextpdf.text.pdf.draw.LineSeparator;
import com.itextpdf.text.pdf.draw.VerticalPositionMark;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Created By Jack on 2018/8/17
 *
 * @author Jack
 * @date 2018/8/17 15:29
 * @Description:
 * pdf  
 */

public class PdfTest1 {

    /**
     *    :
     *        PDF
     * @param fileUrl
     * @throws Exception
     */
    public static void createSimplePdf(String fileUrl) throws  Exception{
        System.out.println("begin       pdf  :fileUrl="+fileUrl);
        //Step 1—Create a Document.  Document
        Document document = new Document();
        //Step 2—Get a PdfWriter instance.       
        PdfWriter.getInstance(document, new FileOutputStream(fileUrl));
        //Step 3—Open the Document.    
        document.open();
        //Step 4—Add content.  pdf   
        document.add(new Paragraph("Hello World"));
        //Step 5—Close the Document.    
        document.close();
        System.out.println("end       pdf  ");
    }

    /**
     *   2:
     *     ,     ,    ,Title,Author,Subject,Keywords
     * @param fileUrl
     */
    public static void createTest2Pdf(String fileUrl) throws Exception {
        System.out.println("begin");
        //    
        Rectangle rect = new Rectangle(PageSize.B5.rotate());
        //Rectangle rect = new Rectangle(PageSize.A4.rotate());
        //     
        rect.setBackgroundColor(BaseColor.ORANGE);
        Document doc = new Document(rect);
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(fileUrl));
        //PDF  (  1.7)
        writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
        //    
        doc.addTitle("Title@sample");
        doc.addAuthor("Author@rensanning");
        doc.addSubject("Subject@iText sample");
        doc.addKeywords("Keywords@iText");
        doc.addCreator("Creator@iText");
        //    
        doc.setMargins(10, 20, 30, 40);
        doc.open();
        doc.add(new Paragraph("Hello World"));
        doc.close();
        writer.close();
        System.out.println("end");
    }

    /**
     *   3:
     *   pdf     
     * @param fileUrl
     */
    public static void createPdfWithPassword(String fileUrl) throws Exception {
        System.out.println("begin");
        Document doc = new Document();
        FileOutputStream out = new FileOutputStream(fileUrl);
        PdfWriter writer = PdfWriter.getInstance(doc, out);
        //      :"jack"
        writer.setEncryption("Hello".getBytes(), "jack".getBytes(),
                PdfWriter.ALLOW_SCREENREADERS,
                PdfWriter.STANDARD_ENCRYPTION_128);
        doc.open();
        doc.add(new Paragraph("Hello World"));
        out.close();
        doc.close();
        System.out.println("end");
    }

    /**
     *  4:
     *   Page
     * @param fileUrl
     */
    public static void createPdfAddPage(String fileUrl)throws Exception{
        System.out.println("begin");
        Document document = new Document();
        FileOutputStream out = new FileOutputStream(fileUrl);
        PdfWriter writer = PdfWriter.getInstance(document, out);
        document.open();
        //   
        document.add(new Paragraph("First page"));
        document.add(new Paragraph("first page content"));
        //     
        document.newPage();
        writer.setPageEmpty(false);
        //   
        document.newPage();
        document.add(new Paragraph("New page"));
        document.close();
        System.out.println("end");
    }


    /**
     *   5:
     *     (   )
     * @param fileUrl
     */
    public static void createPdfBgImage(String fileUrl)throws Exception{
        System.out.println("begin");
        //    
        PdfReader reader = new PdfReader(fileUrl);
        PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("E:\\mystudy\\pdf\\api\\setWatermark2.pdf"));

        Image img = Image.getInstance("E:\\mystudy\\pdf\\api\\mypic.jpg");
        img.setAbsolutePosition(200, 400);
        PdfContentByte under = stamp.getUnderContent(1);
        under.addImage(img);

        //    
        PdfContentByte over = stamp.getOverContent(2);
        over.beginText();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,
                BaseFont.EMBEDDED);
        over.setFontAndSize(bf, 18);
        over.setTextMatrix(30, 30);
        over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE", 230, 430, 45);
        over.endText();

        //   
        Image img2 = Image.getInstance("E:\\mystudy\\pdf\\api\\bg-login-hk.jpg");
        img2.setAbsolutePosition(0, 0);
        PdfContentByte under2 = stamp.getUnderContent(3);
        under2.addImage(img2);
        stamp.close();
        reader.close();

        System.out.println("end");
    }

    /**
     *   Chunk, Phrase, Paragraph, List
     * @param fileUrl
     */
    public static void createPdfTest6(String fileUrl)throws Exception{
        System.out.println("begin...........");
        //Chunk  : a String, a Font, and some attributes
        Document document = new Document();
        //FileOutputStream out = new FileOutputStream(fileUrl);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl));
        document.open();
        document.add(new Chunk("China"));
        document.add(new Chunk(" "));
        Font font = new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE);
        Chunk id = new Chunk("chinese", font);
        id.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);
        id.setTextRise(6);
        document.add(id);
        document.add(Chunk.NEWLINE);

        document.add(new Chunk("Japan"));
        document.add(new Chunk(" "));
        Font font2 = new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE);
        Chunk id2 = new Chunk("japanese", font2);
        id2.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);
        id2.setTextRise(6);
        id2.setUnderline(0.2f, -2f);
        document.add(id2);
        document.add(Chunk.NEWLINE);

        //Phrase  : a List of Chunks with leading
        document.newPage();
        document.add(new Phrase("Phrase page"));

        Phrase director = new Phrase();
        Chunk name = new Chunk("China");
        name.setUnderline(0.2f, -2f);
        director.add(name);
        director.add(new Chunk(","));
        director.add(new Chunk(" "));
        director.add(new Chunk("chinese"));
        director.setLeading(24);
        document.add(director);

        Phrase director2 = new Phrase();
        Chunk name2 = new Chunk("Japan");
        name2.setUnderline(0.2f, -2f);
        director2.add(name2);
        director2.add(new Chunk(","));
        director2.add(new Chunk(" "));
        director2.add(new Chunk("japanese"));
        director2.setLeading(24);
        document.add(director2);

        //Paragraph  : a Phrase with extra properties and a newline
        document.newPage();
        document.add(new Paragraph("Paragraph page"));

        Paragraph info = new Paragraph();
        info.add(new Chunk("China "));
        info.add(new Chunk("chinese"));
        info.add(Chunk.NEWLINE);
        info.add(new Phrase("Japan "));
        info.add(new Phrase("japanese"));
        document.add(info);

        //List  : a sequence of Paragraphs called ListItem
        document.newPage();
        List list = new List(List.ORDERED);
        for (int i = 0; i < 10; i++) {
            ListItem item = new ListItem(String.format("%s: %d movies",
                    "country" + (i + 1), (i + 1) * 100), new Font(
                    Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE));
            List movielist = new List(List.ORDERED, List.ALPHABETICAL);
            movielist.setLowercase(List.LOWERCASE);
            for (int j = 0; j < 5; j++) {
                ListItem movieitem = new ListItem("Title" + (j + 1));
                List directorlist = new List(List.UNORDERED);
                for (int k = 0; k < 3; k++) {
                    directorlist.add(String.format("%s, %s", "Name1" + (k + 1),
                            "Name2" + (k + 1)));
                }
                movieitem.add(directorlist);
                movielist.add(movieitem);
            }
            item.add(movielist);
            list.add(item);
        }
        document.add(list);
        document.close();
        System.out.println("end.......");
    }

    /**
     *   7:
     *   Anchor, Image, Chapter, Section
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfTest7(String fileUrl)throws Exception{
        System.out.println("begin...........");
        //Chunk  : a String, a Font, and some attributes
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl));
        document.open();
        //Anchor  : internal and external links
        Paragraph country = new Paragraph();
        Anchor dest = new Anchor("china", new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.BLUE));
        dest.setName("CN");
        dest.setReference("http://www.china.com");//external
        country.add(dest);
        country.add(String.format(": %d sites", 10000));
        document.add(country);

        document.newPage();
        Anchor toUS = new Anchor("Go to first page.", new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.BLUE));
        toUS.setReference("#CN");//internal
        document.add(toUS);

        //Image  
        document.newPage();
        Image img = Image.getInstance("E:\\mystudy\\pdf\\api\\mypic.jpg");
        img.setAlignment(Image.LEFT | Image.TEXTWRAP);
        img.setBorder(Image.BOX);
        img.setBorderWidth(10);
        img.setBorderColor(BaseColor.WHITE);
        img.scaleToFit(1000, 72);//  
        img.setRotationDegrees(-30);//  
        document.add(img);

        //Chapter, Section  (  )
        document.newPage();
        Paragraph title = new Paragraph("Title");
        Chapter chapter = new Chapter(title, 1);

        title = new Paragraph("Section A");
        Section section = chapter.addSection(title);
        section.setBookmarkTitle("bmk");
        section.setIndentation(30);
        section.setBookmarkOpen(false);
        section.setNumberStyle(
                Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT);

        Section subsection = section.addSection(new Paragraph("Sub Section A"));
        subsection.setIndentationLeft(20);
        subsection.setNumberDepth(1);
        document.add(chapter);
        document.close();

        System.out.println("end...........");
    }

    /**
     *   8:
     *   
     * @param fileUrl
     */
    public static void createPdfDrawing(String fileUrl)throws Exception{
        System.out.println("begin...........");
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl));
        document.open();
        //    
        document.add(new VerticalPositionMark() {
            @Override
            public void draw(PdfContentByte canvas, float llx, float lly,
                             float urx, float ury, float y) {
                canvas.beginText();
                BaseFont bf = null ;
                try {
                    bf = BaseFont.createFont(BaseFont.ZAPFDINGBATS, "", BaseFont.EMBEDDED);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                canvas.setFontAndSize(bf, 12);
                // LEFT
                canvas.showTextAligned(Element.ALIGN_CENTER, String.valueOf((char) 220), llx - 10, y, 0);
                // RIGHT
                canvas.showTextAligned(Element.ALIGN_CENTER, String.valueOf((char) 220), urx + 10, y + 8, 180);
                canvas.endText();
            }
        });

        //  
        Paragraph p1 = new Paragraph("LEFT");
        p1.add(new Chunk(new LineSeparator()));
        p1.add("R");
        document.add(p1);
        //  
        Paragraph p2 = new Paragraph("LEFT");
        p2.add(new Chunk(new DottedLineSeparator()));
        p2.add("R");
        document.add(p2);
        //   
        LineSeparator UNDERLINE = new LineSeparator(1, 100,null , Element.ALIGN_CENTER, -2);
        Paragraph p3 = new Paragraph("NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN");
        p3.add(UNDERLINE);
        document.add(p3);
        document.close();
        System.out.println("end...........");
    }

    /**
     *   9:
     *     
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfParagraph(String fileUrl)throws Exception{
        System.out.println("begin...........");
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl));
        document.open();
        Paragraph p = new Paragraph("In the previous example, you added a header and footer with the showTextAligned() method. This example demonstrates that it’s sometimes more interesting to use PdfPTable and writeSelectedRows(). You can define a bottom border for each cell so that the header is underlined. This is the most elegant way to add headers and footers, because the table mechanism allows you to position and align lines, images, and text.");
        //  
        p.setAlignment(Element.ALIGN_JUSTIFIED);
        document.add(p);
        document.newPage();
        p.setAlignment(Element.ALIGN_JUSTIFIED);
        p.setIndentationLeft(1 * 15f);
        p.setIndentationRight((5 - 1) * 15f);
        document.add(p);
        //  
        document.newPage();
        p.setAlignment(Element.ALIGN_RIGHT);
        p.setSpacingAfter(15f);
        document.add(p);
        //  
        document.newPage();
        p.setAlignment(Element.ALIGN_LEFT);
        p.setSpacingBefore(15f);
        document.add(p);
        //  
        document.newPage();
        p.setAlignment(Element.ALIGN_CENTER);
        p.setSpacingAfter(15f);
        p.setSpacingBefore(15f);
        document.add(p);
        document.close();
        System.out.println("end...........");
    }

    /**
     *   :10
     *   Page
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfDeletePage(String fileUrl)throws Exception{
        System.out.println("begin...........");
        FileOutputStream out = new FileOutputStream(fileUrl);
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, out);

        document.open();
        document.add(new Paragraph("First page"));
        document.add(new Paragraph("version"));

        document.newPage();
        writer.setPageEmpty(false);

        document.newPage();
        document.add(new Paragraph("New page"));
        document.close();

        PdfReader reader = new PdfReader(fileUrl);
        reader.selectPages("1,3");
        PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("E:\\mystudy\\pdf\\api\\deletePage2.pdf"));
        stamp.close();
        reader.close();
        System.out.println("end...........");
    }

    /**
     *   :11
     *   Page
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfInsertPage(String fileUrl)throws Exception{
        System.out.println("begin...........");
        FileOutputStream out = new FileOutputStream(fileUrl);
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, out);
        document.open();
        document.add(new Paragraph("1 page"));

        document.newPage();
        document.add(new Paragraph("2 page"));

        document.newPage();
        document.add(new Paragraph("3 page"));
        document.close();

        PdfReader reader = new PdfReader(fileUrl);
        PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("E:\\mystudy\\pdf\\api\\"
                + "insertPage2.pdf"));
        stamp.insertPage(2, reader.getPageSize(1));
        /*ColumnText ct = new ColumnText(writer.DirectContent);
        ct.addElement(new Paragraph(24, new Chunk("INSERT PAGE")));
        ct.setCanvas(stamp.getOverContent(2));
        ct.setSimpleColumn(36, 36, 559, 770);*/

        stamp.close();
        reader.close();
        System.out.println("end...........");
    }

    /**
     *   :12
     *   page
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfSortPage(String fileUrl)throws Exception{
        System.out.println("begin...............");
        Document doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(fileUrl));
        writer.setLinearPageMode();
        doc.open();
        doc.add(new Paragraph("1 page"));
        doc.newPage();
        doc.add(new Paragraph("2 page"));
        doc.newPage();
        doc.add(new Paragraph("3 page"));
        doc.newPage();
        doc.add(new Paragraph("4 page"));
        doc.newPage();
        doc.add(new Paragraph("5 page"));

        int[] order = {4,3,2,1};
        writer.reorderPages(order);
        doc.close();
        System.out.println("end...............");
    }


    /**
     *   :13
     *    
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfCatalog(String fileUrl)throws Exception{
        System.out.println("begin...............");
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl));
        document.open();
        // Code 1
        document.add(new Chunk("Chapter 1").setLocalDestination("1"));
        document.newPage();
        document.add(new Chunk("Chapter 2").setLocalDestination("2"));
        document.add(new Paragraph(new Chunk("Sub 2.1").setLocalDestination("2.1")));
        document.add(new Paragraph(new Chunk("Sub 2.2").setLocalDestination("2.2")));
        document.newPage();
        document.add(new Chunk("Chapter 3").setLocalDestination("3"));
        // Code 2
        PdfContentByte cb = writer.getDirectContent();
        PdfOutline root = cb.getRootOutline();
        // Code 3
        @SuppressWarnings("unused")
        PdfOutline oline1 = new PdfOutline(root, PdfAction.gotoLocalPage("1", false), "Chapter 1");

        PdfOutline oline2 = new PdfOutline(root, PdfAction.gotoLocalPage("2", false), "Chapter 2");
        oline2.setOpen(false);

        @SuppressWarnings("unused")
        PdfOutline oline2_1 = new PdfOutline(oline2, PdfAction.gotoLocalPage("2.1", false), "Sub 2.1");
        @SuppressWarnings("unused")
        PdfOutline oline2_2 = new PdfOutline(oline2, PdfAction.gotoLocalPage("2.2", false), "Sub 2.2");

        @SuppressWarnings("unused")
        PdfOutline oline3 = new PdfOutline(root, PdfAction.gotoLocalPage("3", false), "Chapter 3");
        document.close();
        System.out.println("end...............");
    }


    /**
     *   14:
     *   Header, Footer
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfHeadAndFooter(String fileUrl)throws Exception{
        System.out.println("begin...............");
        Document doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(fileUrl));
        writer.setPageEvent(new PdfPageEventHelper() {
            @Override
            public void onEndPage(PdfWriter writer, Document document) {
                PdfContentByte cb = writer.getDirectContent();
                cb.saveState();
                cb.beginText();
                BaseFont bf = null;
                try {
                    bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                cb.setFontAndSize(bf, 10);
                //Header
                float x = document.top(-20);
                // 
                cb.showTextAligned(PdfContentByte.ALIGN_LEFT,
                        "H-Left",
                        document.left(), x, 0);
                // 
                cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
                        writer.getPageNumber()+ " page",
                        (document.right() + document.left())/2,
                        x, 0);
                // 
                cb.showTextAligned(PdfContentByte.ALIGN_RIGHT,
                        "H-Right",
                        document.right(), x, 0);
                //Footer
                float y = document.bottom(-20);
                // 
                cb.showTextAligned(PdfContentByte.ALIGN_LEFT,
                        "F-Left",
                        document.left(), y, 0);
                // 
                cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
                        writer.getPageNumber()+" page",
                        (document.right() + document.left())/2,
                        y, 0);
                // 
                cb.showTextAligned(PdfContentByte.ALIGN_RIGHT,
                        "F-Right",
                        document.right(), y, 0);
                cb.endText();
                cb.restoreState();
            }
        });
        doc.open();
        doc.add(new Paragraph("1 page"));
        doc.newPage();
        doc.add(new Paragraph("2 page"));
        doc.newPage();
        doc.add(new Paragraph("3 page"));
        doc.newPage();
        doc.add(new Paragraph("4 page"));
        doc.close();
        System.out.println("end...............");
    }

    /**
     *   15:
     *     
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfWord(String fileUrl)throws Exception{
        System.out.println("begin...............");
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl));
        document.open();
        PdfContentByte canvas = writer.getDirectContent();
        Phrase phrase1 = new Phrase("This is a test!left");
        Phrase phrase2 = new Phrase("This is a test!right");
        Phrase phrase3 = new Phrase("This is a test!center");
        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase1, 50, 500, 0);
        ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, phrase2, 100, 536, 0);
        ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase3, 50, 572, 0);
        document.close();
        System.out.println("end...............");
    }


    /**
     *   :16
     *    
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfSlid(String fileUrl)throws Exception{
        System.out.println("begin...............");
        Document doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(fileUrl));
        writer.setPdfVersion(PdfWriter.VERSION_1_5);
        //  
        writer.setViewerPreferences(PdfWriter.PageModeFullScreen);
        writer.setPageEvent(new PdfPageEventHelper() {
            @Override
            public void onStartPage(PdfWriter writer, Document document) {
                writer.setTransition(new PdfTransition(PdfTransition.DISSOLVE, 3));
                //    
                writer.setDuration(5);
            }
        });
        doc.open();
        doc.add(new Paragraph("1 page"));
        doc.newPage();
        doc.add(new Paragraph("2 page"));
        doc.newPage();
        doc.add(new Paragraph("3 page"));
        doc.newPage();
        doc.add(new Paragraph("4 page"));
        doc.newPage();
        doc.add(new Paragraph("5 page"));
        doc.close();
        System.out.println("end...............");
    }

    /**
     *   17:
     *   PDF Zip
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfZip(String fileUrl)throws Exception{
        System.out.println("begin.........................");
        ZipOutputStream zip = null;
        try {
            zip = new ZipOutputStream(new FileOutputStream(fileUrl));
            for (int i = 1; i <= 3; i++) {
                ZipEntry entry = new ZipEntry("test" + i + ".pdf");
                zip.putNextEntry(entry);
                Document document = new Document();
                PdfWriter writer = PdfWriter.getInstance(document, zip);
                writer.setCloseStream(false);
                document.open();
                document.add(new Paragraph("Hello " + i));
                document.close();
                zip.closeEntry();
            }
            System.out.println("end.....................................");
        }finally {
            if (zip != null) {
                zip.close();
            }
        }

    }


    /**
     *   18:
     *   pdf
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfDivision(String fileUrl)throws Exception{
        System.out.println("begin.....................................");
        FileOutputStream out = new FileOutputStream(fileUrl);
        Document document = new Document();
        PdfWriter.getInstance(document, out);
        document.open();
        document.add(new Paragraph("1 page"));
        document.newPage();
        document.add(new Paragraph("2 page"));
        document.newPage();
        document.add(new Paragraph("3 page"));
        document.newPage();
        document.add(new Paragraph("4 page"));
        document.close();
        PdfReader reader = new PdfReader(fileUrl);
        Document dd = new Document();
        PdfWriter writer = PdfWriter.getInstance(dd, new FileOutputStream("E:\\mystudy\\pdf\\api\\" + "splitPDF1.pdf"));
        dd.open();
        PdfContentByte cb = writer.getDirectContent();
        dd.newPage();
        cb.addTemplate(writer.getImportedPage(reader, 1), 0, 0);
        dd.newPage();
        cb.addTemplate(writer.getImportedPage(reader, 2), 0, 0);
        dd.close();
        writer.close();
        Document dd2 = new Document();
        PdfWriter writer2 = PdfWriter.getInstance(dd2, new FileOutputStream("E:\\mystudy\\pdf\\api\\" + "splitPDF2.pdf"));
        dd2.open();
        PdfContentByte cb2 = writer2.getDirectContent();
        dd2.newPage();
        cb2.addTemplate(writer2.getImportedPage(reader, 3), 0, 0);
        dd2.newPage();
        cb2.addTemplate(writer2.getImportedPage(reader, 4), 0, 0);
        dd2.close();
        writer2.close();
        System.out.println("end.....................................");
    }

    /**
     *   19:
     *   pdf
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfMerge(String fileUrl)throws Exception{
        System.out.println("begin.....................................");
        String FILE_DIR = "E:\\mystudy\\pdf\\api\\";
        PdfReader reader1 = new PdfReader(FILE_DIR + "splitPDF1.pdf");
        PdfReader reader2 = new PdfReader(FILE_DIR + "splitPDF2.pdf");
        FileOutputStream out = new FileOutputStream(FILE_DIR + "mergePDF.pdf");

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, out);
        document.open();
        PdfContentByte cb = writer.getDirectContent();

        int totalPages = 0;
        totalPages += reader1.getNumberOfPages();
        totalPages += reader2.getNumberOfPages();
        java.util.List readers = new ArrayList();
        readers.add(reader1);
        readers.add(reader2);

        int pageOfCurrentReaderPDF = 0;
        Iterator iteratorPDFReader = readers.iterator();

        // Loop through the PDF files and add to the output.
        while (iteratorPDFReader.hasNext()) {
            PdfReader pdfReader = iteratorPDFReader.next();
            // Create a new page in the target for each source page.
            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                document.newPage();
                pageOfCurrentReaderPDF++;
                PdfImportedPage page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                cb.addTemplate(page, 0, 0);
            }
            pageOfCurrentReaderPDF = 0;
        }
        out.flush();
        document.close();
        out.close();
        System.out.println("end.....................................");
    }


    /**
     *   20:
     * Annotation
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfAnnotation (String fileUrl)throws Exception{
        System.out.println("begin.....................................");
        Document doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(fileUrl));
        writer.setLinearPageMode();
        doc.open();
        doc.add(new Paragraph("1 page"));
        doc.add(new Annotation("Title", "This is a annotation!"));

        doc.newPage();
        doc.add(new Paragraph("2 page"));
        Chunk chunk = new Chunk("\u00a0");
        chunk.setAnnotation(PdfAnnotation.createText(writer,null , "Title", "This is a another annotation!", false, "Comment"));
        doc.add(chunk);

        //    
        doc.newPage();
        doc.add(new Paragraph("3 page"));
        Chunk chunk2 = new Chunk("\u00a0\u00a0");
        PdfAnnotation annotation = PdfAnnotation.createFileAttachment(
                writer,null , "Title",null ,
                "E:\\mystudy\\pdf\\api\\4px_logon_HK.png",
                "img.jpg");
        annotation.put(PdfName.NAME,
                new PdfString("Paperclip"));
        chunk2.setAnnotation(annotation);
        doc.add(chunk2);
        doc.close();
        System.out.println("end.....................................");
    }


    /**
     *   21:
     * Table
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfTable (String fileUrl)throws Exception{
        System.out.println("begin.....................................");
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl));
        document.open();
        PdfPTable table = new PdfPTable(3);
        PdfPCell cell;
        cell = new PdfPCell(new Phrase("Cell with colspan 3"));
        cell.setColspan(3);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
        cell.setRowspan(2);
        table.addCell(cell);
        table.addCell("row 1; cell 1");
        table.addCell("row 1; cell 2");
        table.addCell("row 2; cell 1");
        table.addCell("row 2; cell 2");
        document.add(table);
        document.close();
        System.out.println("end.....................................");
    }

    /**
     *   22:
     *     
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfTableNest (String fileUrl)throws Exception{
        System.out.println("begin.....................................");
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl));
        document.open();
        //    
        PdfPTable table = new PdfPTable(4);
        //1 2 
        PdfPTable nested1 = new PdfPTable(2);
        nested1.addCell("1.1");
        nested1.addCell("1.2");
        //2 1 
        PdfPTable nested2 = new PdfPTable(1);
        nested2.addCell("2.1");
        nested2.addCell("2.2");

        //          
        for (int k = 0; k < 24; ++k) {
            if (k == 1) {
                table.addCell(nested1);
            } else if (k == 20) {
                table.addCell(nested2);
            } else {
                table.addCell("cell " + k);
            }
        }
        document.add(table);
        document.close();
        System.out.println("end.....................................");
    }

    /**
     *   23:
     *       
     * @param fileUrl
     * @throws Exception
     */
    public static void createPdfTableWidth (String fileUrl)throws Exception{
        System.out.println("begin.....................................");
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl));
        document.open();
        PdfPTable table = new PdfPTable(3);
        PdfPCell cell;
        cell = new PdfPCell(new Phrase("Cell with colspan 3"));
        cell.setColspan(3);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
        cell.setRowspan(2);
        table.addCell(cell);
        table.addCell("row 1; cell 1");
        table.addCell("row 1; cell 2");
        table.addCell("row 2; cell 1");
        table.addCell("row 2; cell 2");

        //100%
        table.setWidthPercentage(100);
        document.add(table);
        document.add(new Paragraph("

")); // 50% table.setHorizontalAlignment(Element.ALIGN_LEFT); table.setWidthPercentage(50); document.add(table); document.add(new Paragraph("

")); // 50% table.setHorizontalAlignment(Element.ALIGN_CENTER); table.setWidthPercentage(50); document.add(table); document.add(new Paragraph("

")); // 50% table.setWidthPercentage(50); table.setHorizontalAlignment(Element.ALIGN_RIGHT); document.add(table); document.add(new Paragraph("

")); // table.setTotalWidth(300); table.setLockedWidth(true); document.add(table); document.close(); System.out.println("end....................................."); } /** * 24: * * @param fileUrl * @throws Exception */ public static void createPdfTableSpace (String fileUrl)throws Exception{ System.out.println("begin....................................."); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl)); document.open(); PdfPTable table = new PdfPTable(3); BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(baseFontChinese , 11 , Font.NORMAL); Font fontZH = fontChinese; PdfPCell cell = new PdfPCell(new Paragraph(" 3 ",fontZH)); cell.setColspan(3); table.addCell(cell); table.addCell("1.1"); table.addCell("2.1"); table.addCell("3.1"); table.addCell("1.2"); table.addCell("2.2"); table.addCell("3.2"); cell = new PdfPCell(new Paragraph(" ",fontZH)); cell.setBorderColor(new BaseColor(255, 0, 0)); table.addCell(cell); cell = new PdfPCell(new Paragraph(" 2 ",fontZH)); cell.setColspan(2); cell.setBackgroundColor(new BaseColor(0xC0, 0xC0, 0xC0)); table.addCell(cell); table.setWidthPercentage(50); document.add(new Paragraph(" 2 ",fontZH)); document.add(table); document.add(table); document.newPage(); document.add(new Paragraph(" 'SpacingBefore' 'setSpacingAfter'",fontZH)); table.setSpacingBefore(15f); document.add(table); document.add(table); document.add(new Paragraph(" ",fontZH)); table.setSpacingAfter(15f); document.close(); System.out.println("end....................................."); } /** * 25: * * @param fileUrl * @throws Exception */ public static void createPdfTableCellWidth(String fileUrl)throws Exception{ System.out.println("begin....................................."); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl)); document.open(); // float[] widths = {0.1f, 0.1f, 0.05f, 0.75f}; PdfPTable table = new PdfPTable(widths); table.addCell("10%"); table.addCell("10%"); table.addCell("5%"); table.addCell("75%"); table.addCell("aa"); table.addCell("aa"); table.addCell("a"); table.addCell("aaaaaaaaaaaaaaa"); table.addCell("bb"); table.addCell("bb"); table.addCell("b"); table.addCell("bbbbbbbbbbbbbbb"); table.addCell("cc"); table.addCell("cc"); table.addCell("c"); table.addCell("ccccccccccccccc"); document.add(table); document.add(new Paragraph("

")); // widths[0] = 20f; widths[1] = 20f; widths[2] = 10f; widths[3] = 50f; table.setWidths(widths); document.add(table); // widths[0] = 40f; widths[1] = 40f; widths[2] = 20f; widths[3] = 300f; Rectangle r = new Rectangle(PageSize.A4.getRight(72), PageSize.A4.getTop(72)); table.setWidthPercentage(widths, r); document.add(new Paragraph("

")); document.add(table); document.close(); System.out.println("end....................................."); } /** * :26 * * @param fileUrl * @throws Exception */ public static void createPdfTableCellHeight(String fileUrl)throws Exception{ System.out.println("begin....................................."); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl)); document.open(); PdfPTable table = new PdfPTable(2); PdfPCell cell; BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(baseFontChinese , 11 , Font.NORMAL); Font fontZH = fontChinese; // table.addCell(new PdfPCell(new Paragraph(" ", fontZH))); cell = new PdfPCell(new Paragraph("blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah")); cell.setNoWrap(false); table.addCell(cell); // table.addCell(new PdfPCell(new Paragraph(" ", fontZH))); cell.setNoWrap(true); table.addCell(cell); // table.addCell(new PdfPCell(new Paragraph(" ",fontZH))); cell = new PdfPCell(new Paragraph("1. blah blah
2. blah blah blah
3. blah blah
4. blah blah blah
5. blah blah
6. blah blah blah
7. blah blah
8. blah blah blah")); table.addCell(cell); // table.addCell(new PdfPCell(new Paragraph(" ",fontZH))); cell.setFixedHeight(50f); table.addCell(cell); // table.addCell(new PdfPCell(new Paragraph(" ",fontZH))); cell = new PdfPCell(new Paragraph(" :50",fontZH)); cell.setMinimumHeight(50f); table.addCell(cell); // page table.setExtendLastRow(true); table.addCell(new PdfPCell(new Paragraph(" ",fontZH))); cell = new PdfPCell(new Paragraph(" page ",fontZH)); table.addCell(cell); document.add(table); document.close(); System.out.println("end....................................."); } /** * 27: * * @param fileUrl * @throws Exception */ public static void createPdfTableCellColour(String fileUrl)throws Exception{ System.out.println("begin....................................."); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl)); document.open(); PdfPTable table = new PdfPTable(4); BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(baseFontChinese , 11 , Font.NORMAL); Font fontZH = fontChinese; PdfPCell cell; cell = new PdfPCell(new Paragraph(" ",fontZH)); table.addCell(cell); // , cell = new PdfPCell(new Paragraph(" , ",fontZH)); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(BaseColor.RED); table.addCell(cell); // , cell = new PdfPCell(new Paragraph(" , ",fontZH)); cell.setBorder(Rectangle.BOTTOM); cell.setBorderColorBottom(BaseColor.MAGENTA); cell.setBorderWidthBottom(5f); cell.setBackgroundColor(BaseColor.GREEN); table.addCell(cell); // , cell = new PdfPCell(new Paragraph(" , ",fontZH)); cell.setBorder(Rectangle.TOP); cell.setUseBorderPadding(true); cell.setBorderWidthTop(5f); cell.setBorderColorTop(BaseColor.CYAN); cell.setBackgroundColor(BaseColor.BLUE); table.addCell(cell); cell = new PdfPCell(new Paragraph(" ",fontZH)); table.addCell(cell); cell = new PdfPCell(new Paragraph("0.25")); cell.setBorder(Rectangle.NO_BORDER); cell.setGrayFill(0.25f); table.addCell(cell); cell = new PdfPCell(new Paragraph("0.5")); cell.setBorder(Rectangle.NO_BORDER); cell.setGrayFill(0.5f); table.addCell(cell); cell = new PdfPCell(new Paragraph("0.75")); cell.setBorder(Rectangle.NO_BORDER); cell.setGrayFill(0.75f); table.addCell(cell); document.add(table); document.close(); System.out.println("end....................................."); } /** * 28: * * @param fileUrl * @throws Exception */ public static void createPdfTableCellImage(String fileUrl)throws Exception{ System.out.println("begin.................................."); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl)); document.open(); BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(baseFontChinese , 11 , Font.NORMAL); Font fontZH = fontChinese; Image image = Image.getInstance("E:\\mystudy\\pdf\\api\\mypic.jpg"); float[] widths = { 1f, 4f }; PdfPTable table = new PdfPTable(widths); // table.addCell(new PdfPCell(new Paragraph(" ", fontZH))); table.addCell(image); // table.addCell("This two"); table.addCell(new PdfPCell(image, true)); // table.addCell("This three"); table.addCell(new PdfPCell(image, false)); document.add(table); document.close(); System.out.println("end.................................."); } /** * 29: * * @param fileUrl * @throws Exception */ public static void createPdfTableHead(String fileUrl)throws Exception{ System.out.println("begin.................................."); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl)); document.open(); String[] bogusData = { "M0065920", "SL", "FR86000P", "PCGOLD", "119000", "96 06", "2001-08-13", "4350", "6011648299", "FLFLMTGP", "153", "119000.00" }; int NumColumns = 12; // 12 PdfPTable datatable = new PdfPTable(NumColumns); // percentage int headerwidths[] = { 9, 4, 8, 10, 8, 11, 9, 7, 9, 10, 4, 10 }; datatable.setWidths(headerwidths); datatable.setWidthPercentage(100); datatable.getDefaultCell().setPadding(3); datatable.getDefaultCell().setBorderWidth(2); datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); datatable.addCell("Clock #"); datatable.addCell("Trans Type"); datatable.addCell("Cusip"); datatable.addCell("Long Name"); datatable.addCell("Quantity"); datatable.addCell("Fraction Price"); datatable.addCell("Settle Date"); datatable.addCell("Portfolio"); datatable.addCell("ADP Number"); datatable.addCell("Account ID"); datatable.addCell("Reg Rep ID"); datatable.addCell("Amt To Go "); datatable.setHeaderRows(1); // datatable.getDefaultCell().setBorderWidth(1); // for (int i = 1; i < 1000; i++) { for (int x = 0; x < NumColumns; x++) { datatable.addCell(bogusData[x]); } } document.add(datatable); document.close(); System.out.println("end.................................."); } /** * 30: * * @param fileUrl * @throws Exception */ public static void createPdfTableDivision(String fileUrl)throws Exception{ System.out.println("begin.................................."); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl)); document.open(); // PdfContentByte cb = writer.getDirectContent(); PdfPTable table = new PdfPTable(10); for (int k = 1; k <= 100; ++k) { table.addCell("The number " + k); } table.setTotalWidth(400); table.writeSelectedRows(0, 5, 0, -1, 5, 700, cb); table.writeSelectedRows(5, -1, 0, -1, 210, 700, cb); document.close(); System.out.println("end.................................."); } /** * 31: * * @param fileUrl * @throws Exception */ public static void createPdfTableCellMessage(String fileUrl)throws Exception{ System.out.println("begin..............................."); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl)); document.open(); BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(baseFontChinese , 11 , Font.NORMAL); Font fontZH = fontChinese; PdfPTable table = new PdfPTable(2); PdfPCell cell; Paragraph p = new Paragraph("Quick brown fox jumps over the lazy dog. Quick brown fox jumps over the lazy dog."); table.addCell(new PdfPCell(new Paragraph(" ",fontZH))); table.addCell(p); table.addCell(new PdfPCell(new Paragraph("Padding:10",fontZH))); cell = new PdfPCell(p); cell.setPadding(10f); table.addCell(cell); table.addCell(new PdfPCell(new Paragraph("Padding:0",fontZH))); cell = new PdfPCell(p); cell.setPadding(0f); table.addCell(cell); table.addCell(new PdfPCell(new Paragraph(" Padding:0 Padding:20",fontZH))); cell = new PdfPCell(p); cell.setPaddingTop(0f); cell.setPaddingLeft(20f); table.addCell(cell); document.add(table); document.newPage(); table = new PdfPTable(2); table.addCell(new PdfPCell(new Paragraph(" Leading",fontZH))); table.getDefaultCell().setLeading(0f, 0f); table.addCell("blah blah
blah blah blah
blah blah
blah blah blah
blah blah
blah blah blah
blah blah
blah blah blah
"); table.getDefaultCell().setLeading(14f, 0f); table.addCell(new PdfPCell(new Paragraph(" Leading:14pt",fontZH))); table.addCell("blah blah
blah blah blah
blah blah
blah blah blah
blah blah
blah blah blah
blah blah
blah blah blah
"); table.addCell(new PdfPCell(new Paragraph(" ",fontZH))); table.getDefaultCell().setLeading(0f, 1.0f); table.addCell("blah blah
blah blah blah
blah blah
blah blah blah
blah blah
blah blah blah
blah blah
blah blah blah
"); document.add(table); document.close(); System.out.println("end..............................."); } /** * 32: * * @param fileUrl * @throws Exception */ public static void createPdfTableCellFrame(String fileUrl)throws Exception{ System.out.println("begin................................."); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl)); document.open(); // PdfPTable table1 = new PdfPTable(3); table1.getDefaultCell().setBorder(PdfPCell.NO_BORDER); table1.addCell(new Paragraph("Cell 1")); table1.addCell(new Paragraph("Cell 2")); table1.addCell(new Paragraph("Cell 3")); document.add(table1); // document.newPage(); Rectangle b1 = new Rectangle(0f, 0f); b1.setBorderWidthLeft(6f); b1.setBorderWidthBottom(5f); b1.setBorderWidthRight(4f); b1.setBorderWidthTop(2f); b1.setBorderColorLeft(BaseColor.RED); b1.setBorderColorBottom(BaseColor.ORANGE); b1.setBorderColorRight(BaseColor.YELLOW); b1.setBorderColorTop(BaseColor.GREEN); PdfPTable table2 = new PdfPTable(1); PdfPCell cell = new PdfPCell(new Paragraph("Cell 1")); cell.cloneNonPositionParameters(b1); table2.addCell(cell); document.add(table2); document.close(); System.out.println("end................................."); } /** * 33: * Barcode QRCode * @param fileUrl * @throws Exception */ public static void createPdfBarcode(String fileUrl)throws Exception{ System.out.println("begin................................."); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl)); document.open(); String myString = "https://www.baidu.com"; Barcode128 code128 = new Barcode128(); code128.setCode(myString.trim()); code128.setCodeType(Barcode128.CODE128); Image code128Image = code128.createImageWithBarcode(writer.getDirectContent(),null ,null ); code128Image.setAbsolutePosition(10,700); code128Image.scalePercent(125); document.add(code128Image); BarcodeQRCode qrcode = new BarcodeQRCode(myString.trim(), 1, 1, null); Image qrcodeImage = qrcode.getImage(); qrcodeImage.setAbsolutePosition(10,600); qrcodeImage.scalePercent(200); document.add(qrcodeImage); document.close(); System.out.println("end................................."); } public static void main(String[] args) throws Exception { //1, : //createSimplePdf("E:\\mystudy\\pdf\\api\\test1.pdf"); //2, , , //createTest2Pdf("E:\\mystudy\\pdf\\api\\test2.pdf"); //3, pdf pdf //createPdfWithPassword("E:\\mystudy\\pdf\\api\\test3.pdf"); //4, page //createPdfAddPage("E:\\mystudy\\pdf\\api\\test4.pdf"); //5,pdf ( ) //createPdfBgImage("E:\\mystudy\\pdf\\api\\test4.pdf"); //6, Chunk, Phrase, Paragraph, List //createPdfTest6("E:\\mystudy\\pdf\\api\\test6.pdf"); //7, Anchor, Image, Chapter, Section //createPdfTest7("E:\\mystudy\\pdf\\api\\test7.pdf"); //8, //createPdfDrawing("E:\\mystudy\\pdf\\api\\test8.pdf"); //9, //createPdfParagraph("E:\\mystudy\\pdf\\api\\test9.pdf"); //10, //createPdfDeletePage("E:\\mystudy\\pdf\\api\\test10.pdf"); //11, Page //createPdfInsertPage("E:\\mystudy\\pdf\\api\\test11.pdf"); //12, page //createPdfSortPage("E:\\mystudy\\pdf\\api\\test12.pdf"); //13, //createPdfCatalog("E:\\mystudy\\pdf\\api\\test13.pdf"); //14, Header, Footer //createPdfHeadAndFooter("E:\\mystudy\\pdf\\api\\test14.pdf"); //15, //createPdfWord("E:\\mystudy\\pdf\\api\\test15.pdf"); //16, //createPdfSlid("E:\\mystudy\\pdf\\api\\test16.pdf"); //17,pdf zip //createPdfZip("E:\\mystudy\\pdf\\api\\test17.zip"); //createPdfDivision("E:\\mystudy\\pdf\\api\\test18.pdf"); //19, pdf //createPdfMerge(null); //20,Annotation //createPdfAnnotation("E:\\mystudy\\pdf\\api\\test20.pdf"); //21,Table //createPdfTable("E:\\mystudy\\pdf\\api\\test21.pdf"); //22, //createPdfTableNest("E:\\mystudy\\pdf\\api\\test22.pdf"); //23, //createPdfTableWidth("E:\\mystudy\\pdf\\api\\test23.pdf"); //24, //createPdfTableSpace("E:\\mystudy\\pdf\\api\\test24.pdf"); //25, //createPdfTableCellWidth("E:\\mystudy\\pdf\\api\\test25.pdf"); //26, //createPdfTableCellHeight("E:\\mystudy\\pdf\\api\\test26.pdf"); //27, //createPdfTableCellColour("E:\\mystudy\\pdf\\api\\test27.pdf"); //28, //createPdfTableCellImage("E:\\mystudy\\pdf\\api\\test28.pdf"); //29, //createPdfTableHead("E:\\mystudy\\pdf\\api\\test29.pdf"); //30, //createPdfTableDivision("E:\\mystudy\\pdf\\api\\test30.pdf"); //31, //createPdfTableCellMessage("E:\\mystudy\\pdf\\api\\test31.pdf"); //32, //createPdfTableCellFrame("E:\\mystudy\\pdf\\api\\test32.pdf"); //33, Barcode QRCode createPdfBarcode("E:\\mystudy\\pdf\\api\\test33.pdf"); } }

 
例:
package com.jack.studytest.pdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;

/**
 * Created By Jack on 2018/8/20
 *
 * @author Jack
 * @date 2018/8/20 14:39
 * @Description:
 */
public class PdfTest2 {


    public static void createTable(String fileUrl)throws Exception{
        System.out.println("      .....................");
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileUrl));
        document.open();

        PdfPTable table = new PdfPTable(12);
        //    
        BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font fontChinese =  new  Font(baseFontChinese , 10 , Font.NORMAL);
        Font fontZH = fontChinese;

        Paragraph p= new Paragraph("       
",fontZH); p.setAlignment(Element.ALIGN_CENTER); p.setSpacingAfter(15f); document.add(p); PdfPCell cell1 = new PdfPCell(new Paragraph("xxx-001-002",fontZH)); cell1.setColspan(7); table.addCell(cell1); PdfPCell cell2 = new PdfPCell(new Paragraph("1 *200¥=200¥",fontZH)); cell2.setColspan(5); table.addCell(cell2); PdfPCell cell3 = new PdfPCell(new Paragraph("Jack",fontZH)); cell3.setColspan(7); table.addCell(cell3); PdfPCell cell4 = new PdfPCell(new Paragraph("2 *200¥=400¥",fontZH)); cell4.setColspan(5); table.addCell(cell4); PdfPCell cell5 = new PdfPCell(new Paragraph(" 13 ",fontZH)); cell5.setColspan(7); cell5.setRowspan(2); table.addCell(cell5); PdfPCell cell6 = new PdfPCell(new Paragraph(" :
:",fontZH)); cell6.setColspan(5); cell6.setRowspan(2); table.addCell(cell6); PdfPCell cell7 = new PdfPCell(new Paragraph(" ",fontZH)); cell7.setColspan(1); cell7.setVerticalAlignment(Element.ALIGN_MIDDLE); cell7.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell7); PdfPCell cell8 = new PdfPCell(new Paragraph(" ",fontZH)); cell8.setColspan(2); cell8.setVerticalAlignment(Element.ALIGN_MIDDLE); cell8.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell8); PdfPCell cell9 = new PdfPCell(new Paragraph(" ",fontZH)); cell9.setColspan(3); cell9.setVerticalAlignment(Element.ALIGN_MIDDLE); cell9.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell9); PdfPCell cell10 = new PdfPCell(new Paragraph(" ",fontZH)); cell10.setColspan(2); cell10.setVerticalAlignment(Element.ALIGN_MIDDLE); cell10.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell10); PdfPCell cell11 = new PdfPCell(new Paragraph(" ",fontZH)); cell11.setColspan(2); cell11.setVerticalAlignment(Element.ALIGN_MIDDLE); cell11.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell11); PdfPCell cell12 = new PdfPCell(new Paragraph(" ",fontZH)); cell12.setColspan(2); cell12.setVerticalAlignment(Element.ALIGN_MIDDLE); cell12.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell12); // insertRow(table,fontZH); insertRow(table,fontZH); insertRow(table,fontZH); insertRow(table,fontZH); document.add(table); document.close(); System.out.println(" ...................."); } public static void insertRow(PdfPTable table, Font fontZH){ PdfPCell cell7 = new PdfPCell(new Paragraph("I300",fontZH)); cell7.setColspan(1); cell7.setVerticalAlignment(Element.ALIGN_MIDDLE); cell7.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell7); PdfPCell cell8 = new PdfPCell(new Paragraph("xxx-223-001-008",fontZH)); cell8.setColspan(2); //cell8.setVerticalAlignment(Element.ALIGN_MIDDLE); //cell8.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell8); PdfPCell cell9 = new PdfPCell(new Paragraph(" 13 ",fontZH)); cell9.setColspan(3); //cell9.setVerticalAlignment(Element.ALIGN_MIDDLE); //cell9.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell9); PdfPCell cell10 = new PdfPCell(new Paragraph("2018-08-17 10:58:35",fontZH)); cell10.setColspan(2); table.addCell(cell10); PdfPCell cell11 = new PdfPCell(new Paragraph("2018-08-19 16:38:45",fontZH)); cell11.setColspan(2); table.addCell(cell11); PdfPCell cell12 = new PdfPCell(new Paragraph(" ",fontZH)); cell12.setColspan(2); table.addCell(cell12); } public static void main(String[] args) throws Exception { String fileUrl = "E:\\mystudy\\pdf\\label_make2.pdf"; createTable(fileUrl); } }

 
 
 
 
参考資料:
JAva操作pdfテンプレート、ストライプ横符号とQR符号:
https://blog.csdn.net/ruixue0117/article/details/77599808?locationNum=8&fps=1
https://blog.csdn.net/zhangxin961304090/article/details/46789237
JAva操作pdfリファレンス:
開始:http://rensanning.iteye.com/blog/1538689
https://blog.csdn.net/xuepiaohan2006/article/details/9084231
iText重要文書資料:
http://www.cuteke.cn/u/CuteKe/blogs/39
https://github.com/iTextCN
http://www.cuteke.cn/blogs
https://blog.csdn.net/column/details/18037.html
JArパッケージアドレス:
http://mvnrepository.com/artifact/com.itextpdf/itext7-core
http://mvnrepository.com/artifact/com.itextpdf/itextpdf