itext 加载 html 标签,itext 输出html

本文介绍了如何使用iText库将HTML内容转换为PDF,并展示了如何处理HTML标签,包括图像居中、段落对齐等。通过示例代码详细解释了关键步骤和方法。
摘要由CSDN通过智能技术生成

a4c26d1e5885305701be709a3d33442f.png生成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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值