生成html预览结果
itext生成pdf及每页pdf对应的html
PdfDocument.writeLineToContent
float writeLineToContent(final PdfLine line, final
PdfContentByte text,
final PdfContentByte graphics, final Object currentValues[],
final float ratio) throws DocumentException {
//
ObjectForHtml objectfothtml = new ObjectForHtml();
if (GlobalVariables.OUTPUTHTML) {
objectfothtml.setLine(line);
objectfothtml.setText(text);
objectfothtml.setGraphics(graphics);
objectfothtml.setWidth(line.getOriginalWidth());
objectfothtml.setTop(text.getYTLM());
objectfothtml.setContentlist(new ArrayList());
}
// 控制图片居中
if (GlobalVariables.ImageAlignCenter
&& (line.alignment ==
Element.ALIGN_UNDEFINED || line.alignment ==
Element.ALIGN_JUSTIFIED)) {
boolean imgflag = true;
for (int i = 0; i < line.line.size(); i++) {
PdfChunk ckk = line.line.get(i);
if (!ckk.isImage()) {
if (ckk.value.equals(" ") || ckk.value.equals(" ")
|| ckk.value.equals("") || ckk.value.equals("
")) {
line.line.remove(i);
line.width += ckk.width(ckk.value);
i--;
} else {
imgflag = false;
}
}
}
if (imgflag) {
text.state.xTLM = text.getPdfDocument().left()
+ (text.state.xTLM - text.getPdfDocument().left()) / 2
+ (line.width) / 2;
text.state.tx = text.getXTLM();
line.alignment = Element.ALIGN_CENTER;
}
// if(line.getChunk(0).isImage()){
// text.state.xTLM = text.state.xTLM
// + (line.width) / 2;
// line.alignment = Element.ALIGN_CENTER;
// }
}
// end
PdfFont currentFont = (PdfFont) currentValues[0];
float lastBaseFactor = ((Float)
currentValues[1]).floatValue();
PdfChunk chunk;
int numberOfSpaces;
int lineLen;
boolean isJustified;
float hangingCorrection = 0;
float hScale = 1;
float lastHScale = Float.NaN;
float baseWordSpacing = 0;
float baseCharacterSpacing = 0;
float glueWidth = 0;
float lastX = text.getXTLM() + line.getOriginalWidth();
numberOfSpaces = line.numberOfSpaces();
lineLen = line.getLineLengthUtf32();
// does the line need to be justified?
isJustified = line.hasToBeJustified()
&& (numberOfSpaces != 0 ||
lineLen > 1);
int separatorCount = line.getSeparatorCount();
if (separatorCount > 0) {
glueWidth = line.widthLeft() / separatorCount;
} else if (isJustified &&
separatorCount == 0) {
if (line.isNewlineSplit()
&& line.widthLeft()
>= lastBaseFactor
* (ratio * numberOfSpaces + lineLen - 1)) {
if (line.isRTL()) {
text.moveText(line.widthLeft() - lastBaseFactor
* (ratio * numberOfSpaces + lineLen - 1), 0);
}
baseWordSpacing = ratio * lastBaseFactor;
baseCharacterSpacing = lastBaseFactor;
} else {
float width = line.widthLeft();
PdfChunk last = line.getChunk(line.size() - 1);
if (last != null) {
String s = last.toString();
char c;
if (s.length() > 0
&& hangingPunctuation.indexOf((c
= s.charAt(s
.length() - 1))) >= 0) {
float oldWidth = width;
width += last.font().width(c) * 0.4f;
hangingCorrection = width - oldWidth;
}
}
float baseFactor = width
/ (ratio * numberOfSpaces + lineLen - 1);
baseWordSpacing = ratio * baseFactor;
baseCharacterSpacing = baseFactor;
lastBaseFactor = baseFactor;
}
} else if (line.alignment == Element.ALIGN_LEFT
|| line.alignment == Element.ALIGN_UNDEFINED) {
lastX -= line.widthLeft();
}
int lastChunkStroke = line.getLastStrokeChunk();
int chunkStrokeIdx = 0;
float xMarker = text.getXTLM();
float baseXMarker = xMarker;
float yMarker = text.getYTLM();
boolean adjustMatrix = false;
float tabPosition = 0;
// looping over all the chunks in 1 line
for (Iterator j = line.iterator(); j.hasNext();) {
// new
LineContent linecontent = new LineContent();
HashMap map = new HashMap();
// end
chunk = j.next();
if (isTagged(writer) &&
chunk.accessibleElement != null) {
text.ope