一個PDF我用這個技術來插入圖像如何將鏈接添加到使用ColdFusion和iText的
Adding a dynamic image to a PDF using ColdFusion and iText
現在,我需要在X/Y
插入一個鏈接到一個外部URL和文本里面有itext和ColdFusion。
有人可以幫我做這個嗎?
謝謝。
一個PDF我用這個技術來插入圖像如何將鏈接添加到使用ColdFusion和iText的
Adding a dynamic image to a PDF using ColdFusion and iText
現在,我需要在X/Y
插入一個鏈接到一個外部URL和文本里面有itext和ColdFusion。
有人可以幫我做這個嗎?
謝謝。
下面是粗略的例子,適用於CF9。有可能更優雅的方法,但這應該給你的基本想法。
注 - IIRC CF8使用早期版本的iText(1.4)。 CF9使用2.1.0。所以我相對確定它不會按照CF8的原樣運行。如果需要,您可以使用JavaLoader.cfc運行更高版本。
更新:修改爲顯示定義特定字體,大小和顏色的一種方式。正確的設置取決於您的系統,所需的字體,編碼等等。
<cfscript>
inputPath = "c:\sourceFile.pdf";
outputPath = "c:\sourceFileWithLink.pdf";
try {
// initialize objects
pdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader").init(inputPath);
outStream = createObject("java", "java.io.FileOutputStream").init(outputPath);
pdfStamper = createObject("java", "com.lowagie.text.pdf.PdfStamper").init(pdfReader, outStream);
// create a chunk with a link to www.google.com
chunk = createObject("java", "com.lowagie.text.Chunk").init("Go To Google");
chunk.setAnchor("http://www.google.com");
//////////////////////////////////////////
// Define embedded font
BaseFont = createObject("java", "com.lowagie.text.pdf.BaseFont");
Font = createObject("java", "com.lowagie.text.Font");
bf = BaseFont.createFont("c:/windows/fonts/Framd.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
// Create the main font object (size 14)
Color = createObject("java", "java.awt.Color");
textFont = Font.init(bf, 14, Font.UNDERLINE, Color.RED);
// Apply the font to the chunk text
chunk.setFont(textFont);
//////////////////////////////////////////
// prepare to write the link onto the *first* page only
cb = pdfStamper.getOverContent(1); // first page
ct = createObject("java", "com.lowagie.text.pdf.ColumnText").init(cb);
ct.addElement(chunk);
// position towards bottom right of page
page = pdfReader.getPageSize(1);
llx = page.getRight()-200;
lly = page.getBottom();
urx = page.getRight();
ury = page.getBottom() + 36;
// initialize column dimensions
ct.setSimpleColumn(llx, lly, urx, ury);
// write the text
ct.go();
WriteOutput("Finished!");
}
finally
{
// cleanup
if (IsDefined("pdfStamper")) {
pdfStamper.close();
}
if (IsDefined("outStream")) {
outStream.close();
}
}
</cfscript>
Hooo .. 奇妙..多數民衆贊成在工作! 你能解釋一下如何改變字體,大小,顏色和unlerline嗎? 的文字? – Alain 2010-02-10 16:04:03
創建一個Font對象並將其應用於包含該文本的Chunk。您可能需要先創建一個BaseFont和java.awt.Color對象。然後使用它們創建一個具有所需大小,樣式和顏色的Font()。 http://api.itextpdf.com/com/itextpdf/text/Font.html – Leigh 2010-02-10 17:07:03
糟糕,這是錯誤版本的API(5.0.0)。這看起來更接近 http://www.docjar.com/docs/api/com/lowagie/text/Font.html – Leigh 2010-02-10 17:49:55
你是什麼意思的「文本」。鏈接的文本,或別的..? – Leigh 2010-02-10 01:57:26
是的,鏈接文本 – Alain 2010-02-10 05:12:21
只是好奇,但你嘗試使用cfpdf動作=「addWatermark」添加圖像?無論如何,由於您是在overContent上撰寫的,所以它本質上是一回事。它似乎支持所有需要的屬性:不透明度,頁面,前景和位置。值得拍攝無論如何.. – Leigh 2010-02-10 08:38:29