JavaはiText5でPDFを生成します

jar包:itext-asian-5.2.0.jar itextpdf-5.5.5.jar itext-1.4.6.jar

Java用iText5処理PDFitex-asian-5.2.0.jar itextpdf-5.5.5.jar itext-1.4.6.jar

説明

新しいドキュメントオブジェクトを作成する

  1. Document document = new Document(); //デフォルトのページサイズはA4です
  2. Document document = new Document(PageSize.A4); //ページサイズをA4として指定します
  3. Document document = new Document(PageSize.A4,50,50,30,20); //ページサイズをA4として指定し、余白をカスタマイズします
     

ディスク書き込み操作用のPdfWriterオブジェクト

PdfWriter.getInstance(document、new FileOutputStream(filePath));

ドキュメントを開く

document.open();

ドキュメントにコンテンツを追加する

document.add();

ドキュメントを閉じる

document.close();

段落

Paragraph pt = new Paragraph(name、headfont); //フォントスタイルを設定する

pt.setAlignment(1); //テキストを中央0から左1、中央2、右に設定します

pt.setIndentationLeft(12); //左インデント

pt.setIndentationRight(12); //右インデント

pt.setFirstLineIndent(24); //最初の行のインデント

paragraph.setLeading(20f); //行間隔

paragraph.setSpacingBefore(5f); //段落に空白を設定する

paragraph.setSpacingAfter(10f); //段落の下のスペースを設定します

テーブル

      Table table = new Table(4); //括弧パラメータは列を示します
      intwidth [] = {10,40,40}; //各列の幅の比率を設定します

      table.setWidths(width);

      table.setWidth(60); //ページ幅の割合の計算

      table.setAlignment(Element.ALIGN_LEFT); //居左

      table.setAutoFillEmptyCells(true); //自動入力

      table.setBorderWidth((float)0.1); //テーブルの境界線の幅

      table.setPadding(1); //マージン:セルの端とセルのコンテンツの間のマージン

      table.setSpacing(0); // Spacing:セルとセルの間の距離

      table.addCell(new Paragraph( "name")、 "cell")); //セルの内容を追加する

      table.endHeaders(); //すべてのページにヘッダーが表示されます

 

インスタンス

タイトルのみのPDFを生成する


import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfJava
{

   public void pdfTest(String filePath) throws Exception
   {
      Document document = new Document(PageSize.A4, 36, 36, 36, 36);
      PdfWriter.getInstance(document, new FileOutputStream(filePath));

      document.open();

      
      //中文字体,解决中文不能显示问题
      BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
      Font cusTitleFont = new Font(bfChinese);
      cusTitleFont.setSize(16);

      Font cusFont = new Font(bfChinese);
      cusFont.setSize(9);

      //添加标题开始
      Font redFont = new Font(bfChinese);
      redFont.setColor(BaseColor.RED);//设置颜色
      redFont.setSize(20);//设置字体大小
      Paragraph chapterTitle = new Paragraph("java生成pdf文件", redFont);//设置内容
      chapterTitle.setAlignment(1);

      Chapter chapter1 = new Chapter(chapterTitle, 1);
      chapter1.setNumberDepth(0);

      document.add(chapter1);
      //添加标题结束
      
      //关闭文档
      document.close();
      
   }

   /**
    * @param args
    */
   public static void main(String[] args)
   {
      try
      {
         String filePath="d:\\test1.pdf";
         new PdfJava().pdfTest(filePath);
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }

   }

}

ファイルは次のとおりです。

テーブルコンテンツを生成する


import java.io.FileOutputStream;
import java.util.List;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPRow;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfJava
{

   public void pdfTest(String filePath) throws Exception
   {
      Document document = new Document(PageSize.A4, 36, 36, 36, 36);
      PdfWriter.getInstance(document, new FileOutputStream(filePath));

      document.open();

      
      //中文字体,解决中文不能显示问题
      BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
      Font cusTitleFont = new Font(bfChinese);
      cusTitleFont.setSize(16);

      Font cusFont = new Font(bfChinese);
      cusFont.setSize(9);

      //添加标题开始
      Font redFont = new Font(bfChinese);
      redFont.setColor(BaseColor.RED);//设置颜色
      redFont.setSize(20);//设置字体大小
      Paragraph chapterTitle = new Paragraph("java生成pdf文件", redFont);//设置内容
      chapterTitle.setAlignment(1);

      Chapter chapter1 = new Chapter(chapterTitle, 1);
      chapter1.setNumberDepth(0);

      document.add(chapter1);
      //添加标题结束
      
      
    //添加一个内容段落
      document.add(new Paragraph("表格信息", cusTitleFont));
      // 表格.
      PdfPTable table = new PdfPTable(6);
      table.setWidthPercentage(100); // 宽度100%填充
      table.setSpacingBefore(10f); // 前间距
      table.setSpacingAfter(10f); // 后间距

      List<PdfPRow> listRow = table.getRows();
      //设置列宽
      float[] columnWidths = { 2f, 2f, 2f ,2f, 2f, 2f};
      table.setWidths(columnWidths);

      //行
      PdfPCell cells1[] = new PdfPCell[6];
      PdfPRow row1 = new PdfPRow(cells1);

      //单元格
      cells1[0] = new PdfPCell(new Paragraph("第一行第1列:", cusFont));//单元格内容
      cells1[0].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

      cells1[1] = new PdfPCell(new Paragraph("第一行第1列值", cusFont));
      cells1[1].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
      
      cells1[2] = new PdfPCell(new Paragraph("第一行第2列:", cusFont));//单元格内容
      cells1[2].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

      cells1[3] = new PdfPCell(new Paragraph("第一行第2列值", cusFont));
      cells1[3].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
      
      cells1[4] = new PdfPCell(new Paragraph("第一行第3列:", cusFont));//单元格内容
      cells1[4].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

      cells1[5] = new PdfPCell(new Paragraph("第一行第3列值", cusFont));
      cells1[5].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
      //把第一行添加到集合
      listRow.add(row1);
      
    //行
      PdfPCell cells2[] = new PdfPCell[6];
      PdfPRow row2 = new PdfPRow(cells2);

      //单元格
      cells2[0] = new PdfPCell(new Paragraph("第二行第1列:", cusFont));//单元格内容
      cells2[0].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

      cells2[1] = new PdfPCell(new Paragraph("第二行第1列值", cusFont));
      cells2[1].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
      
      cells2[2] = new PdfPCell(new Paragraph("第二行第2列:", cusFont));//单元格内容
      cells2[2].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

      cells2[3] = new PdfPCell(new Paragraph("第二行第2列值", cusFont));
      cells2[3].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
      
      cells2[4] = new PdfPCell(new Paragraph("第二行第3列:", cusFont));//单元格内容
      cells2[4].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

      cells2[5] = new PdfPCell(new Paragraph("第二行第3列值", cusFont));
      cells2[5].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
      //把第二行添加到集合
      listRow.add(row2);
      
      //把表格添加到文件中
      document.add(table);
      
      //关闭文档
      document.close();
      
   }

   /**
    * @param args
    */
   public static void main(String[] args)
   {
      try
      {
         String filePath="d:\\test1.pdf";
         new PdfJava().pdfTest(filePath);
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }

   }

}

ファイルは次のとおりです。

公式シールを追加(写真)

   
      Image image1 = Image.getInstance("d:\\gongzhang.jpg");
      //设置图片位置的x轴和y轴
      image1.setAbsolutePosition(100, 500);//注意是从文档的坐下角往右、往上计算的
      //设置图片的宽度和高度
      image1.scaleAbsolute(100, 100);
      //将图片1添加到pdf文件中
      document.add(image1);
      

著者情報を追加する

 //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);  

 

HTML to PDF 

 

JavaはiText5を使用してPDFコードと関連するjarダウンロードを操作します

 

おすすめ

転載: blog.csdn.net/dkm123456/article/details/114701470