iText jsp/htmlをpdfに変換し、ヘッダーフッターの追加をサポート

160828 ワード

会社の関连する业务はpdfを导く必要があって、各大きいウェブサイトのフォーラムを探して、それから自分でまた多くのその他のコードを総括して融合して、多くの损失を食べたことがあって、特にすべてのソースコードを投げてみんなに参考にして、私は2年研究开発してまだおかずの段阶に属します.
PDFにヘッダーフッターを直接付けるためにITextRendererを直接書き換えた
次は直接ソースに行きましょう.
関連するソースコードとjarパッケージは行けますhttp://download.csdn.net/download/patpeng/10202716ダウンロード
HTML









<c:set var="ctx" value=""/>




<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>pdf  title>
<style type="text/css">
*{
font-size: 10px;
font-family:SimSun;/*   ,     */
}
body{
background-color:#fff;
font-family: SimSun;
}
.tb-group-btn{border-color:#C9C9C9}
div.answerBox{
font-weight:700;
}
style>
<script src="${ctx}/js/jquery.min.js">script>
head>
<body style="width:auto;height:100%;">

<div id="donotExport" style="float:right;border:0;padding:0;width:100%">
div>
<div id="needPrint">
<div class="line" style="width:100%;margin-top:30px" align="center">
<strong style="font-size:20px;">  pdfstrong>
div>
<h4 style="margin:10px 0;font-weight:600">  pdfh4>
<div>
<table border="1" style="border:#7D7D7D;width:100%;text-align:center;border-collapse:collapse;color:#676767;font-size:16px;line-height:30px">
<tbody>
<tr style="">
<td style="size:18px">  pdftd>
<td>  pdftd>
<td>  pdftd>
<td>  pdftd>
<td>  pdftd>
<td>  pdftd>
tr>
<tr align="center">
<td style="size:18px">  pdftd>
<td>  pdftd>
<td>  pdftd>
<td>  pdftd>
<td>  pdftd>
<td>  pdftd>
tr>
tbody>
table>
div>
<h4 style="margin:20px 0;font-weight:600">  pdfh4>
div>
body>
html>

ItextPdfUtil
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.xhtmlrenderer.pdf.ITextFontResolver;


import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;


public class ItextPdfUtil {
    /**
     *   url      pdf
     * @param url:jsp   url
     * @param fileName:   PDF   
     * @param request
     * @param response
     * @param flag :String[] 0:  ;1  ;2  ; null        
     *
     * @version:v4.0
     * @author:pengkz
     * @date:2018 1 3    4:58:25
     */
    public static void html2Pdf(String  url,String fileName,HttpServletRequest request,HttpServletResponse response,String[] flag){       
   
    OutputStream outputStream = null;
    ByteArrayInputStream inputStream = null;
try {
inputStream = getPDFInputStreamByURL(url, request,flag);

outputStream = response.getOutputStream();
    response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("gbk"), "iso8859-1"));
    int b;  
    while((b=inputStream.read())!= -1)  
    {  
    outputStream.write(b);  
    }  
    outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
    try {
    if (outputStream!=null) {
    outputStream.close();
    outputStream=null;
    }
    if (inputStream!=null) {
    inputStream.close();
        inputStream=null;
}
} catch (IOException e) {
e.printStackTrace();
}
}       
    }
/**
*   url    jsp    
* @param url 
* @return
* @throws Exception
*
* @version:v2.0
* @author:pengkz
* @date:2017 7 21    2:58:31
*/
public static String getHTMLSourceByUrl(String url) throws Exception {
URL urls=new URL(url);
HttpURLConnection conn = (HttpURLConnection) urls.openConnection();
conn.setRequestProperty("Content-type", "text/html");
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestProperty("contentType", "utf-8");
conn.setRequestProperty("Charset", "utf-8");
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream(); //        html     
byte[] data = readInputStream(inStream); //          byte    
String htmlSource = new String(data,"utf-8");//      
conn.disconnect();
if (inStream!=null) {
inStream.close();
inStream=null;
}
return htmlSource;
}

   
/**
*             
* @param inputStream
* @return
* @throws IOException
*
* @version:v2.0
* @author:pengkz
* @date:2017 7 21    2:58:19
*/
    private static  byte[] readInputStream(InputStream inputStream) throws IOException {    
        byte[] buffer = new byte[1024];    
        int len = 0;    
        ByteArrayOutputStream bos = new ByteArrayOutputStream();    
        while((len = inputStream.read(buffer)) != -1) {    
            bos.write(buffer, 0, len);    
        }    
        bos.close();    
        return bos.toByteArray();    
    } 
    



    /**
     *   URL   pdf inputstream 
     * @param url
     * @param request
     * @param flag String[],         0:    /1:     /2:     
     * @return
     * @throws DocumentException
     * @throws IOException
     * @throws Exception
     *
     * @version:v2.0
     * @author:pengkz
     * @date:2017 7 28    3:46:21
     */
public static ByteArrayInputStream getPDFInputStreamByURL(String url, HttpServletRequest request,String[] flag)
throws DocumentException, IOException, Exception {
ByteArrayOutputStream os = null;
ITextRendererO renderer = new ITextRendererO();
String[] array=new String[3];
if (flag.length>=3) {
array=flag;
}else {
for (int i = 0; i < flag.length; i++) {
array[i]=flag[i];
}
}
ITextFontResolver fontResolver = renderer.getFontResolver();
String apath=request.getSession().getServletContext().getRealPath("/");
//      
String templateFilePath = apath.replace("\\", "/").replaceAll(request.getContextPath(), "")+"simsun.ttc";//         
fontResolver.addFont(templateFilePath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
String htmlss = getHTMLSourceByUrl(url);
renderer.setDocumentFromString(htmlss);
renderer.layout();
os = new ByteArrayOutputStream();
renderer.createPDF(os,templateFilePath,array);
ByteArrayInputStream inputStream = new ByteArrayInputStream(os.toByteArray());
renderer=null;
os.close();
os=null;
return inputStream;
} 
    
}


ITextRendererO

/*
 * {{{ header & license
 * Copyright (c) 2006 Wisconsin Court System
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * }}}
 */



import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Shape;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.List;
import java.util.regex.Pattern;


import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xhtmlrenderer.context.StyleReference;
import org.xhtmlrenderer.css.style.CalculatedStyle;
import org.xhtmlrenderer.extend.NamespaceHandler;
import org.xhtmlrenderer.extend.UserInterface;
import org.xhtmlrenderer.layout.BoxBuilder;
import org.xhtmlrenderer.layout.Layer;
import org.xhtmlrenderer.layout.LayoutContext;
import org.xhtmlrenderer.layout.SharedContext;
import org.xhtmlrenderer.pdf.ITextFontContext;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextOutputDevice;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xhtmlrenderer.pdf.ITextReplacedElementFactory;
import org.xhtmlrenderer.pdf.ITextTextRenderer;
import org.xhtmlrenderer.pdf.ITextUserAgent;
import org.xhtmlrenderer.pdf.PDFCreationListener;
import org.xhtmlrenderer.pdf.PDFEncryption;
import org.xhtmlrenderer.render.BlockBox;
import org.xhtmlrenderer.render.PageBox;
import org.xhtmlrenderer.render.RenderingContext;
import org.xhtmlrenderer.render.ViewportBox;
import org.xhtmlrenderer.resource.XMLResource;
import org.xhtmlrenderer.simple.extend.XhtmlNamespaceHandler;
import org.xhtmlrenderer.util.Configuration;
import org.xml.sax.InputSource;


import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;


public class ITextRendererO extends ITextRenderer {
// These two defaults combine to produce an effective resolution of 96 px to
    // the inch
    private static final float DEFAULT_DOTS_PER_POINT = 20f * 4f / 3f;
    private static final int DEFAULT_DOTS_PER_PIXEL = 20;


    private final SharedContext _sharedContext;
    private final ITextOutputDevice _outputDevice;


    private Document _doc;
    private BlockBox _root;


    private final float _dotsPerPoint;


    private com.lowagie.text.Document _pdfDoc;
    private PdfWriter _writer;


    private PDFEncryption _pdfEncryption;


    // note: not hard-coding a default version in the _pdfVersion field as this
    // may change between iText releases
    // check for null before calling writer.setPdfVersion()
    // use one of the values in PDFWriter.VERSION...
    private Character _pdfVersion;


    private final char[] validPdfVersions = new char[] { PdfWriter.VERSION_1_2, PdfWriter.VERSION_1_3, PdfWriter.VERSION_1_4,
            PdfWriter.VERSION_1_5, PdfWriter.VERSION_1_6, PdfWriter.VERSION_1_7 };


    private Integer _pdfXConformance;


private PDFCreationListener _listener;


    private boolean _timeouted;


    public ITextRendererO() {
        this(DEFAULT_DOTS_PER_POINT, DEFAULT_DOTS_PER_PIXEL);
    }


    public ITextRendererO(float dotsPerPoint, int dotsPerPixel) {
        this(dotsPerPoint, dotsPerPixel, new ITextOutputDevice(dotsPerPoint));
    }


    public ITextRendererO(float dotsPerPoint, int dotsPerPixel, ITextOutputDevice outputDevice) {
        this(dotsPerPoint, dotsPerPixel, outputDevice, new ITextUserAgent(outputDevice));
    }


    public ITextRendererO(float dotsPerPoint, int dotsPerPixel, ITextOutputDevice outputDevice, ITextUserAgent userAgent) {
        _dotsPerPoint = dotsPerPoint;


        _outputDevice = outputDevice;


        _sharedContext = new SharedContext();
        _sharedContext.setUserAgentCallback(userAgent);
        _sharedContext.setCss(new StyleReference(userAgent));
        userAgent.setSharedContext(_sharedContext);
        _outputDevice.setSharedContext(_sharedContext);


        ITextFontResolver fontResolver = new ITextFontResolver(_sharedContext);
        _sharedContext.setFontResolver(fontResolver);


        ITextReplacedElementFactory replacedElementFactory = new ITextReplacedElementFactory(_outputDevice);
        _sharedContext.setReplacedElementFactory(replacedElementFactory);


        _sharedContext.setTextRenderer(new ITextTextRenderer());
        _sharedContext.setDPI(72 * _dotsPerPoint);
        _sharedContext.setDotsPerPixel(dotsPerPixel);
        _sharedContext.setPrint(true);
        _sharedContext.setInteractive(false);


        _timeouted= false;
    }


    public Document getDocument() {
        return _doc;
    }


    public ITextFontResolver getFontResolver() {
        return (ITextFontResolver) _sharedContext.getFontResolver();
    }


    private Document loadDocument(final String uri) {
        return _sharedContext.getUac().getXMLResource(uri).getDocument();
    }


    public void setDocument(String uri) {
        setDocument(loadDocument(uri), uri);
    }


    public void setDocument(Document doc, String url) {
        setDocument(doc, url, new XhtmlNamespaceHandler());
    }


    public void setDocument(File file) throws IOException {


        File parent = file.getAbsoluteFile().getParentFile();
        setDocument(loadDocument(file.toURI().toURL().toExternalForm()), (parent == null ? "" : parent.toURI().toURL().toExternalForm()));
    }


    public void setDocumentFromString(String content) {
        setDocumentFromString(content, null);
    }


    public void setDocumentFromString(String content, String baseUrl) {
        InputSource is = new InputSource(new BufferedReader(new StringReader(content)));
        Document dom = XMLResource.load(is).getDocument();


        setDocument(dom, baseUrl);
    }


    public void setDocument(Document doc, String url, NamespaceHandler nsh) {
        _doc = doc;


        getFontResolver().flushFontFaceFonts();


        _sharedContext.reset();
        if (Configuration.isTrue("xr.cache.stylesheets", true)) {
            _sharedContext.getCss().flushStyleSheets();
        } else {
            _sharedContext.getCss().flushAllStyleSheets();
        }
        _sharedContext.setBaseURL(url);
        _sharedContext.setNamespaceHandler(nsh);
        _sharedContext.getCss().setDocumentContext(_sharedContext, _sharedContext.getNamespaceHandler(), doc, new NullUserInterface());
        getFontResolver().importFontFaces(_sharedContext.getCss().getFontFaceRules());
    }


    public PDFEncryption getPDFEncryption() {
        return _pdfEncryption;
    }


    public void setPDFEncryption(PDFEncryption pdfEncryption) {
        _pdfEncryption = pdfEncryption;
    }


    public void setPDFVersion(char _v) {
        for (int i = 0; i < validPdfVersions.length; i++) {
            if (_v == validPdfVersions[i]) {
                _pdfVersion = new Character(_v);
                return;
            }
        }
        throw new IllegalArgumentException("Invalid PDF version character; use "
                + "valid constants from PdfWriter (e.g. PdfWriter.VERSION_1_2)");
    }


    public char getPDFVersion() {
        return _pdfVersion == null ? '0' : _pdfVersion.charValue();
    }

public void setPDFXConformance(int pdfXConformance){
_pdfXConformance = new Integer(pdfXConformance);
}

public int getPDFXConformance(){
        return _pdfXConformance == null ? '0' : _pdfXConformance.intValue();
}




    public void layout() {
        LayoutContext c = newLayoutContext();
        BlockBox root = BoxBuilder.createRootBox(c, _doc);
        root.setContainingBlock(new ViewportBox(getInitialExtents(c)));
        root.layout(c);
        Dimension dim = root.getLayer().getPaintingDimension(c);
        root.getLayer().trimEmptyPages(c, dim.height);
        root.getLayer().layoutPages(c);
        _root = root;
    }


    private Rectangle getInitialExtents(LayoutContext c) {
        PageBox first = Layer.createPageBox(c, "first");


        return new Rectangle(0, 0, first.getContentWidth(c), first.getContentHeight(c));
    }


    private RenderingContext newRenderingContext() {
        RenderingContext result = _sharedContext.newRenderingContextInstance();
        result.setFontContext(new ITextFontContext());


        result.setOutputDevice(_outputDevice);


        _sharedContext.getTextRenderer().setup(result.getFontContext());


        result.setRootLayer(_root.getLayer());


        return result;
    }


    private LayoutContext newLayoutContext() {
        LayoutContext result = _sharedContext.newLayoutContextInstance();
        result.setFontContext(new ITextFontContext());


        _sharedContext.getTextRenderer().setup(result.getFontContext());


        return result;
    }


    public void createPDF(OutputStream os,String templateFilePath,String ...flag) throws DocumentException {
        createPDF(os, true, 0,templateFilePath,flag);
    }


    public void writeNextDocument() throws DocumentException {
        writeNextDocument(0);
    }


    public void writeNextDocument(int initialPageNo) throws DocumentException {
        List pages = _root.getLayer().getPages();


        RenderingContext c = newRenderingContext();
        c.setInitialPageNo(initialPageNo);
        PageBox firstPage = (PageBox) pages.get(0);
        com.lowagie.text.Rectangle firstPageSize = new com.lowagie.text.Rectangle(0, 0, firstPage.getWidth(c) / _dotsPerPoint,
                firstPage.getHeight(c) / _dotsPerPoint);


        _outputDevice.setStartPageNo(_writer.getPageNumber());


        _pdfDoc.setPageSize(firstPageSize);
        _pdfDoc.newPage();


        writePDF(pages, c, firstPageSize, _pdfDoc, _writer);
    }


    public void finishPDF() {
        if (_pdfDoc != null) {
            fireOnClose();
            _pdfDoc.close();
        }
    }


    public void createPDF(OutputStream os, boolean finish) throws DocumentException {
        createPDF(os, finish, 0);
    }


    /**
     * NOTE: Caller is responsible for cleaning up the OutputStream if
     * something goes wrong.
     * flag Object[],         0:    /1:     /2:     
     */
    public void createPDF(OutputStream os, boolean finish, int initialPageNo,String templateFilePath,String ...flag) throws DocumentException {
        List pages = _root.getLayer().getPages();


        RenderingContext c = newRenderingContext();
        c.setInitialPageNo(initialPageNo);
        PageBox firstPage = (PageBox) pages.get(0);
        com.lowagie.text.Rectangle firstPageSize = new com.lowagie.text.Rectangle(0, 0, firstPage.getWidth(c) / _dotsPerPoint,
                firstPage.getHeight(c) / _dotsPerPoint);


        com.lowagie.text.Document doc = new com.lowagie.text.Document(firstPageSize, 0,0, 20, 10);
        PdfWriter writer = PdfWriter.getInstance(doc, os);
        if (flag!=null&&flag.length>0) {

        //        
        BaseFont fontChinese=null;  
        try {  
        fontChinese = BaseFont.createFont(templateFilePath+",1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED,false);//    
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        Font chinese = new Font(fontChinese, 10, Font.NORMAL);   
        
        /** 
         * HeaderFooter  2      false        
         *             ,        
         */  
        if (StringUtils.isNotBlank(flag[0])) {
        HeaderFooter header=new HeaderFooter(new Phrase(flag[0]+"     ",chinese),false);  
        //          
        //      header.setBorder(Rectangle.NO_BORDER);  
        header.setAlignment(2);  
        header.setBorder(Font.NORMAL);  
        doc.setHeader(header);  
}
        if (StringUtils.isNotBlank(flag[1])&&StringUtils.isNotBlank(flag[2])) {//         
        HeaderFooterfooter=new HeaderFooter(new Phrase(flag[1]+" | "+flag[2]+"  ",chinese),false);  
        /** 
        * 0    
        * 1    
        * 2    
        */  
        footer.setBorder(Font.NORMAL);
        footer.setAlignment(1); 
        //  footer.setBorderColor(Color.red);  
        doc.setFooter(footer);  
}
        //        
        }
        
        if (_pdfVersion != null) {
            writer.setPdfVersion(_pdfVersion.charValue());
        }

if (_pdfXConformance != null) {
writer.setPDFXConformance(_pdfXConformance.intValue());
}


        if (_pdfEncryption != null) {
            writer.setEncryption(_pdfEncryption.getUserPassword(), _pdfEncryption.getOwnerPassword(),
                    _pdfEncryption.getAllowedPrivileges(), _pdfEncryption.getEncryptionType());
        }
        _pdfDoc = doc;
        _writer = writer;


        firePreOpen();
        doc.open();


        writePDF(pages, c, firstPageSize, doc, writer);


        if (finish) {
            fireOnClose();
            doc.close();
        }
    }


    private void firePreOpen() {
        if (_listener != null) {
            _listener.preOpen(this);
        }
    }


    private void firePreWrite(int pageCount) {
        if (_listener != null) {
            _listener.preWrite(this, pageCount);
        }
    }


    private void fireOnClose() {
        if (_listener != null) {
            _listener.onClose(this);
        }
    }


    private void writePDF(List pages, RenderingContext c, com.lowagie.text.Rectangle firstPageSize, com.lowagie.text.Document doc,
            PdfWriter writer) throws DocumentException {
        _outputDevice.setRoot(_root);


        _outputDevice.start(_doc);
        _outputDevice.setWriter(writer);
        _outputDevice.initializePage(writer.getDirectContent(), firstPageSize.getHeight());


        _root.getLayer().assignPagePaintingPositions(c, Layer.PAGED_MODE_PRINT);


        int pageCount = _root.getLayer().getPages().size();
        c.setPageCount(pageCount);
        firePreWrite(pageCount); // opportunity to adjust meta data
        setDidValues(doc); // set PDF header fields from meta data
        for (int i = 0; i < pageCount; i++) {


            if (isTimeouted() || Thread.currentThread().isInterrupted())
                throw new RuntimeException("Timeout occured");


            PageBox currentPage = (PageBox) pages.get(i);
            c.setPage(i, currentPage);
            paintPage(c, writer, currentPage);
            _outputDevice.finishPage();
            if (i != pageCount - 1) {
                PageBox nextPage = (PageBox) pages.get(i + 1);
                com.lowagie.text.Rectangle nextPageSize = new com.lowagie.text.Rectangle(0, 0, nextPage.getWidth(c) / _dotsPerPoint,
                        nextPage.getHeight(c) / _dotsPerPoint);
                doc.setPageSize(nextPageSize);
                doc.newPage();
                _outputDevice.initializePage(writer.getDirectContent(), nextPageSize.getHeight());
            }
        }


        _outputDevice.finish(c, _root);
    }


    // Sets the document information dictionary values from html metadata
    private void setDidValues(com.lowagie.text.Document doc) {
        String v = _outputDevice.getMetadataByName("title");
        if (v != null) {
            doc.addTitle(v);
        }
        v = _outputDevice.getMetadataByName("author");
        if (v != null) {
            doc.addAuthor(v);
        }
        v = _outputDevice.getMetadataByName("subject");
        if (v != null) {
            doc.addSubject(v);
        }
        v = _outputDevice.getMetadataByName("keywords");
        if (v != null) {
            doc.addKeywords(v);
        }
    }


    private void paintPage(RenderingContext c, PdfWriter writer, PageBox page) {
        provideMetadataToPage(writer, page);


        page.paintBackground(c, 0, Layer.PAGED_MODE_PRINT);
        page.paintMarginAreas(c, 0, Layer.PAGED_MODE_PRINT);
        page.paintBorder(c, 0, Layer.PAGED_MODE_PRINT);


        Shape working = _outputDevice.getClip();


        Rectangle content = page.getPrintClippingBounds(c);
        _outputDevice.clip(content);


        int top = -page.getPaintingTop() + page.getMarginBorderPadding(c, CalculatedStyle.TOP);


        int left = page.getMarginBorderPadding(c, CalculatedStyle.LEFT);


        _outputDevice.translate(left, top);
        _root.getLayer().paint(c);
        _outputDevice.translate(-left, -top);


        _outputDevice.setClip(working);
    }


    private void provideMetadataToPage(PdfWriter writer, PageBox page) {
        byte[] metadata = null;
        if (page.getMetadata() != null) {
            try {
                String metadataBody = stringfyMetadata(page.getMetadata());
                if (metadataBody != null) {
                    metadata = createXPacket(stringfyMetadata(page.getMetadata())).getBytes("UTF-8");
                }
            } catch (UnsupportedEncodingException e) {
                // Can't happen
                throw new RuntimeException(e);
            }
        }


        if (metadata != null) {
            writer.setPageXmpMetadata(metadata);
        }
    }


    private String stringfyMetadata(Element element) {
        Element target = getFirstChildElement(element);
        if (target == null) {
            return null;
        }


        try {
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            StringWriter output = new StringWriter();
            transformer.transform(new DOMSource(target), new StreamResult(output));


            return output.toString();
        } catch (TransformerConfigurationException e) {
            // Things must be in pretty bad shape to get here so
            // rethrow as runtime exception
            throw new RuntimeException(e);
        } catch (TransformerException e) {
            throw new RuntimeException(e);
        }
    }


    private static Element getFirstChildElement(Element element) {
        Node n = element.getFirstChild();
        while (n != null) {
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) n;
            }
            n = n.getNextSibling();
        }
        return null;
    }


    private String createXPacket(String metadata) {
        StringBuffer result = new StringBuffer(metadata.length() + 50);
        result.append("
"
); result.append(metadata); result.append("
"
); return result.toString(); } public ITextOutputDevice getOutputDevice() { return _outputDevice; } public SharedContext getSharedContext() { return _sharedContext; } public void exportText(Writer writer) throws IOException { RenderingContext c = newRenderingContext(); c.setPageCount(_root.getLayer().getPages().size()); _root.exportText(c, writer); } public BlockBox getRootBox() { return _root; } public float getDotsPerPoint() { return _dotsPerPoint; } public List findPagePositionsByID(Pattern pattern) { return _outputDevice.findPagePositionsByID(newLayoutContext(), pattern); } private static final class NullUserInterface implements UserInterface { public boolean isHover(Element e) { return false; } public boolean isActive(Element e) { return false; } public boolean isFocus(Element e) { return false; } } public PDFCreationListener getListener() { return _listener; } public void setListener(PDFCreationListener listener) { _listener = listener; } public PdfWriter getWriter() { return _writer; } public void setTimeouted(boolean timeouted) { _timeouted= timeouted; } public boolean isTimeouted() { return _timeouted; } }