pdfcontentbyte.java

来自「有关对pdf操作的代码」· Java 代码 · 共 1,856 行 · 第 1/5 页

JAVA
1,856
字号
     * @param a an element of the transformation matrix     * @param b an element of the transformation matrix     * @param c an element of the transformation matrix     * @param d an element of the transformation matrix     * @param e an element of the transformation matrix     * @param f an element of the transformation matrix     * @throws DocumentException on error     */    public void addImage(Image image, float a, float b, float c, float d, float e, float f) throws DocumentException {        addImage(image, a, b, c, d, e, f, false);    }        /**     * Adds an <CODE>Image</CODE> to the page. The positioning of the <CODE>Image</CODE>     * is done with the transformation matrix. To position an <CODE>image</CODE> at (x,y)     * use addImage(image, image_width, 0, 0, image_height, x, y). The image can be placed inline.     * @param image the <CODE>Image</CODE> object     * @param a an element of the transformation matrix     * @param b an element of the transformation matrix     * @param c an element of the transformation matrix     * @param d an element of the transformation matrix     * @param e an element of the transformation matrix     * @param f an element of the transformation matrix     * @param inlineImage <CODE>true</CODE> to place this image inline, <CODE>false</CODE> otherwise     * @throws DocumentException on error     */    public void addImage(Image image, float a, float b, float c, float d, float e, float f, boolean inlineImage) throws DocumentException {        try {            if (image.getLayer() != null)                beginLayer(image.getLayer());            if (image.isImgTemplate()) {                writer.addDirectImageSimple(image);                PdfTemplate template = image.getTemplateData();                float w = template.getWidth();                float h = template.getHeight();                addTemplate(template, a / w, b / w, c / h, d / h, e, f);            }            else {                content.append("q ");                content.append(a).append(' ');                content.append(b).append(' ');                content.append(c).append(' ');                content.append(d).append(' ');                content.append(e).append(' ');                content.append(f).append(" cm");                if (inlineImage) {                    content.append("\nBI\n");                    PdfImage pimage = new PdfImage(image, "", null);                    for (Iterator it = pimage.getKeys().iterator(); it.hasNext();) {                        PdfName key = (PdfName)it.next();                        PdfObject value = pimage.get(key);                        String s = (String)abrev.get(key);                        if (s == null)                            continue;                        content.append(s);                        boolean check = true;                        if (key.equals(PdfName.COLORSPACE) && value.isArray()) {                            ArrayList ar = ((PdfArray)value).getArrayList();                            if (ar.size() == 4                                 && PdfName.INDEXED.equals(ar.get(0))                                 && ((PdfObject)ar.get(1)).isName()                                && ((PdfObject)ar.get(2)).isNumber()                                && ((PdfObject)ar.get(3)).isString()                            ) {                                check = false;                            }                                                    }                        if (check && key.equals(PdfName.COLORSPACE) && !value.isName()) {                            PdfName cs = writer.getColorspaceName();                            PageResources prs = getPageResources();                            prs.addColor(cs, writer.addToBody(value).getIndirectReference());                            value = cs;                        }                        value.toPdf(null, content);                        content.append('\n');                    }                    content.append("ID\n");                    pimage.writeContent(content);                    content.append("\nEI\nQ").append_i(separator);                }                else {                    PdfName name;                    PageResources prs = getPageResources();                    Image maskImage = image.getImageMask();                    if (maskImage != null) {                        name = writer.addDirectImageSimple(maskImage);                        prs.addXObject(name, writer.getImageReference(name));                    }                    name = writer.addDirectImageSimple(image);                    name = prs.addXObject(name, writer.getImageReference(name));                    content.append(' ').append(name.getBytes()).append(" Do Q").append_i(separator);                }            }            if (image.hasBorders()) {                saveState();                float w = image.getWidth();                float h = image.getHeight();                concatCTM(a / w, b / w, c / h, d / h, e, f);                rectangle(image);                restoreState();            }            if (image.getLayer() != null)                endLayer();            Annotation annot = image.getAnnotation();            if (annot == null)                return;            float[] r = new float[unitRect.length];            for (int k = 0; k < unitRect.length; k += 2) {                r[k] = a * unitRect[k] + c * unitRect[k + 1] + e;                r[k + 1] = b * unitRect[k] + d * unitRect[k + 1] + f;            }            float llx = r[0];            float lly = r[1];            float urx = llx;            float ury = lly;            for (int k = 2; k < r.length; k += 2) {                llx = Math.min(llx, r[k]);                lly = Math.min(lly, r[k + 1]);                urx = Math.max(urx, r[k]);                ury = Math.max(ury, r[k + 1]);            }            annot = new Annotation(annot);            annot.setDimensions(llx, lly, urx, ury);            PdfAnnotation an = PdfAnnotationsImp.convertAnnotation(writer, annot, new Rectangle(llx, lly, urx, ury));            if (an == null)                return;            addAnnotation(an);        }        catch (Exception ee) {            throw new DocumentException(ee);        }    }        /**     * Makes this <CODE>PdfContentByte</CODE> empty.     */    public void reset() {        content.reset();        stateList.clear();        state = new GraphicState();    }        /**     * Starts the writing of text.     */    public void beginText() {        state.xTLM = 0;        state.yTLM = 0;        content.append("BT").append_i(separator);    }        /**     * Ends the writing of text and makes the current font invalid.     */    public void endText() {        content.append("ET").append_i(separator);    }        /**     * Saves the graphic state. <CODE>saveState</CODE> and     * <CODE>restoreState</CODE> must be balanced.     */    public void saveState() {        content.append("q").append_i(separator);        stateList.add(new GraphicState(state));    }        /**     * Restores the graphic state. <CODE>saveState</CODE> and     * <CODE>restoreState</CODE> must be balanced.     */    public void restoreState() {        content.append("Q").append_i(separator);        int idx = stateList.size() - 1;        if (idx < 0)            throw new RuntimeException("Unbalanced save/restore state operators.");        state = (GraphicState)stateList.get(idx);        stateList.remove(idx);    }        /**     * Sets the character spacing parameter.     *     * @param       charSpace           a parameter     */    public void setCharacterSpacing(float charSpace) {        state.charSpace = charSpace;        content.append(charSpace).append(" Tc").append_i(separator);    }        /**     * Sets the word spacing parameter.     *     * @param       wordSpace           a parameter     */    public void setWordSpacing(float wordSpace) {        state.wordSpace = wordSpace;        content.append(wordSpace).append(" Tw").append_i(separator);    }        /**     * Sets the horizontal scaling parameter.     *     * @param       scale               a parameter     */    public void setHorizontalScaling(float scale) {        state.scale = scale;        content.append(scale).append(" Tz").append_i(separator);    }        /**     * Sets the text leading parameter.     * <P>     * The leading parameter is measured in text space units. It specifies the vertical distance     * between the baselines of adjacent lines of text.</P>     *     * @param       leading         the new leading     */    public void setLeading(float leading) {        state.leading = leading;        content.append(leading).append(" TL").append_i(separator);    }        /**     * Set the font and the size for the subsequent text writing.     *     * @param bf the font     * @param size the font size in points     */    public void setFontAndSize(BaseFont bf, float size) {        checkWriter();        if (size < 0.0001f && size > -0.0001f)            throw new IllegalArgumentException("Font size too small: " + size);        state.size = size;        state.fontDetails = writer.addSimple(bf);        PageResources prs = getPageResources();        PdfName name = state.fontDetails.getFontName();        name = prs.addFont(name, state.fontDetails.getIndirectReference());        content.append(name.getBytes()).append(' ').append(size).append(" Tf").append_i(separator);    }        /**     * Sets the text rendering parameter.     *     * @param       rendering               a parameter     */    public void setTextRenderingMode(int rendering) {        content.append(rendering).append(" Tr").append_i(separator);    }        /**     * Sets the text rise parameter.     * <P>     * This allows to write text in subscript or superscript mode.</P>     *     * @param       rise                a parameter     */    public void setTextRise(float rise) {        content.append(rise).append(" Ts").append_i(separator);    }        /**     * A helper to insert into the content stream the <CODE>text</CODE>     * converted to bytes according to the font's encoding.     *     * @param text the text to write     */    private void showText2(String text) {        if (state.fontDetails == null)            throw new NullPointerException("Font and size must be set before writing any text");        byte b[] = state.fontDetails.convertToBytes(text);        escapeString(b, content);    }        /**     * Shows the <CODE>text</CODE>.     *     * @param text the text to write     */    public void showText(String text) {        showText2(text);        content.append("Tj").append_i(separator);    }        /**     * Constructs a kern array for a text in a certain font     * @param text the text     * @param font the font     * @return a PdfTextArray     */    public static PdfTextArray getKernArray(String text, BaseFont font) {        PdfTextArray pa = new PdfTextArray();        StringBuffer acc = new StringBuffer();        int len = text.length() - 1;        char c[] = text.toCharArray();        if (len >= 0)            acc.append(c, 0, 1);        for (int k = 0; k < len; ++k) {            char c2 = c[k + 1];            int kern = font.getKerning(c[k], c2);            if (kern == 0) {                acc.append(c2);            }            else {                pa.add(acc.toString());                acc.setLength(0);                acc.append(c, k + 1, 1);                pa.add(-kern);            }        }        pa.add(acc.toString());        return pa;    }        /**     * Shows the <CODE>text</CODE> kerned.     *     * @param text the text to write     */    public void showTextKerned(String text) {        if (state.fontDetails == null)            throw new NullPointerException("Font and size must be set before writing any text");        BaseFont bf = state.fontDetails.getBaseFont();        if (bf.hasKernPairs())            showText(getKernArray(text, bf));        else            showText(text);    }        /**     * Moves to the next line and shows <CODE>text</CODE>.     *     * @param text the text to write     */    public void newlineShowText(String text) {        state.yTLM -= state.leading;        showText2(text);        content.append("'").append_i(separator);    }        /**     * Moves to the next line and shows text string, using the given values of the character and word spacing parameters.     *     * @param       wordSpacing     a parameter     * @param       charSpacing     a parameter     * @param text the text to write     */    public void newlineShowText(float wordSpacing, float charSpacing, String text) {        state.yTLM -= state.leading;        content.append(wordSpacing).append(' ').append(charSpacing);        showText2(text);        content.append("\"").append_i(separator);                // The " operator sets charSpace and wordSpace into graphics state        // (cfr PDF reference v1.6, table 5.6)        state.charSpace = charSpacing;        state.wordSpace = wordSpacing;    }        /**     * Changes the text matrix.     * <P>     * Remark: this operation also initializes the current point position.</P>     *     * @param       a           operand 1,1 in the matrix     * @param       b           operand 1,2 in the matrix     * @param       c           operand 2,1 in the matrix     * @param       d           operand 2,2 in the matrix     * @param       x           operand 3,1 in the matrix     * @param       y           operand 3,2 in the matrix     */

⌨️ 快捷键说明

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