📄 pdfdocument.java
字号:
return true; } catch(Exception e) { throw new DocumentException(e.getMessage()); } } // methods to add Content /** * Adds an image to the document and to the page resources. * @param image the <CODE>Image</CODE> to add * @return the name of the image added * @throws PdfException on error * @throws DocumentException on error */ PdfName addDirectImage(Image image) throws PdfException, DocumentException { PdfName name = addDirectImageSimple(image); xObjectDictionary.put(name, writer.getImageReference(name)); return name; } /** Adds an image to the document but not to the page resources. It is used with * templates and <CODE>Document.add(Image)</CODE>. * @param image the <CODE>Image</CODE> to add * @return the name of the image added * @throws PdfException on error * @throws DocumentException on error */ PdfName addDirectImageSimple(Image image) throws PdfException, DocumentException { PdfName name; // if the images is already added, just retrieve the name if (images.containsKey(image.getMySerialId())) { name = (PdfName) images.get(image.getMySerialId()); } // if it's a new image, add it to the document else { if (image.isImgTemplate()) { name = new PdfName("img" + images.size()); if (image.templateData() == null) { try { ImgWMF wmf = (ImgWMF)image; wmf.readWMF(writer.getDirectContent().createTemplate(0, 0)); } catch (Exception e) { throw new DocumentException(e.getMessage()); } } } else { PdfImage i = new PdfImage(image, "img" + images.size()); writer.add(i); name = i.name(); } images.put(image.getMySerialId(), name); } return name; } /** * Adds an image to the document. * @param image the <CODE>Image</CODE> to add * @throws PdfException on error * @throws DocumentException on error */ private void add(Image image) throws PdfException, DocumentException { pageEmpty = false; PdfName name = addDirectImageSimple(image); if (image.hasAbsolutePosition()) { graphics.addImage(image); 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.scaledHeight() < indentBottom()) { if (imageWait == null) { imageWait = image; return; } newPage(); if (currentHeight != 0 && indentTop() - currentHeight - image.scaledHeight() < indentBottom()) { imageWait = image; return; } } // avoid endless loops imageWait = null; boolean textwrap = (image.alignment() & Image.TEXTWRAP) == Image.TEXTWRAP && !((image.alignment() & Image.MIDDLE) == Image.MIDDLE); boolean underlying = (image.alignment() & Image.UNDERLYING) == Image.UNDERLYING; float diff = leading / 2; if (textwrap) { diff += leading; } float lowerleft = indentTop() - currentHeight - image.scaledHeight() -diff; float mt[] = image.matrix(); switch(image.alignment() & Image.MIDDLE) { case Image.RIGHT: graphics.addImage(image, mt[0], mt[1], mt[2], mt[3], indentRight() - image.scaledWidth() - mt[4], lowerleft - mt[5]); break; case Image.MIDDLE: float middle = indentRight() - indentLeft() - image.scaledWidth(); graphics.addImage(image, mt[0], mt[1], mt[2], mt[3], indentLeft() + (middle / 2) - mt[4], lowerleft - mt[5]); break; case Image.LEFT: default: graphics.addImage(image, mt[0], mt[1], mt[2], mt[3], indentLeft() - mt[4], lowerleft - mt[5]); } if (textwrap) { if (imageEnd < 0 || imageEnd < currentHeight + image.scaledHeight() + diff) { imageEnd = currentHeight + image.scaledHeight() + diff; } if ((image.alignment() & Image.RIGHT) == Image.RIGHT) { imageIndentRight += image.scaledWidth(); } else { imageIndentLeft += image.scaledWidth(); } } if (!(textwrap || underlying)) { currentHeight += image.scaledHeight() + diff; flushLines(); text.moveText(0, - (image.scaledHeight() + diff)); newLine(); } } /** * Initializes a page. * <P> * If the footer/header is set, it is printed. * @throws DocumentException on error */ private void initPage() throws DocumentException { // initialisation of some page objects annotations = new PdfArray(); fontDictionary = new PdfFontDictionary(); xObjectDictionary = new PdfXObjectDictionary(); writer.resetContent(); // the pagenumber is incremented pageN++; // graphics and text are initialized float oldleading = leading; int oldAlignment = alignment; imageEnd = -1; imageIndentRight = 0; imageIndentLeft = 0; graphics = new PdfContentByte(writer); text = new PdfContentByte(writer); text.beginText(); leading = 16; indentBottom = 0; indentTop = 0; currentHeight = 0; // backgroundcolors, etc... thisPageSize = pageSize; if (pageSize.backgroundColor() != null || pageSize.hasBorders() || pageSize.borderColor() != null || pageSize.grayFill() > 0) { add(pageSize); } // if there is a watermark, the watermark is added if (watermark != null) { float mt[] = watermark.matrix(); graphics.addImage(watermark, mt[0], mt[1], mt[2], mt[3], watermark.offsetX() - mt[4], watermark.offsetY() - mt[5]); } // if there is a footer, the footer is added if (footer != null) { footer.setPageNumber(pageN); leading = footer.paragraph().leading(); add(footer.paragraph()); // adding the footer limits the height indentBottom = currentHeight; text.moveText(left(), indentBottom()); flushLines(); text.moveText(-left(), -bottom()); footer.setTop(bottom(currentHeight)); footer.setBottom(bottom() - (0.75f * leading)); footer.setLeft(left()); footer.setRight(right()); graphics.rectangle(footer); indentBottom = currentHeight + leading * 3 / 2; currentHeight = 0; } // we move to the left/top position of the page text.moveText(left(), top()); // if there is a header, the header = added if (header != null) { header.setPageNumber(pageN); leading = header.paragraph().leading(); text.moveText(0, leading); add(header.paragraph()); newLine(); indentTop = currentHeight - leading; header.setTop(top() + leading); header.setBottom(indentTop() + leading * 2 / 3); header.setLeft(left()); header.setRight(right()); graphics.rectangle(header); flushLines(); currentHeight = 0; } pageEmpty = true; // if there is an image waiting to be drawn, draw it try { if (imageWait != null) { add(imageWait); imageWait = null; } } catch(Exception e) { e.printStackTrace(); } leading = oldleading; alignment = oldAlignment; carriageReturn(); PdfPageEvent pageEvent = writer.getPageEvent(); if (pageEvent != null) { if (firstPageEvent) { pageEvent.onOpenDocument(writer, this); } pageEvent.onStartPage(writer, this); } firstPageEvent = false; } /** * If the current line is not empty or null, it is added to the arraylist * of lines and a new empty line is added. * @throws DocumentException on error */ private void carriageReturn() throws DocumentException { // the arraylist with lines may not be null if (lines == null) { lines = new ArrayList(); } // If the current line is not null if (line != null) { // we check if the end of the page is reached if (currentHeight + line.height() < indentTop() - indentBottom()) { // if so nonempty lines are added and the heigt is augmented if (line.size() > 0) { currentHeight += line.height(); lines.add(line); pageEmpty = false; } } // if the end of the line is reached, we start a new page else { newPage(); } } if (imageEnd > -1 && currentHeight > imageEnd) { imageEnd = -1; imageIndentRight = 0; imageIndentLeft = 0; } // a new current line is constructed line = new PdfLine(indentLeft(), indentRight(), alignment, leading); } /** * Adds the current line to the list of lines and also adds an empty line. * @throws DocumentException on error */ private void newLine() throws DocumentException { carriageReturn(); if (lines != null && lines.size() > 0) { lines.add(line); currentHeight += line.height(); } line = new PdfLine(indentLeft(), indentRight(), alignment, leading); } /** * Writes all the lines to the text-object. * * @return the displacement that was caused * @throws DocumentException on error */ private float flushLines() throws DocumentException { // checks if the ArrayList with the lines is not null if (lines == null) { return 0; } //add by Jin-Hsia Yang boolean newline=false; //end add by Jin-Hsia Yang // checks if a new Line has to be made. if (line != null && line.size() > 0) { lines.add(line); line = new PdfLine(indentLeft(), indentRight(), alignment, leading); //add by Jin-Hsia Yang newline=true; //end add by Jin-Hsia Yang } // checks if the ArrayList with the lines is empty if (lines.size() == 0) { return 0; } // initialisation of some parameters Object currentValues[] = new Object[2]; PdfFont currentFont = null; int currentLeading = 0; float displacement = 0; PdfLine l; PdfChunk chunk; Float lastBaseFactor = new Float(0); currentValues[1] = lastBaseFactor; // looping over all the lines for (Iterator i = lines.iterator();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -