Java com.lowagie.text.Image 代码实例

・49 分钟阅读

以下是展示如何使用com.lowagie.text.Image的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


/**
 * Adds an image to this Cell.
 *
 * @param i           the image to add
 * @param left        the left border
 * @param right       the right border
 * @param extraHeight extra height to add 上面 image
 * @param alignment   horizontal alignment (constant from Element class)
 * @return the height of the image
 */
private float addImage(Image i, float left, float right, float extraHeight, int alignment) {
    Image image = Image.getInstance(i);
    if (image.getScaledWidth() > right - left) {
        image.scaleToFit(right - left, Float.MAX_VALUE);
    }
    flushCurrentLine();
    if (line == null) {
        line = new PdfLine(left, right, alignment, leading);
    }
    PdfLine imageLine = line;
    // left and right in chunk is relative to the start of the line
    right = right - left;
    left = 0f;
    if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) {
        left = right - image.getScaledWidth();
    } else if ((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE) {
        left = left + ((right - left - image.getScaledWidth()) / 2f);
    }
    Chunk imageChunk = new Chunk(image, left, 0);
    imageLine.add(new PdfChunk(imageChunk, null));
    addLine(imageLine);
    return imageLine.height();
}
 

实例 2


/**
  * Adds some annotated images to a PDF file.
  * @param args no arguments needed
  */
 public static void main(String[] args) {
     System.out.println("images and annotations");
     // step 1: creation of a document-object
     Document document = new Document(PageSize.A4, 50, 50, 50, 50);
     try {
         // step 2:
         // we create a writer that listens to the document
         PdfWriter.getInstance(document, new FileOutputStream("annotated_images.pdf"));
         // step 3: we open the document
         document.open();
         // step 4: we add some content
         Image jpeg = Image.getInstance("otsoe.jpg");
         jpeg.setAnnotation(new Annotation("picture", "This is my dog", 0, 0, 0, 0));
jpeg.setAbsolutePosition(100f, 550f);
document.add(jpeg);
         Image wmf = Image.getInstance("iText.wmf");
         wmf.setAnnotation(new Annotation(0, 0, 0, 0, "http://www.lowagie.com/iText"));
wmf.setAbsolutePosition(100f, 200f);
document.add(wmf);
     }
     catch (Exception de) {
         de.printStackTrace();
     }
     // step 5: we close the document
     document.close();
 }
 

实例 3


/**
 * Uses a java.awt.Image object to construct a com.lowagie.text.Image object.
 * @param args no arguments needed
 */
public static void main(String[] args) {
    System.out.println("Images using java.awt.image");
    // step 1: creation of a document-object
    Document document = new Document();
    try {
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("awt_image.pdf"));
        // step 3: we open the document
        document.open();
        // step 4: we add content to the document
        for (int i = 0; i < 300; i++) {
            document.add(new Phrase("Who is this? "));
        }
        PdfContentByte cb = writer.getDirectContent();
        java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage("H.gif");
        Image image = Image.getInstance(awtImage, null);
        image.setAbsolutePosition(100, 500);
        cb.addImage(image);
        Image gif = Image.getInstance(awtImage, new Color(0x00, 0xFF, 0xFF), true);
        gif.setAbsolutePosition(300, 500);
        cb.addImage(gif);
        Image img1 = Image.getInstance(awtImage, null, true);
        img1.setAbsolutePosition(100, 200);
        cb.addImage(img1);
        Image img2 = Image.getInstance(awtImage, new Color(0xFF, 0xFF, 0x00), false);
        img2.setAbsolutePosition(300, 200);
        cb.addImage(img2);
    }
    catch(DocumentException de) {
        System.err.println(de.getMessage());
    }
    catch(IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    // step 5: we close the document
    document.close();
}
 

实例 4


/**
 * Constructs a RtfImage for an Image.
 * 
 * @param doc The RtfDocument this RtfImage belongs to
 * @param image The Image that this RtfImage wraps
 * @throws DocumentException If an error occurred accessing the image content
 */
public RtfImage(RtfDocument doc, Image image) throws DocumentException
{
    super(doc);
    imageType = image.getOriginalType();
    if (!(imageType == Image.ORIGINAL_JPEG || imageType == Image.ORIGINAL_BMP
            || imageType == Image.ORIGINAL_PNG || imageType == Image.ORIGINAL_WMF || imageType == Image.ORIGINAL_GIF)) {
        throw new DocumentException(MessageLocalization.getComposedMessage("only.bmp.png.wmf.gif.and.jpeg.images.are.supported.by.the.rtf.writer"));
    }
    alignment = image.getAlignment();
    width = image.getWidth();
    height = image.getHeight();
    plainWidth = image.getPlainWidth();
    plainHeight = image.getPlainHeight();
    this.imageData = getImageData(image);
}
 

实例 5


/**
 * General example using cell events.
 * 
 * @param args
 *            no arguments needed
 */
public static void main(String[] args) {
        System.out.println("CellEvents");
        // step1
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        try {
                // step2
                PdfWriter writer = PdfWriter.getInstance(document,
                                new FileOutputStream("CellEvents.pdf"));
                // step3
                document.open();
                // step4
                CellEvents event = new CellEvents();
                Image im = Image.getInstance("otsoe.jpg");
                im.setRotationDegrees(30);
                PdfPTable table = new PdfPTable(4);
                table.addCell("text 1");
                PdfPCell cell = new PdfPCell(im, true);
                cell.setCellEvent(event);
                table.addCell(cell);
                table.addCell("text 3");
                im.setRotationDegrees(0);
                table.addCell(im);
                table.setTotalWidth(300);
                PdfContentByte cb = writer.getDirectContent();
                table.writeSelectedRows(0, -1, 50, 600, cb);
                table.setHeaderRows(3);
                document.add(table);
        } catch (Exception de) {
                de.printStackTrace();
        }
        // step5
        document.close();
}
 

实例 6


/** Reads a PNG from an url.
 * @param url the url
 * @throws IOException on error
 * @return the image
 */    
public static Image getImage(URL url) throws IOException {
    InputStream is = null;
    try {
        is = url.openStream();
        Image img = getImage(is);
        img.setUrl(url);
        return img;
    }
    finally {
        if (is != null) {
            is.close();
        }
    }
}
 

实例 7


/**
 * Adds an <CODE>Image</CODE> to the page. The <CODE>Image</CODE> must have
 * absolute positioning. The image can be placed inline.
 * @param image the <CODE>Image</CODE> object
 * @param inlineImage <CODE>true</CODE> to place this image inline, <CODE>false</CODE> otherwise
 * @throws DocumentException if the <CODE>Image</CODE> does not have absolute positioning
 */
public void addImage(Image image, boolean inlineImage) throws DocumentException {
    if (!image.hasAbsoluteY())
        throw new DocumentException(MessageLocalization.getComposedMessage("the.image.must.have.absolute.positioning"));
    float matrix[] = image.matrix();
    matrix[Image.CX] = image.getAbsoluteX() - matrix[Image.CX];
    matrix[Image.CY] = image.getAbsoluteY() - matrix[Image.CY];
    addImage(image, matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], inlineImage);
}
 

实例 8


/**
 * Extracts the image data from the Image.
 * 
 * @param image The image for which to extract the content
 * @return The raw image data, not formated
 * @throws DocumentException If an error occurs accessing the image content
 */
private byte[][] getImageData(Image image) throws DocumentException 
{
        final int WMF_PLACEABLE_HEADER_SIZE = 22;
    final RtfByteArrayBuffer bab = new RtfByteArrayBuffer();
    try {
        if(imageType == Image.ORIGINAL_BMP) {
                bab.append(MetaDo.wrapBMP(image));
        } else {                
                final byte[] iod = image.getOriginalData();
                if(iod == null) {
                final InputStream imageIn = image.getUrl().openStream();
                if(imageType == Image.ORIGINAL_WMF) { //remove the placeable header first
                        for(int k = 0; k < WMF_PLACEABLE_HEADER_SIZE; k++) {
                        if(imageIn.read() < 0) throw new EOFException(MessageLocalization.getComposedMessage("while.removing.wmf.placeable.header"));
                }
                }
                bab.write(imageIn);
                imageIn.close();
            } else {
                if(imageType == Image.ORIGINAL_WMF) {
                        //remove the placeable header                           
                        bab.write(iod, WMF_PLACEABLE_HEADER_SIZE, iod.length - WMF_PLACEABLE_HEADER_SIZE);
                } else {
                        bab.append(iod);
                }
            }
        }
        return bab.toByteArrayArray();
    } catch(IOException ioe) {
        throw new DocumentException(ioe.getMessage());
    }
}
 

实例 9


/**
 * A more complex example using ColumnText.
 * @param args no arguments needed
 */
public static void main(String[] args) {
    System.out.println("Columns and objects");
    // step 1: creation of a document-object
    Document document = new Document(PageSize.LETTER, 90, 54, 72, 72);
    try {
        // step 2: we create a writer that listens to the document
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("columnobjects.pdf"));
        float gutter = 20;
        int numColumns = 3;
        float fullWidth = document.right() - document.left();
        float columnWidth = (fullWidth - (numColumns - 1) * gutter) / numColumns;
        float allColumns[] = new float[numColumns]; // left
        for (int k = 0; k < numColumns; ++k) {
            allColumns[k] = document.left() + (columnWidth + gutter) * k;
        }
        // set the fonts
        Font font24B = FontFactory.getFont(FontFactory.TIMES_ROMAN, 24, Font.BOLD);
        Font font10B = FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.BOLD);
        Font font14B = FontFactory.getFont(FontFactory.TIMES_ROMAN, 14, Font.BOLD, new Color(255, 0, 0));
        Font font9 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 9);
        Font font11 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 11);
        // step 3: we open the document
        document.open();            
        // step 4:
        // get the stream content
        PdfContentByte cb = writer.getDirectContent();
        // headers
        Phrase fullTitle = new Phrase("Full Title", font24B);
        float currentY = document.top();
        ColumnText ct = new ColumnText(cb);
        ct.setSimpleColumn(fullTitle, document.left(), 0, document.right(), document.top(), 24, Element.ALIGN_JUSTIFIED);
        ct.go();
        currentY = ct.getYLine();
        currentY -= 4;
        cb.setLineWidth(1);
        cb.moveTo(document.left(), currentY);
        cb.lineTo(document.right(), currentY);
        cb.stroke();
        currentY -= 4;
        ct.setYLine(currentY);
        ct.addText(new Chunk("Author: Name of the author comes here", font10B));
        ct.setLeading(10);
        ct.go();
        currentY = ct.getYLine();
        currentY -= 15;
        float topColumn = currentY;
        for (int k = 1; k < numColumns; ++k) {
            float x = allColumns[k] - gutter / 2;
            cb.moveTo(x, topColumn);
            cb.lineTo(x, document.bottom());
        }
        cb.stroke();
        Image img = Image.getInstance("cover.png");
        cb.addImage(img, img.getScaledWidth(), 0, 0, img.getScaledHeight(), document.left(), currentY - img.getScaledHeight());
        currentY -= img.getScaledHeight() + 10;
        ct.setYLine(currentY);
        ct.addText(new Chunk("Key Data:", font14B));
        ct.go();
        currentY = ct.getYLine();
        currentY -= 4;
        PdfPTable ptable = new PdfPTable(2);
        ptable.getDefaultCell().setPaddingLeft(4);
        ptable.getDefaultCell().setPaddingTop(0);
        ptable.getDefaultCell().setPaddingBottom(4);
        ptable.addCell(new Phrase("Imprint Name:", font9));
        ptable.addCell(new Phrase("Prentice Hall", font9));
        ptable.addCell(new Phrase("Series Name:", font9));
        ptable.addCell(new Phrase("", font9));
        ptable.addCell(new Phrase("ISBN:", font9));
        ptable.addCell(new Phrase("Hall", font9));
        ptable.addCell(new Phrase("UPC Code:", font9));
        ptable.addCell(new Phrase("0789718103", font9));
        ptable.addCell(new Phrase("EAN #", font9));
        ptable.addCell(new Phrase("0786718103", font9));
        ptable.addCell(new Phrase("Price:", font9));
        ptable.addCell(new Phrase("49.99", font9));
        ptable.addCell(new Phrase("Page Count:", font9));
        ptable.addCell(new Phrase("500", font9));
        ptable.addCell(new Phrase("Discount:", font9));
        ptable.addCell(new Phrase("10%", font9));
        ptable.addCell(new Phrase("Trim Size:", font9));
        ptable.addCell(new Phrase("420x340", font9));
        ptable.addCell(new Phrase("Cover:", font9));
        ptable.addCell(new Phrase("Hard", font9));
        ptable.addCell(new Phrase("Interior Color:", font9));
        ptable.addCell(new Phrase("none", font9));
        ptable.addCell(new Phrase("Media with book:", font9));
        ptable.addCell(new Phrase("CD", font9));
        ptable.addCell(new Phrase("Author(s):", font9));
        ptable.addCell(new Phrase("Ben Forta", font9));
        ptable.addCell(new Phrase("Editor:", font9));
        ptable.addCell(new Phrase("Ben Forta", font9));
        ptable.addCell(new Phrase("Pub Date:", font9));
        ptable.addCell(new Phrase("06/05/1998", font9));
        ptable.setTotalWidth(columnWidth);
        currentY = ptable.writeSelectedRows(0, -1, document.left(), currentY, cb) - 20;
        for (int k = 0; k < headings.length; ++k) {
            ct.addText(new Chunk(headings[k] + "n", font14B));
            ct.addText(new Chunk(texts[k] + "nn", font11));
        }
        int currentColumn = 0;
        ct.setSimpleColumn(allColumns[currentColumn], document.bottom(),
            allColumns[currentColumn] + columnWidth, currentY, 15, Element.ALIGN_JUSTIFIED);
        ct.setLeading(2, 1);
        for (;;) {
            int rc = ct.go();
            if ((rc & ColumnText.NO_MORE_TEXT) != 0)
                break;
            // we run out of column. Let's go to another one
            ++currentColumn;
            if (currentColumn >= allColumns.length)
                break;
            ct.setSimpleColumn(allColumns[currentColumn], document.bottom(),
                allColumns[currentColumn] + columnWidth, topColumn, 15, Element.ALIGN_JUSTIFIED);
            ct.setLeading(2, 1);
        }
        // step 5: we close the document
        document.close();
    }
    catch (Exception de) {
        System.err.println(de.getMessage());
    }
}
 

实例 10


/**
  * Adds an element. Elements supported are <CODE>Paragraph</CODE>,
  * <CODE>List</CODE>, <CODE>PdfPTable</CODE>, <CODE>Image</CODE> and
  * <CODE>Graphic</CODE>.
  * <p>
  * It removes all the text placed with <CODE>addText()</CODE>.
  * 
  * @param element the <CODE>Element</CODE>
  */    
 public void addElement(Element element) {
     if (element == null)
         return;
     if (element instanceof Image) {
         Image img = (Image)element;
         PdfPTable t = new PdfPTable(1);
         float w = img.getWidthPercentage();
         if (w == 0) {
             t.setTotalWidth(img.getScaledWidth());
             t.setLockedWidth(true);
         }
         else
             t.setWidthPercentage(w);
         t.setSpacingAfter(img.getSpacingAfter());
         t.setSpacingBefore(img.getSpacingBefore());
         switch (img.getAlignment()) {
             case Image.LEFT:
                 t.setHorizontalAlignment(Element.ALIGN_LEFT);
                 break;
             case Image.RIGHT:
                 t.setHorizontalAlignment(Element.ALIGN_RIGHT);
                 break;
             default:
                 t.setHorizontalAlignment(Element.ALIGN_CENTER);
                 break;
         }
         PdfPCell c = new PdfPCell(img, true);
         c.setPadding(0);
         c.setBorder(img.getBorder());
         c.setBorderColor(img.getBorderColor());
         c.setBorderWidth(img.getBorderWidth());
         c.setBackgroundColor(img.getBackgroundColor());
         t.addCell(c);
         element = t;
     }
     if (element.type() == Element.CHUNK) {
        element = new Paragraph((Chunk)element);
     }
     else if (element.type() == Element.PHRASE) {
        element = new Paragraph((Phrase)element);
     }
     if (element instanceof SimpleTable) {
        try {
        element = ((SimpleTable)element).createPdfPTable();
} catch (DocumentException e) {
        throw new IllegalArgumentException(MessageLocalization.getComposedMessage("element.not.allowed"));
}
     }
     else if (element.type() != Element.PARAGRAPH && element.type() != Element.LIST && element.type() != Element.PTABLE && element.type() != Element.YMARK)
         throw new IllegalArgumentException(MessageLocalization.getComposedMessage("element.not.allowed"));
     if (!composite) {
         composite = true;
         compositeElements = new LinkedList();
         bidiLine = null;
         waitPhrase = null;
     }
     compositeElements.add(element);
 }
 

实例 11


/**
 * Adds an image to the document.
 * @param image the <CODE>Image</CODE> to add
 * @throws PdfException on error
 * @throws DocumentException on error
 */
protected void add(Image image) throws PdfException, DocumentException {
    if (image.hasAbsoluteY()) {
        graphics.addImage(image);
        pageEmpty = false;
        return;
    }
    // if there isn't enough room for the image on this page, save it for the next page
    if (currentHeight != 0 && indentTop() - currentHeight - image.getScaledHeight() < indentBottom()) {
        if (!strictImageSequence && imageWait == null) {
            imageWait = image;
            return;
        }
        newPage();
        if (currentHeight != 0 && indentTop() - currentHeight - image.getScaledHeight() < indentBottom()) {
            imageWait = image;
            return;
        }
    }
    pageEmpty = false;
    // avoid endless loops
    if (image == imageWait)
        imageWait = null;
    boolean textwrap = (image.getAlignment() & Image.TEXTWRAP) == Image.TEXTWRAP
    && !((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE);
    boolean underlying = (image.getAlignment() & Image.UNDERLYING) == Image.UNDERLYING;
    float diff = leading / 2;
    if (textwrap) {
        diff += leading;
    }
    float lowerleft = indentTop() - currentHeight - image.getScaledHeight() -diff;
    float mt[] = image.matrix();
    float startPosition = indentLeft() - mt[4];
    if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) startPosition = indentRight() - image.getScaledWidth() - mt[4];
    if ((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE) startPosition = indentLeft() + ((indentRight() - indentLeft() - image.getScaledWidth()) / 2) - mt[4];
    if (image.hasAbsoluteX()) startPosition = image.getAbsoluteX();
    if (textwrap) {
        if (imageEnd < 0 || imageEnd < currentHeight + image.getScaledHeight() + diff) {
            imageEnd = currentHeight + image.getScaledHeight() + diff;
        }
        if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) {
                // indentation suggested by Pelikan Stephan
                indentation.imageIndentRight += image.getScaledWidth() + image.getIndentationLeft();
        }
        else {
                // indentation suggested by Pelikan Stephan
                indentation.imageIndentLeft += image.getScaledWidth() + image.getIndentationRight();
        }
    }
    else {
        if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) startPosition -= image.getIndentationRight();
        else if ((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE) startPosition += image.getIndentationLeft() - image.getIndentationRight();
        else startPosition += image.getIndentationLeft();
    }
    graphics.addImage(image, mt[0], mt[1], mt[2], mt[3], startPosition, lowerleft - mt[5]);
    if (!(textwrap || underlying)) {
        currentHeight += image.getScaledHeight() + diff;
        flushLines();
        text.moveText(0, - (image.getScaledHeight() + diff));
        newLine();
    }
}
 

实例 12


/**
 * Reads next frame image
 */
protected void readImage() throws IOException {
    ix = readShort();    // (sub)image position & size
    iy = readShort();
    iw = readShort();
    ih = readShort();
    int packed = in.read();
    lctFlag = (packed & 0x80) != 0;     // 1 - local color table flag
    interlace = (packed & 0x40) != 0;   // 2 - interlace flag
    // 3 - sort flag
    // 4-5 - reserved
    lctSize = 2 << (packed & 7);        // 6-8 - local color table size
    m_bpc = newBpc(m_gbpc);
    if (lctFlag) {
        m_curr_table = readColorTable((packed & 7) + 1);   // read table
        m_bpc = newBpc((packed & 7) + 1);
    }
    else {
        m_curr_table = m_global_table;
    }
    if (transparency && transIndex >= m_curr_table.length / 3)
        transparency = false;
    if (transparency && m_bpc == 1) { // Acrobat 5.05 doesn't like this combination
        byte tp[] = new byte[12];
        System.arraycopy(m_curr_table, 0, tp, 0, 6);
        m_curr_table = tp;
        m_bpc = 2;
    }
    boolean skipZero = decodeImageData();   // decode pixel data
    if (!skipZero)
        skip();
    Image img = null;
    try {
        img = new ImgRaw(iw, ih, 1, m_bpc, m_out);
        PdfArray colorspace = new PdfArray();
        colorspace.add(PdfName.INDEXED);
        colorspace.add(PdfName.DEVICERGB);
        int len = m_curr_table.length;
        colorspace.add(new PdfNumber(len / 3 - 1));
        colorspace.add(new PdfString(m_curr_table));
        PdfDictionary ad = new PdfDictionary();
        ad.put(PdfName.COLORSPACE, colorspace);
        img.setAdditional(ad);
        if (transparency) {
            img.setTransparency(new int[]{transIndex, transIndex});
        }
    }
    catch (Exception e) {
        throw new ExceptionConverter(e);
    }
    img.setOriginalType(Image.ORIGINAL_GIF);
    img.setOriginalData(fromData);
    img.setUrl(fromUrl);
    GifFrame gf = new GifFrame();
    gf.image = img;
    gf.ix = ix;
    gf.iy = iy;
    frames.add(gf);   // add image to frame list
    //resetFrame();
}
 

实例 13


public boolean handleControlWord(RtfCtrlWordData ctrlWordData) {
        boolean result = false;
        boolean skipCtrlWord = false;
        if (this.rtfParser.isImport()) {
                skipCtrlWord = true;
                if (ctrlWordData.ctrlWord.equals("shppict")) {
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("nonshppict")) /* never gets here because this is a destination set to null */{
                        skipCtrlWord = true;
                        this.rtfParser.setTokeniserStateSkipGroup();
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("blipuid")) {
                        skipCtrlWord = true;
                        this.rtfParser.setTokeniserStateSkipGroup();
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("picprop")) {
                        skipCtrlWord = true;
                        this.rtfParser.setTokeniserStateSkipGroup();
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("pict")) {
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("emfblip")) {
                        result = true;
                        pictureType = Image.ORIGINAL_NONE;
                } else if (ctrlWordData.ctrlWord.equals("pngblip")) {
                        result = true;
                        pictureType = Image.ORIGINAL_PNG;
                } else if (ctrlWordData.ctrlWord.equals("jepgblip")) {
                        result = true;
                        pictureType = Image.ORIGINAL_JPEG;
                } else if (ctrlWordData.ctrlWord.equals("macpict")) {
                        result = true;
                        pictureType = Image.ORIGINAL_NONE;
                } else if (ctrlWordData.ctrlWord.equals("pmmetafile")) {
                        result = true;
                        pictureType = Image.ORIGINAL_NONE;
                } else if (ctrlWordData.ctrlWord.equals("wmetafile")) {
                        result = true;
                        pictureType = Image.ORIGINAL_WMF;
                } else if (ctrlWordData.ctrlWord.equals("dibitmap")) {
                        result = true;
                        pictureType = Image.ORIGINAL_NONE;
                } else if (ctrlWordData.ctrlWord.equals("wbitmap")) {
                        result = true;
                        pictureType = Image.ORIGINAL_BMP;
                }
                /* bitmap information */
                else if (ctrlWordData.ctrlWord.equals("wbmbitspixel")) {
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("wbmplanes")) {
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("wbmwidthbytes")) {
                        result = true;
                } else
                /* picture size, scaling and cropping */
                if (ctrlWordData.ctrlWord.equals("picw")) {
                        this.width = ctrlWordData.toLong();
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("pich")) {
                        this.height = ctrlWordData.toLong();
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("picwgoal")) {
                        this.desiredWidth = ctrlWordData.toLong();
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("pichgoal")) {
                        this.desiredHeight = ctrlWordData.toLong();
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("picscalex")) {
                        this.scaleX = ctrlWordData.toInteger();
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("picscaley")) {
                        this.scaleY = ctrlWordData.toInteger();
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("picscaled")) {
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("picprop")) {
                        skipCtrlWord = true;
                        this.rtfParser.setTokeniserStateSkipGroup();
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("defshp")) {
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("piccropt")) {
                        this.cropTop = ctrlWordData.toInteger();
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("piccropb")) {
                        this.cropBottom = ctrlWordData.toInteger();
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("piccropl")) {
                        this.cropLeft = ctrlWordData.toInteger();
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("piccropr")) {
                        this.cropRight = ctrlWordData.toInteger();
                        result = true;
                } else
                /* metafile information */
                if (ctrlWordData.ctrlWord.equals("picbmp")) {
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("picbpp")) {
                        result = true;
                } else
                /* picture data */
                if (ctrlWordData.ctrlWord.equals("bin")) {
                        this.dataFormat = FORMAT_BINARY;
                        // set length to param
                        this.binaryLength = ctrlWordData.longValue();
                        this.rtfParser.setTokeniserStateBinary(binaryLength);
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("blipupi")) {
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("blipuid")) {
                        skipCtrlWord = true;
                        this.rtfParser.setTokeniserStateSkipGroup();
                        result = true;
                } else if (ctrlWordData.ctrlWord.equals("bliptag")) {
                        result = true;
                }
        }
        if (this.rtfParser.isConvert()) {
                if (ctrlWordData.ctrlWord.equals("shppict")) {
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("nonshppict")) {
                        skipCtrlWord = true;
                        this.rtfParser.setTokeniserStateSkipGroup();
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("blipuid")) {
                        result = true;
                        this.rtfParser.setTokeniserStateSkipGroup();
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("pict")) {
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("emfblip")) {
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("pngblip")) {
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("jepgblip")) {
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("macpict")) {
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("pmmetafile")) {
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("wmetafile")) {
                        skipCtrlWord = true;
                        this.rtfParser.setTokeniserStateSkipGroup();
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("dibitmap")) {
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("wbitmap")) {
                        result = true;
                }
                /* bitmap information */
                if (ctrlWordData.ctrlWord.equals("wbmbitspixel")) {
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("wbmplanes")) {
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("wbmwidthbytes")) {
                        result = true;
                }
                /* picture size, scaling and cropping */
                if (ctrlWordData.ctrlWord.equals("picw")) {
                        this.width = ctrlWordData.toLong();
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("pich")) {
                        this.height = ctrlWordData.toLong();
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("picwgoal")) {
                        this.desiredWidth = ctrlWordData.toLong();
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("pichgoal")) {
                        this.desiredHeight = ctrlWordData.toLong();
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("picscalex")) {
                        this.scaleX = ctrlWordData.toInteger();
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("picscaley")) {
                        this.scaleY = ctrlWordData.toInteger();
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("picscaled")) {
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("picprop")) {
                        skipCtrlWord = true;
                        this.rtfParser.setTokeniserStateSkipGroup();
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("defshp")) {
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("piccropt")) {
                        this.cropTop = ctrlWordData.toInteger();
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("piccropb")) {
                        this.cropBottom = ctrlWordData.toInteger();
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("piccropl")) {
                        this.cropLeft = ctrlWordData.toInteger();
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("piccropr")) {
                        this.cropRight = ctrlWordData.toInteger();
                        result = true;
                }
                /* metafile information */
                if (ctrlWordData.ctrlWord.equals("picbmp")) {
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("picbpp")) {
                        result = true;
                }
                /* picture data */
                if (ctrlWordData.ctrlWord.equals("bin")) {
                        dataFormat = FORMAT_BINARY;
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("blipupi")) {
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("blipuid")) {
                        skipCtrlWord = true;
                        this.rtfParser.setTokeniserStateSkipGroup();
                        result = true;
                }
                if (ctrlWordData.ctrlWord.equals("bliptag")) {
                        result = true;
                }
        }
        if (!skipCtrlWord) {
                switch (this.rtfParser.getConversionType()) {
                case RtfParser.TYPE_IMPORT_FULL:
                        writeBuffer();
                        writeText(ctrlWordData.toString());
                        result = true;
                        break;
                case RtfParser.TYPE_IMPORT_FRAGMENT:
                        writeBuffer();
                        writeText(ctrlWordData.toString());
                        result = true;
                        break;
                case RtfParser.TYPE_CONVERT:
                        result = true;
                        break;
                default: // error because is should be an import or convert
                        result = false;
                        break;
                }
        }
        return result;
}
 

实例 14


/**
 * Applying masks to images.
 * @param args no arguments needed
 */
public static void main(String[] args) {
    System.out.println("masked images");
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("maskedImages.pdf"));
        document.open();
        Paragraph p = new Paragraph("Some text behind a masked image.");
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        document.add(p);
        PdfContentByte cb = writer.getDirectContent();
        byte maskr[] = {(byte)0x3c, (byte)0x7e, (byte)0xe7, (byte)0xc3, (byte)0xc3, (byte)0xe7, (byte)0x7e, (byte)0x3c};
        Image mask = Image.getInstance(8, 8, 1, 1, maskr);
        mask.makeMask();
        mask.setInverted(true);
        Image image = Image.getInstance("otsoe.jpg");
        image.setImageMask(mask);
        image.setAbsolutePosition(60, 550);
        // explicit masking
        cb.addImage(image);
        // stencil masking
        cb.setRGBColorFill(255, 0, 0);
        cb.addImage(mask, mask.getScaledWidth() * 8, 0, 0, mask.getScaledHeight() * 8, 100, 450);
        cb.setRGBColorFill(0, 255, 0);
        cb.addImage(mask, mask.getScaledWidth() * 8, 0, 0, mask.getScaledHeight() * 8, 100, 400);
        cb.setRGBColorFill(0, 0, 255);
        cb.addImage(mask, mask.getScaledWidth() * 8, 0, 0, mask.getScaledHeight() * 8, 100, 350);
        document.close();
    }
    catch (Exception de) {
        de.printStackTrace();
    }
}
 

实例 15


/**
 * Rotating images.
 * @param args No arguments needed
 */
public static void main(String[] args) {
    System.out.println("Rotating an Image");
    // step 1: creation of a document-object
    Document document = new Document();
    try {
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file
        PdfWriter.getInstance(document, new FileOutputStream("rotating.pdf"));
        // step 3: we open the document
        document.open();
        // step 4: we add content
        Image jpg = Image.getInstance("otsoe.jpg");
        jpg.setAlignment(Image.MIDDLE);
        jpg.setRotation((float)Math.PI / 6);
        document.add(new Paragraph("rotate 30 degrees"));
        document.add(jpg);
        document.newPage();
        jpg.setRotation((float)Math.PI / 4);
        document.add(new Paragraph("rotate 45 degrees"));
        document.add(jpg);
        document.newPage();
        jpg.setRotation((float)Math.PI / 2);
        document.add(new Paragraph("rotate pi/2 radians"));
        document.add(jpg);
        document.newPage();
        jpg.setRotation((float)(Math.PI * 0.75));
        document.add(new Paragraph("rotate 135 degrees"));
        document.add(jpg);
        document.newPage();
        jpg.setRotation((float)Math.PI);
        document.add(new Paragraph("rotate pi radians"));
        document.add(jpg);
        document.newPage();
        jpg.setRotation((float)(2.0 * Math.PI));
        document.add(new Paragraph("rotate 2 x pi radians"));
        document.add(jpg);
    }
    catch(DocumentException de) {
        System.err.println(de.getMessage());
    }
    catch(IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    // step 5: we close the document
    document.close();
}
 
讨论
淘淘あ西西 profile image