這是我的代碼來創建一個PDF從Java與itext PDF庫,我成功了。我現在的問題是,我不能添加javascript代碼,無論從onload調用HTML還是從Java代碼都沒有。 我沒有任何異常。 我做錯了什麼?有可能實現嗎?生成PDF文件時加載來自PDF與Java itextpdf的JavaScript
public class App {
public static final String src = "new.pdf";
public static final String HTML = "test.html";
public static void main(String[] args) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(src));
// writer.addJavaScript("<script>alert('asdas')</script>");
document.open();
document.setJavaScript_onLoad("yourFunction()");
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(HTML));
document.close();
}
}
<html>
<head>
<style>.col{padding:3px 20px 3px 20px}</style>
<script type="text/javascript">
function yourFunction(){
alert('You clicked the bottom text');
}
</script>
</head>
<body style="font-family:tahoma" onload="yourFunction()">
<div style="background:rgb(230,230,230); padding:5px ;border:1px solid black;">
<b style="color:rgb(51,153,255)">Sample header</b>
<img style="float:right" height="25px" width="80px" src="resources/images/itext.png"/>
</div>
<br/>
<table border='0' style='border-collapse: collapse;'>
<tr>
<td class="col">String 1</td>
<td class="col">: 1234354545</td>
</tr>
<tr>
<td class="col">String 32</td>
<td class="col">: rere</td>
</tr>
<tr>
<td class="col">String 3</td>
<td class="col">: ureuiu</td>
</tr>
<tr>
<td class="col">Date</td>
<td class="col">: dfdfjkdjk</td>
</tr>
</table>
<br/>
<br/>
<br/>
<hr/>
<br/>
Contact us
</body>
</html>
我看到這篇文章,我認爲這是可能的舊版本http://javabeat.net/javascript-communication-between-html-and-pdf-in-itext/ – xXxpRoGrAmmErxXx
我誤解了你的問題一點,我我會擴大我的答案。 –