如何利用iText的(x,y)位置添加一个PdfPTable到HTML字符串在文档中? [英] How to add a PdfPTable to the HTML string in a document at (x,y) location using iText?

查看:2112
本文介绍了如何利用iText的(x,y)位置添加一个PdfPTable到HTML字符串在文档中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的 HTML转换为PDF 使用的 iText的

我已经使用的 HTMLWorker类具有下列内容code(德precated):

I am already using HTMLWorker class (deprecated) having the following content code:

    String htmlString = "<html><body> This is my Project <table width= '50%' border='0' align='left' cellpadding='0' cellspacing='0'><tr><td>{VERTICALTEXT}</td></tr></table></body></html>";

    OutputStream file = new FileOutputStream(new File("C:\\Test.pdf"));
    Document document = new Document();
    PdfWriter.getInstance(document, file);
    document.open();
    HTMLWorker htmlWorker = new HTMLWorker(document);
    htmlWorker.parse(new StringReader(htmlString ));
    document.close();
    file.close();
}

现在我要替换 {VERTICALTEXT} 动态与一些字符串。

Now I want to replace {VERTICALTEXT} dynamically with some string.

于是我进一步增加了code如下:

PdfPTable table = null;
PdfPCell cell;
cell = new PdfPCell(new Phrase("My Vertical Text"));
cell.setRotation(90);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
String verticalLoc  = table.toString(); //this variable should hold the text "My Vertical Text" in 90 degree rotated form.

HashMap<String, String> map = new HashMap<String, String>();
map.put("VERTICALTEXT", verticalLoc);

html = new String(buffer);

for (HashMap.Entry<String, String> e : map.entrySet())
{
    String value = e.getValue() != null ? e.getValue():"";
        html = html.replace("{" + e.getKey() + "}", value);
}

htmlWorker.parse(new StringReader(htmlStr));

在输出:

{VERTICALTEXT} 替换 com.itextpdf.text.pdf.PdfPTable@41d62bcO

所需的输出:

{VERTICALTEXT} 替换我的90度垂直文本旋转形式。

推荐答案

这是解决想通了,并测试 -

的Java文件相关的code:

static PdfWriter writer;
writer = PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
PdfPTable table = new PdfPTable(2);
PdfPCell cell;
cell = new PdfPCell(new Phrase("My Vertical Text"));
cell.setRotation(90);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
htmlWorker.parse(new StringReader(htmlStr));
table.setTotalWidth(400f);
table.writeSelectedRows( 0, -1, 80, 330, writer.getDirectContent()); 

所以方法的神奇 writeSelectedRows 中放置表在(X,Y)的位置工作。

So the magic of method writeSelectedRows worked in placing the table to the (x, y) location.

其中,

x = 80
y = 330

关于<一个完整的细节href=\"http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPTable.html#writeSelectedRows%28int,%20int,%20float,%20float,%20com.itextpdf.text.pdf.PdfContentByte%29\"相对=nofollow> writeSelectedRows 。

这将帮助别人面临着同样的问题的iText 定位。

This will help somebody facing the same problems with itext positioning.

这篇关于如何利用iText的(x,y)位置添加一个PdfPTable到HTML字符串在文档中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆