📄 pdfdocument.java
字号:
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
}
// CONSTRUCTING A PdfDocument/PdfWriter INSTANCE
/**
* Constructs a new PDF document.
* @throws DocumentException on error
*/
public PdfDocument() {
super();
addProducer();
addCreationDate();
}
/** The <CODE>PdfWriter</CODE>. */
private PdfWriter writer;
/**
* Adds a <CODE>PdfWriter</CODE> to the <CODE>PdfDocument</CODE>.
*
* @param writer the <CODE>PdfWriter</CODE> that writes everything
* what is added to this document to an outputstream.
* @throws DocumentException on error
*/
public void addWriter(PdfWriter writer) throws DocumentException {
if (this.writer == null) {
this.writer = writer;
annotationsImp = new PdfAnnotationsImp(writer);
return;
}
throw new DocumentException("You can only add a writer to a PdfDocument once.");
}
// LISTENER METHODS START
// [L0] ElementListener interface
/** This is the PdfContentByte object, containing the text. */
private PdfContentByte text;
/** This is the PdfContentByte object, containing the borders and other Graphics. */
private PdfContentByte graphics;
/** This represents the leading of the lines. */
private float leading = 0;
/** This represents the current alignment of the PDF Elements. */
private int alignment = Element.ALIGN_LEFT;
/** This is the current height of the document. */
private float currentHeight = 0;
/** Signals that onParagraph is valid (to avoid that a Chapter/Section title is treated as a Paragraph). */
private boolean isParagraph = true;
/** The current active <CODE>PdfAction</CODE> when processing an <CODE>Anchor</CODE>. */
private PdfAction anchorAction = null;
/**
* Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>.
*
* @param element the element to add
* @return <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not.
* @throws DocumentException when a document isn't open yet, or has been closed
*/
public boolean add(Element element) throws DocumentException {
if (writer != null && writer.isPaused()) {
return false;
}
try {
switch(element.type()) {
// Information (headers)
case Element.HEADER:
info.addkey(((Meta)element).getName(), ((Meta)element).getContent());
break;
case Element.TITLE:
info.addTitle(((Meta)element).getContent());
break;
case Element.SUBJECT:
info.addSubject(((Meta)element).getContent());
break;
case Element.KEYWORDS:
info.addKeywords(((Meta)element).getContent());
break;
case Element.AUTHOR:
info.addAuthor(((Meta)element).getContent());
break;
case Element.CREATOR:
info.addCreator(((Meta)element).getContent());
break;
case Element.PRODUCER:
// you can not change the name of the producer
info.addProducer();
break;
case Element.CREATIONDATE:
// you can not set the creation date, only reset it
info.addCreationDate();
break;
// content (text)
case Element.CHUNK: {
// if there isn't a current line available, we make one
if (line == null) {
carriageReturn();
}
// we cast the element to a chunk
PdfChunk chunk = new PdfChunk((Chunk) element, anchorAction);
// we try to add the chunk to the line, until we succeed
{
PdfChunk overflow;
while ((overflow = line.add(chunk)) != null) {
carriageReturn();
chunk = overflow;
chunk.trimFirstSpace();
}
}
pageEmpty = false;
if (chunk.isAttribute(Chunk.NEWPAGE)) {
newPage();
}
break;
}
case Element.ANCHOR: {
Anchor anchor = (Anchor) element;
String url = anchor.getReference();
leading = anchor.getLeading();
if (url != null) {
anchorAction = new PdfAction(url);
}
// we process the element
element.process(this);
anchorAction = null;
break;
}
case Element.ANNOTATION: {
if (line == null) {
carriageReturn();
}
Annotation annot = (Annotation) element;
Rectangle rect = new Rectangle(0, 0);
if (line != null)
rect = new Rectangle(annot.llx(indentRight() - line.widthLeft()), annot.lly(indentTop() - currentHeight), annot.urx(indentRight() - line.widthLeft() + 20), annot.ury(indentTop() - currentHeight - 20));
PdfAnnotation an = PdfAnnotationsImp.convertAnnotation(writer, annot, rect);
annotationsImp.addPlainAnnotation(an);
pageEmpty = false;
break;
}
case Element.PHRASE: {
// we cast the element to a phrase and set the leading of the document
leading = ((Phrase) element).getLeading();
// we process the element
element.process(this);
break;
}
case Element.PARAGRAPH: {
// we cast the element to a paragraph
Paragraph paragraph = (Paragraph) element;
addSpacing(paragraph.spacingBefore(), leading, paragraph.getFont());
// we adjust the parameters of the document
alignment = paragraph.getAlignment();
leading = paragraph.getTotalLeading();
carriageReturn();
// we don't want to make orphans/widows
if (currentHeight + line.height() + leading > indentTop() - indentBottom()) {
newPage();
}
indentation.indentLeft += paragraph.getIndentationLeft();
indentation.indentRight += paragraph.getIndentationRight();
carriageReturn();
PdfPageEvent pageEvent = writer.getPageEvent();
if (pageEvent != null && isParagraph)
pageEvent.onParagraph(writer, this, indentTop() - currentHeight);
// if a paragraph has to be kept together, we wrap it in a table object
if (paragraph.getKeepTogether()) {
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100f);
PdfPCell cell = new PdfPCell();
cell.addElement(paragraph);
cell.setBorder(Table.NO_BORDER);
table.addCell(cell);
this.add(table);
}
else {
indentation.paragraph += paragraph.getIndentationLeft();
line.setExtraIndent(paragraph.getFirstLineIndent());
element.process(this);
indentation.paragraph -= paragraph.getIndentationLeft();
}
addSpacing(paragraph.spacingAfter(), paragraph.getTotalLeading(), paragraph.getFont());
if (pageEvent != null && isParagraph)
pageEvent.onParagraphEnd(writer, this, indentTop() - currentHeight);
alignment = Element.ALIGN_LEFT;
indentation.indentLeft -= paragraph.getIndentationLeft();
indentation.indentRight -= paragraph.getIndentationRight();
carriageReturn();
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;
boolean hasTitle = section.getTitle() != null;
// if the section is a chapter, we begin a new page
if (section.isTriggerNewPage()) {
newPage();
}
// otherwise, we begin a new line
else {
newLine();
}
if (hasTitle) {
float fith = indentTop() - currentHeight;
int rotation = pageSize.getRotation();
if (rotation == 90 || rotation == 180)
fith = pageSize.getHeight() - fith;
PdfDestination destination = new PdfDestination(PdfDestination.FITH, fith);
while (currentOutline.level() >= section.getDepth()) {
currentOutline = currentOutline.parent();
}
PdfOutline outline = new PdfOutline(currentOutline, destination, section.getBookmarkTitle(), section.isBookmarkOpen());
currentOutline = outline;
}
// some values are set
carriageReturn();
indentation.indentLeft += section.getIndentationLeft();
indentation.indentRight += section.getIndentationRight();
indentation.sectionIndentLeft += section.getIndentationLeft();
indentation.sectionIndentRight += section.getIndentationRight();
PdfPageEvent pageEvent = writer.getPageEvent();
if (pageEvent != null)
if (element.type() == Element.CHAPTER)
pageEvent.onChapter(writer, this, indentTop() - currentHeight, section.getTitle());
else
pageEvent.onSection(writer, this, indentTop() - currentHeight, section.getDepth(), section.getTitle());
// the title of the section (if any has to be printed)
if (hasTitle) {
isParagraph = false;
add(section.getTitle());
isParagraph = true;
}
indentation.indentLeft += section.getIndentation();
indentation.sectionIndentLeft += section.getIndentation();
// we process the section
element.process(this);
// some parameters are set back to normal again
indentation.indentLeft -= section.getIndentationLeft() + section.getIndentation();
indentation.indentRight -= section.getIndentationRight();
indentation.sectionIndentLeft -= section.getIndentationLeft() + section.getIndentation();
indentation.sectionIndentRight -= section.getIndentationRight();
if (pageEvent != null)
if (element.type() == Element.CHAPTER)
pageEvent.onChapterEnd(writer, this, indentTop() - currentHeight);
else
pageEvent.onSectionEnd(writer, this, indentTop() - currentHeight);
break;
}
case Element.LIST: {
// we cast the element to a List
List list = (List) element;
if (list.isAlignindent()) {
list.normalizeIndentation();
}
// we adjust the document
indentation.listIndentLeft += list.getIndentationLeft();
indentation.indentRight += list.getIndentationRight();
// we process the items in the list
element.process(this);
// some parameters are set back to normal again
indentation.listIndentLeft -= list.getIndentationLeft();
indentation.indentRight -= list.getIndentationRight();
break;
}
case Element.LISTITEM: {
// we cast the element to a ListItem
ListItem listItem = (ListItem) element;
addSpacing(listItem.spacingBefore(), leading, listItem.getFont());
// we adjust the document
alignment = listItem.getAlignment();
indentation.listIndentLeft += listItem.getIndentationLeft();
indentation.indentRight += listItem.getIndentationRight();
leading = listItem.getTotalLeading();
carriageReturn();
// we prepare the current line to be able to show us the listsymbol
line.setListItem(listItem);
// we process the item
element.process(this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -