⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 htmlwriter.java

📁 一个java操作pdf文件的开发包,很好用的.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * $Id: HtmlWriter.java,v 1.73 2002/11/19 08:33:34 blowagie Exp $ * $Name:  $ * * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie. * * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the License. * * The Original Code is 'iText, a free JAVA-PDF library'. * * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. * * Contributor(s): all the names of the contributors are added in the source code * where applicable. * * Alternatively, the contents of this file may be used under the terms of the * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the * provisions of LGPL are applicable instead of those above.  If you wish to * allow use of your version of this file only under the terms of the LGPL * License and not to allow others to use your version of this file under * the MPL, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the LGPL. * If you do not delete the provisions above, a recipient may use your version * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE * * This library is free software; you can redistribute it and/or modify it * under the terms of the MPL as stated above or under the terms of the GNU * Library General Public License as published by the Free Software Foundation; * either version 2 of the License, or any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU LIBRARY GENERAL PUBLIC LICENSE for more * details. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ */package com.lowagie.text.html;import java.io.OutputStream;import java.io.IOException;import java.util.Date;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.Properties;import java.util.Stack;import java.util.EmptyStackException;import com.lowagie.text.*;import com.lowagie.text.markup.MarkupTags;/** * A <CODE>DocWriter</CODE> class for HTML. * <P> * An <CODE>HtmlWriter</CODE> can be added as a <CODE>DocListener</CODE> * to a certain <CODE>Document</CODE> by getting an instance. * Every <CODE>Element</CODE> added to the original <CODE>Document</CODE> * will be written to the <CODE>OutputStream</CODE> of this <CODE>HtmlWriter</CODE>. * <P> * Example: * <BLOCKQUOTE><PRE> * // creation of the document with a certain size and certain margins * Document document = new Document(PageSize.A4, 50, 50, 50, 50); * try { *    // this will write HTML to the Standard OutputStream *    <STRONG>HtmlWriter.getInstance(document, System.out);</STRONG> *    // this will write HTML to a file called text.html *    <STRONG>HtmlWriter.getInstance(document, new FileOutputStream("text.html"));</STRONG> *    // this will write HTML to for instance the OutputStream of a HttpServletResponse-object *    <STRONG>HtmlWriter.getInstance(document, response.getOutputStream());</STRONG> * } * catch(DocumentException de) { *    System.err.println(de.getMessage()); * } * // this will close the document and all the OutputStreams listening to it * <STRONG>document.close();</CODE> * </PRE></BLOCKQUOTE> */public class HtmlWriter extends DocWriter implements DocListener {        // static membervariables (tags)    /** This is a possible HTML-tag. */    public static final byte[] BEGINCOMMENT = getISOBytes("<!-- ");    /** This is a possible HTML-tag. */    public static final byte[] ENDCOMMENT = getISOBytes(" -->");    /** This is a possible HTML-tag. */    public static final String NBSP = "&nbsp;";        // membervariables    /** This is the current font of the HTML. */    protected Stack currentfont = new Stack();    /** This is the standard font of the HTML. */    protected Font standardfont = new Font();    /** This is a path for images. */    protected String imagepath = null;    /** Stores the page number. */    protected static int pageN = 0;    /** This is the textual part of a header */    protected HeaderFooter header = null;    /** This is the textual part of the footer */    protected HeaderFooter footer = null;        // constructor    /** * Constructs a <CODE>HtmlWriter</CODE>. * * @param doc     The <CODE>Document</CODE> that has to be written as HTML * @param os      The <CODE>OutputStream</CODE> the writer has to write to. */        protected HtmlWriter(Document doc, OutputStream os) {        super(doc, os);                document.addDocListener(this);        this.pageN = document.getPageNumber();        try {            os.write(LT);            os.write(getISOBytes(HtmlTags.HTML));            os.write(GT);            os.write(NEWLINE);            os.write(TAB);            os.write(LT);            os.write(getISOBytes(HtmlTags.HEAD));            os.write(GT);        }        catch(IOException ioe) {            throw new ExceptionConverter(ioe);        }    }        // get an instance of the HtmlWriter    /** * Gets an instance of the <CODE>HtmlWriter</CODE>. * * @param document  The <CODE>Document</CODE> that has to be written * @param os  The <CODE>OutputStream</CODE> the writer has to write to. * @return  a new <CODE>HtmlWriter</CODE> */        public static HtmlWriter getInstance(Document document, OutputStream os) {        return new HtmlWriter(document, os);    }        // implementation of the DocListener methods    /** * Signals that an new page has to be started. * * @return  <CODE>true</CODE> if this action succeeded, <CODE>false</CODE> if not. * @throws  DocumentException when a document isn't open yet, or has been closed */        public boolean newPage() throws DocumentException {        try {            writeStart(MarkupTags.DIV);            write(" ");            write(MarkupTags.STYLE);            write("=\"");            writeCssProperty(MarkupTags.PAGE_BREAK_BEFORE, MarkupTags.ALWAYS);            write("\" /");            os.write(GT);        }        catch(IOException ioe) {            throw new DocumentException(ioe.getMessage());        }        return true;    }    /** * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>. * * @return  <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not. * @throws  DocumentException when a document isn't open yet, or has been closed */        public boolean add(Element element) throws DocumentException {        if (pause) {            return false;        }        try {                        switch(element.type()) {                case Element.HEADER:                    try {                        Header h = (Header) element;                        if (MarkupTags.STYLESHEET.equals(h.name())) {                            writeLink(h);                        }                        else if (HtmlTags.JAVASCRIPT.equals(h.name())) {                            writeJavaScript(h);                        }                        else {                            writeHeader(h);                        }                    }                    catch(ClassCastException cce) {                    }                    return true;                case Element.SUBJECT:                case Element.KEYWORDS:                case Element.AUTHOR:                    Meta meta = (Meta) element;                    writeHeader(meta);                    return true;                case Element.TITLE:                    addTabs(2);                    writeStart(HtmlTags.TITLE);                    os.write(GT);                    addTabs(3);                    write(HtmlEncoder.encode(((Meta)element).content()));                    addTabs(2);                    writeEnd(HtmlTags.TITLE);                    return true;                case Element.CREATOR:                    writeComment("Creator: " + HtmlEncoder.encode(((Meta)element).content()));                    return true;                case Element.PRODUCER:                    writeComment("Producer: " + HtmlEncoder.encode(((Meta)element).content()));                    return true;                case Element.CREATIONDATE:                    writeComment("Creationdate: " + HtmlEncoder.encode(((Meta)element).content()));                    return true;                    default:                        write(element, 2);                        return true;            }        }        catch(IOException ioe) {            throw new ExceptionConverter(ioe);        }    }    /** * Signals that the <CODE>Document</CODE> has been opened and that * <CODE>Elements</CODE> can be added. * <P> * The <CODE>HEAD</CODE>-section of the HTML-document is written. */        public void open() {        super.open();        try {            writeComment("Producer: iTextXML by lowagie.com");            writeComment("CreationDate: " + new Date().toString());            addTabs(1);            writeEnd(HtmlTags.HEAD);            addTabs(1);            writeStart(HtmlTags.BODY);            if (document.leftMargin() > 0) {                write(HtmlTags.LEFTMARGIN, String.valueOf(document.leftMargin()));            }            if (document.rightMargin() > 0) {                write(HtmlTags.RIGHTMARGIN, String.valueOf(document.rightMargin()));            }            if (document.topMargin() > 0) {                write(HtmlTags.TOPMARGIN, String.valueOf(document.topMargin()));            }            if (document.bottomMargin() > 0) {                write(HtmlTags.BOTTOMMARGIN, String.valueOf(document.bottomMargin()));            }            if (pageSize.backgroundColor() != null) {                write(HtmlTags.BACKGROUNDCOLOR, HtmlEncoder.encode(pageSize.backgroundColor()));            }            if (document.getJavaScript_onLoad() != null) {                write(HtmlTags.JAVASCRIPT_ONLOAD, HtmlEncoder.encode(document.getJavaScript_onLoad()));            }            if (document.getJavaScript_onUnLoad() != null) {                write(HtmlTags.JAVASCRIPT_ONUNLOAD, HtmlEncoder.encode(document.getJavaScript_onUnLoad()));            }            if (document.getHtmlStyleClass() != null) {                write(MarkupTags.CLASS, document.getHtmlStyleClass());            }            os.write(GT);            initHeader(); // line added by David Freels        }        catch(IOException ioe) {            throw new ExceptionConverter(ioe);        }    }    /** * Signals that the <CODE>Document</CODE> was closed and that no other * <CODE>Elements</CODE> will be added. */        public void close() {        try {            initFooter(); // line added by David Freels            addTabs(1);            writeEnd(HtmlTags.BODY);            os.write(NEWLINE);            writeEnd(HtmlTags.HTML);            super.close();        }        catch(IOException ioe) {            throw new ExceptionConverter(ioe);        }    }        // some protected methods    /** * Adds the header to the top of the </CODE>Document</CODE> */        protected void initHeader() {        if (header != null) {            try {                add(header.paragraph());            }            catch(Exception e) {                throw new ExceptionConverter(e);            }        }    }    /** *  Adds the header to the top of the </CODE>Document</CODE> */        protected void initFooter() {        if (footer != null) {            try {                // Set the page number. HTML has no notion of a page, so it should always                // add up to 1                footer.setPageNumber(HtmlWriter.pageN + 1);                add(footer.paragraph());            }            catch(Exception e) {                throw new ExceptionConverter(e);            }        }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -