📄 pdfdocument.java
字号:
// some parameters are set back to normal again carriageReturn(); if (pageEvent != null && isParagraph) pageEvent.onParagraphEnd(writer, this, indentTop() - currentHeight); alignment = Element.ALIGN_LEFT; indentLeft -= paragraph.indentationLeft(); indentRight -= paragraph.indentationRight(); //add by Jin-Hsia Yang isParagraphE = false; //end add by Jin-Hsia Yang break; } case Element.SECTION: case Element.CHAPTER: { // Chapters and Sections only differ in their constructor // so we cast both to a Section Section section = (Section) element; PdfDestination destination = new PdfDestination(PdfDestination.FITH, indentTop() - currentHeight); while (currentOutline.level() >= section.depth()) { currentOutline = currentOutline.parent(); } PdfOutline outline = new PdfOutline(currentOutline, destination, section.title(), section.isBookmarkOpen()); outlines.add(outline); currentOutline = outline; // some values are set carriageReturn(); indentLeft += section.indentationLeft(); indentRight += section.indentationRight(); PdfPageEvent pageEvent = writer.getPageEvent(); if (pageEvent != null) if (element.type() == Element.CHAPTER) pageEvent.onChapter(writer, this, indentTop() - currentHeight, section.title()); else pageEvent.onSection(writer, this, indentTop() - currentHeight, section.depth(), section.title()); // the title of the section (if any has to be printed) if (section.title() != null) { isParagraph = false; add(section.title()); isParagraph = true; } indentLeft += section.indentation(); // we process the section element.process(this); // some parameters are set back to normal again indentLeft -= section.indentationLeft() + section.indentation(); indentRight -= section.indentationRight(); if (pageEvent != null) if (element.type() == Element.CHAPTER) pageEvent.onChapterEnd(writer, this, indentTop() - currentHeight); else pageEvent.onSectionEnd(writer, this, indentTop() - currentHeight); // if the section is a chapter, we begin a new page if (section.isChapter()) { newPage(); } // otherwise, we begin a new line else { newLine(); } break; } case Element.LIST: { // we cast the element to a List List list = (List) element; // we adjust the document listIndentLeft += list.indentationLeft(); indentRight += list.indentationRight(); // we process the items in the list element.process(this); // some parameters are set back to normal again listIndentLeft -= list.indentationLeft(); indentRight -= list.indentationRight(); break; } case Element.LISTITEM: { // we cast the element to a ListItem ListItem listItem = (ListItem) element; // we adjust the document alignment = listItem.alignment(); listIndentLeft += listItem.indentationLeft(); indentRight += listItem.indentationRight(); leading = listItem.leading(); carriageReturn(); // we prepare the current line to be able to show us the listsymbol line.setListItem(listItem); // we process the item element.process(this); // if the last line is justified, it should be aligned to the left // if (line.hasToBeJustified()) { // line.resetAlignment(); // } // some parameters are set back to normal again carriageReturn(); listIndentLeft -= listItem.indentationLeft(); indentRight -= listItem.indentationRight(); break; } case Element.RECTANGLE: { Rectangle rectangle = (Rectangle) element; graphics.rectangle(rectangle); pageEmpty = false; break; } case Element.PTABLE: { // before every table, we add a new line and flush all lines newLine(); flushLines(); PdfPTable ptable = (PdfPTable)element; float totalWidth = (indentRight() - indentLeft()) * ptable.getWidthPercentage() / 100; float xWidth = 0; switch (ptable.getHorizontalAlignment()) { case Element.ALIGN_LEFT: xWidth = indentLeft(); break; case Element.ALIGN_RIGHT: xWidth = indentRight() - totalWidth; break; default: xWidth = (indentRight() + indentLeft() - totalWidth) / 2; } ptable.setTotalWidth(totalWidth); addPTable(ptable, xWidth); break; } case Element.TABLE: { /** * This is a list of people who worked on the Table functionality. * To see who did what, please check the CVS repository: * * Leslie Baski * Matt Benson * Francesco De Milato * David Freels * Bruno Lowagie * Veerendra Namineni * Geert Poels * Tom Ring * Paulo Soares */ // before every table, we add a new line and flush all lines newLine(); flushLines(); // initialisation of parameters float pagetop = indentTop(); float oldHeight = currentHeight; float cellDisplacement; PdfCell cell; Color color; // correct table : fill empty cells/ parse table in table ((Table) element).complete(); // constructing the PdfTable PdfTable table = new PdfTable((Table) element, indentLeft(), indentRight(), currentHeight > 0 ? (pagetop - currentHeight) - 6 : pagetop); // drawing the table ArrayList cells = table.getCells(); ArrayList headercells = null; while (! cells.isEmpty()) { // initialisation of some extra parameters; float lostTableBottom = 0; float lostTableTop = 0; // loop over the cells boolean cellsShown = false; for (Iterator iterator = cells.iterator(); iterator.hasNext(); ) { cell = (PdfCell) iterator.next(); lines = cell.getLines(pagetop, indentBottom()); // if there are lines to add, add them if (lines != null && lines.size() > 0) { // we paint the borders of the cells cellsShown = true; graphics.rectangle(cell.rectangle(pagetop, indentBottom())); lostTableBottom = Math.max(cell.bottom(), indentBottom()); lostTableTop = cell.top(); // we write the text float cellTop = cell.top(pagetop - oldHeight); text.moveText(0, cellTop); cellDisplacement = flushLines() - cellTop; text.moveText(0, cellDisplacement); if (oldHeight + cellDisplacement > currentHeight) { currentHeight = oldHeight + cellDisplacement; } } // if a cell is allready added completely, remove it if (cell.mayBeRemoved()) { iterator.remove(); } } // we paint the graphics of the table after looping through all the cells if (cellsShown) { Rectangle tablerec = new Rectangle(table); tablerec.setBorder(table.border()); tablerec.setBorderWidth(table.borderWidth()); tablerec.setBorderColor(table.borderColor()); tablerec.setBackgroundColor(table.backgroundColor()); tablerec.setGrayFill(table.grayFill()); graphics.rectangle(tablerec.rectangle(top(), indentBottom())); } // if the table continues on the next page if (! cells.isEmpty()) { graphics.setLineWidth(table.borderWidth()); if (cellsShown && (table.border() & Rectangle.BOTTOM) == Rectangle.BOTTOM) { // Draw the bottom line // the color is set to the color of the element Color tColor = table.borderColor(); if (tColor != null) { graphics.setRGBColorStroke(tColor.getRed(), tColor.getGreen(), tColor.getBlue()); } graphics.moveTo(table.left(), Math.max(table.bottom(), indentBottom())); graphics.lineTo(table.right(), Math.max(table.bottom(), indentBottom())); graphics.stroke(); if (tColor != null) { graphics.resetRGBColorStroke(); } } // old page pageEmpty = false; float difference = lostTableBottom; // new page newPage(); flushLines(); // this part repeats the table headers (if any) headercells = table.getHeaderCells(); int size = headercells.size(); if (size > 0) { // this is the top of the headersection cell = (PdfCell) headercells.get(0); float oldTop = cell.top(); // loop over all the cells of the table header for (int i = 0; i < size; i++) { cell = (PdfCell) headercells.get(i); // calculation of the new cellpositions cell.setTop(indentTop() - oldTop + cell.top()); cell.setBottom(indentTop() - oldTop + cell.bottom() - 2f * table.cellspacing()); pagetop = cell.bottom(); // we paint the borders of the cell graphics.rectangle(cell.rectangle(indentTop(), indentBottom())); // we write the text of the cell lines = cell.getLines(indentTop(), indentBottom()); float cellTop = cell.top(indentTop()); text.moveText(0, cellTop); cellDisplacement = flushLines() - cellTop; text.moveText(0, cellDisplacement); } currentHeight = indentTop() - pagetop - table.cellspacing(); text.moveText(0, pagetop - indentTop() + table.cellspacing() - currentHeight); } oldHeight = currentHeight; // calculating the new positions of the table and the cells size = Math.min(cells.size(), table.columns()); int i = 0; while (i < size ) { cell = (PdfCell) cells.get(i); if (cell.top(-table.cellspacing()) > lostTableBottom) { float newBottom = pagetop - difference + cell.bottom(); float neededHeight = cell.remainingHeight(); if (newBottom > pagetop - neededHeight) { difference += newBottom - (pagetop - neededHeight); } } i++; } size = cells.size(); table.setTop(indentTop()); table.setBottom(pagetop - difference + table.bottom(table.cellspacing())); for (i = 0; i < size; i++) { cell = (PdfCell) cells.get(i); float newBottom = pagetop - difference + cell.bottom(); float newTop = pagetop - difference + cell.top(-table.cellspacing()); if (newTop > indentTop() - currentHeight - table.cellspacing()) { newTop = indentTop() - currentHeight - table.cellspacing(); //newTop = newBottom + cell.remainingHeight(); } cell.setTop(newTop); cell.setBottom(newBottom); } } } text.moveText(0, oldHeight - currentHeight); lines.add(line); currentHeight += line.height() - pagetop + indentTop(); line = new PdfLine(indentLeft(), indentRight(), alignment, leading); pageEmpty = false; break; } case Element.GIF: case Element.JPEG: case Element.PNG: case Element.IMGRAW: case Element.IMGTEMPLATE: { carriageReturn(); add((Image) element); pageEmpty = false; break; } case Element.GRAPHIC: { Graphic graphic = (Graphic) element; graphic.processAttributes(indentLeft(), indentBottom(), indentRight(), indentTop(), indentTop() - currentHeight); graphics.add(graphic); pageEmpty = false; break; } default: return false; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -