itext pdf download

When doing this, I encountered automatic ignoring of Chinese fonts. Some cases on the Internet are to call the fonts that come with Windows. However, in many cases, local compilation can be done, but when the project is finally placed on the server, there will be a problem that the font cannot be found and the service will not be found. The permissions are limited. The font package cannot be put into the lunx server. It is not advisable to put the font package on the lunx server. The


following


<itext.version>4.2.2</itext.version>
<itextasian.version>5.2. 0</itextasian.version>

<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
</dependency>
    <dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext- asian</artifactId>
</dependency>


version is not easy to be too low




package org.kite.flow.serviceimpl;

import java.io.FileOutputStream;

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 final class OrderTest {


public static void main( String[] args) {

  //Call the first method to generate a file named ITextTest.pdf to the C drive
  try {
   writeSimplePdf();
  }
  catch (Exception e) { e.printStackTrace(); }

 
  //Call the first The second method is to add chapters to the file named ITextTest.pdf on the C drive.
// try {
// writeCharpter();
// }
// catch (Exception e) { e.printStackTrace(); }

 
}


public static void writeSimplePdf() throws Exception {

  // 1. Create a new document object
  // The first parameter is the page size. The next parameters are the left, right, top, and bottom margins, respectively.
  Document document = new Document(PageSize.A4, 50, 50, 50, 50);

  // 2. Establish a writer (Writer) associated with the document object, through which the document (Writer) can be written to the disk.
  // Create a PdfWriter object The first parameter is a reference to the document object, and the second parameter is the actual name of the file, where its output path is also given.
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\ITextTest.pdf"));

  // 3. Open the document
  document.open();
  BaseFont baseFont = BaseFont.createFont("STSong-Light", " UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); 
 
  Font font = new Font(baseFont);

  // 4. Add content to the document
  // Add text through com.lowagie.text.Paragraph. A default paragraph can be created with text and its default font, color, size, etc. settings
  document.add(new Paragraph("First page test test of the document.",font));
  document.add(new Paragraph("Some more text on the first page with different color and font type."));
// FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new Color(255, 150, 200 ))));

   


  // 5. Close the document
  document.close();
}


/**
  * Add a pdf file with chapters
  *
  * @throws Exception
  */
public static void writeCharpter() throws Exception {

  // Create a new document object first One parameter is the page size. The next parameters are the left, right, top, and bottom margins, respectively.
  Document document = new Document(PageSize.A4, 20, 20, 20, 20);

  // Establish a writer (Writer) associated with the document object, through which the document (Writer) can be written to the disk.
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\ITextTest.pdf"));

  // open the file
  document.open();

  // 标题
  document.addTitle("Hello mingri example");

  // 作者
  document.addAuthor("wolf");

  // 主题
  document.addSubject("This example explains how to add metadata.");
  document.addKeywords("iText, Hello mingri");
  document.addCreator("My program using iText");

  // document.newPage();
  // 向文档中添加内容
  document.add(new Paragraph("\n"));
  document.add(new Paragraph("\n"));
  document.add(new Paragraph("\n"));
  document.add(new Paragraph("\n"));
  document.add(new Paragraph("\n"));
  document.add(new Paragraph("First page of the document."));
  document.add(new Paragraph("First page of the document."));
  document.add(new Paragraph("First page of the document."));
  document.add(new Paragraph("First page of the document."));
//   document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.BOLD, new Color(0, 0, 0))));
//   Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));

  // 新建章节
//   Chapter chapter1 = new Chapter(title1, 1);
//   chapter1.setNumberDepth(0);
//   Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));
//   Section section1 = chapter1.addSection(title11);
//   Paragraph someSectionText = new Paragraph("This text comes as part of section 1 of chapter 1.");
//   section1.add(someSectionText);
//   someSectionText = new Paragraph("Following is a 3 X 2 table.");
//   section1.add(someSectionText);
//   document.add(chapter1);

  // 关闭文档
  document.close();
}



}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326235610&siteId=291194637