一个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