Java 打印到PDF文件

 import java.io.*;
import java.text.*;
import java.util.*;


/** A simple text test of SPDF package
 */
public class PDFDemo {
  public static void main(String[] argvthrows IOException {
    PrintWriter pout;
    if (argv.length == 0) {
      pout = new PrintWriter(System.out);
    else {
      if (new File(argv[0]).exists()) {
        throw new IOException(
        "Output file " + argv[0" already exists");
      }
      pout = new PrintWriter(new FileWriter(argv[0]));
    }
    PDF p = new PDF(pout);
    Page p1 = new Page(p);
    p1.add(new MoveTo(p, 100600));
    p1.add(new Text(p, "Hello world, live on the web."));
    p1.add(new Text(p, "Hello world, line 2 on the web."));
    p.add(p1);
    p.setAuthor("Ian F. Darwin");
    p.writePDF();
  }
}
/** The main class for the Darwin Open Systems
 * {Simple,Stupid,Simplistic} PDF API.
 * PDF is Adobe's Portable Document Format, and is probably a trademark
 * of Adobe Systems Inc, Mountain View, California.
 * The Adobe PDF Specification which they publish grants everyone
 * permission to write code to generate and/or process PDF files.
 * A PDF Object represents one PDF file.
 @author Ian F. Darwin, http://www.darwinsys.com/
 @version $Id: PDF.java,v 1.6 2004/02/09 03:34:02 ian Exp $
 */
class PDF {
  /** The output writer */
  protected PrintWriter out;

  /** The list of pages */
  protected ArrayList pages;

  /** The list of object xrefs */
  protected ArrayList xrefs;

  /** The root object */
  PDFObject rootObj = new RootObject(this);

  /** The Info object */
  InfoObject infoObj = new InfoObject(this);

  /** The outlines (outline font) object */
  OutlinesObject outlinesObj = new OutlinesObject(this);

  /** The Pages object */
  PagesObject pagesObj = new PagesObject(this);

  /** The Font Dictionary */
  FontDict fontDict = new FontDict(this);

  /** The object number of the current object */
  protected int currObj = 1;

  /** A flag to avoid writing twice */
  protected boolean startedWriting = false;

  /** A magic number that identifies the output as a PDF file */
  protected final static String PDF_MAGIC = "%PDF-1.0";

  /** Constructor */
  public PDF(PrintWriter o) {
    out = o;

    pages = new ArrayList();
    xrefs = new ArrayList();

  }

  public void add(Page p) {
    pages.add(p);
  }

  public void insertPage(int where, Page p) {
    pages.add(where, p);
  }

  // OUTPUT METHODS -- we provide our own print/println, so we
  // can keep track of file offsets (it was either that, or kludgily
  // use a RandomAccessFile and the getFilePointer() method).

  long offset = 0;

  /** Print a String */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值