ColdFusion CFDOCUMENT与其他PDF的链接
我现在正在使用cfdocument标签创建PDF。PDF只不过是一堆指向其他PDF的链接 所以我创建了这个PDF索引,链接都是HREFColdFusion CFDOCUMENT与其他PDF的链接,pdf,coldfusion,itext,cfdocument,Pdf,Coldfusion,Itext,Cfdocument,我现在正在使用cfdocument标签创建PDF。PDF只不过是一堆指向其他PDF的链接 所以我创建了这个PDF索引,链接都是HREF <a href="Another_PDF.pdf">Another PDF</a> 如果我将localURL属性设置为“否”,则我的URL中包含整个web路径: <a href="http://www.mywebsite.com/media/PDF/Another_PDF.pdf">Another PDF</a&g
<a href="Another_PDF.pdf">Another PDF</a>
如果我将localURL属性设置为“否”,则我的URL中包含整个web路径:
<a href="http://www.mywebsite.com/media/PDF/Another_PDF.pdf">Another PDF</a>
如果我将localURL属性设置为“yes”,那么我会得到:
<a href="File:/D:/website/media/PDF/Another_PDF.pdf">Another PDF</a>
所以这个索引PDF将放在CD上,所有链接的PDF都将放在它旁边,所以我需要一个相对链接。。。更像:
<a href="Another_PDF.pdf">Another PDF</a>
cfdocument似乎没有这样做。我可以修改文档的文件名并将其设置为“F”ile:///Another_PDF.pdf“但这也不行,因为我不知道CD驱动器的驱动器号。。。或者,如果这些文件将在CD上的一个目录中结束
是否有一种方法(可能使用iText或其他方法)可以在创建PDF后打开它,并将URL链接转换为实际的PDF GoTo标记
我知道这有点牵强,但我对此束手无策
因此,我设法进入了对象,但我仍然在努力
转换自:
5 0 obj<</C[0 0 1]/Border[0 0 0]/A<</URI(File:///75110_002.PDF)/S/URI>>/Subtype/Link/Rect[145 502 184 513]>>endobj
50 objendobj
为此:
19 0 obj<</SGoToR/D[0/XYZ null null 0]/F(75110_002.PDF)>>endobj
20 0 obj<</Subtype/Link/Rect[145 502 184 513]/Border[0 0 0]/A 19 0 R>>endobj
19 0 objendobj
20 0 objendobj
哇,这真让我受不了!:)
因此,我成功地打开了文档,循环了链接注释,捕获了Rect坐标和链接到文档的名称(保存到一个结构数组中),然后成功地删除了作为URI链接的注释
所以现在我想我现在可以循环该结构数组,并使用createLink方法或setAction方法将注释放回文档中。但我所看到的所有这些方法的示例都附加到一块(文本)上。但是我的文档已经有了文本,所以我不需要重新制作文本链接,我只需要将链接放回同一位置
因此,我想我可以重新打开文档,查找作为链接的实际文本,然后将setAction附加到现有文本块中。。。。我找不到文本
我糟透了!:) 有一个通过修改pdf注释来更新链接操作的示例。它是用iTextSharp 5.x编写的,但是java代码没有太大的不同
该线程为注释的工作方式提供了可靠的解释。但总而言之,您需要阅读源pdf并在各个页面中循环寻找注释。提取链接并使用类似于getFileFromPath()
的方法仅用文件名替换它们
我很好奇,所以我对上面的iTextSharp代码进行了快速而丑陋的转换。免责声明,未经高度测试:
/**
Usage:
util = createObject("component", "path.to.ThisComponent");
util.fixLinks( "c:/path/to/sourceFile.pdf", "c:/path/to/newFile.pdf");
*/
component {
/**
Convert all absolute links, in the given pdf, to relative links (file name only)
@source - absolute path to the source pdf file
@destination - absolute path to save copy
*/
public function fixLinks( string source, string destination) {
// initialize objects
Local.reader = createObject("java", "com.lowagie.text.pdf.PdfReader").init( arguments.source );
Local.pdfName = createObject("java", "com.lowagie.text.pdf.PdfName");
// check each page for hyperlinks
for ( Local.i = 1; Local.i <= Local.reader.getNumberOfPages(); Local.i++) {
//Get all of the annotations for the current page
Local.page = Local.reader.getPageN( Local.i );
Local.annotations = Local.page.getAsArray( Local.PdfName.ANNOTS ).getArrayList();
// search annotations for links
for (Local.x = 1; !isNull( Local.annotations) && Local.x < arrayLen(Local.annotations); Local.x++) {
// get current properties
Local.current = Local.annotations[ Local.x ];
Local.dictionary = Local.reader.getPdfObject( Local.current );
Local.subType = Local.dictionary.get( Local.PdfName.SUBTYPE );
Local.action = Local.dictionary.get( Local.PdfName.A );
Local.hasLink = true;
//Skip this item if it does not have a link AND action
if (Local.subType != Local.PdfName.LINK || isNull(Local.action)) {
Local.hasLink = false;
}
//Skip this item if it does not have a URI
if ( Local.hasLink && Local.action.get( Local.PdfName.S ) != Local.PdfName.URI ) {
Local.hasLink = false;
}
//If it is a valid URI, update link
if (Local.hasLink) {
// extract file name from URL
Local.oldLink = Local.action.get( Local.pdfName.URI );
Local.newLink = getFileFromPath( Local.oldLink );
// replace link
// WriteDump("Changed link from ["& Local.oldLink &"] ==> ["& Local.newLink &"]");
Local.pdfString = createObject("java", "com.lowagie.text.pdf.PdfString");
Local.action.put( Local.pdfName.URI, Local.pdfString.init( Local.newLink ) );
}
}
}
// save all pages to new file
copyPDF( Local.reader , arguments.destination );
}
/**
Copy all pages in pdfReader to the given destination file
@pdfReader - pdf to copy
@destination - absolute path to save copy
*/
public function copyPDF( any pdfReader, string destination) {
try {
Local.doc = createObject("java", "com.lowagie.text.Document").init();
Local.out = createObject("java", "java.io.FileOutputStream").init( arguments.destination );
Local.writer = createObject("java", "com.lowagie.text.pdf.PdfCopy").init(Local.doc, Local.out);
// open document and save individual pages
Local.doc.open();
for (Local.i = 1; i <= arguments.pdfReader.getNumberOfPages(); Local.i++) {
Local.writer.addPage( Local.writer.getImportedPage( arguments.pdfReader, Local.i) );
}
Local.doc.close();
}
finally
{
// cleanup
if (structKeyExists(Local, "doc")) { Local.doc.close(); }
if (structKeyExists(Local, "writer")) { Local.writer.close(); }
if (structKeyExists(Local, "out")) { Local.out.close(); }
}
}
}
/**
用法:
util=createObject(“组件”、“路径到.ThisComponent”);
util.fixLinks(“c:/path/to/sourceFile.pdf”、“c:/path/to/newFile.pdf”);
*/
组成部分{
/**
将给定pdf中的所有绝对链接转换为相对链接(仅限文件名)
@source-源pdf文件的绝对路径
@目标-保存副本的绝对路径
*/
公共函数固定链接(字符串源、字符串目标){
//初始化对象
Local.reader=createObject(“java”,“com.lowagie.text.pdf.PdfReader”).init(arguments.source);
Local.pdfName=createObject(“java”,“com.lowagie.text.pdf.pdfName”);
//检查每个页面的超链接
对于(Local.i=1;Local.i我最终得到了它:
public function resetLinks( string source, string destination) {
try {
// initialize objects
Local.reader = createObject("java", "com.lowagie.text.pdf.PdfReader").init( arguments.source );
Local.pdfName = createObject("java", "com.lowagie.text.pdf.PdfName");
Local.annot = createObject("java", "com.lowagie.text.pdf.PdfAnnotation");
Local.out = createObject("java", "java.io.FileOutputStream").init( arguments.destination );
Local.stamper = createObject("java", "com.lowagie.text.pdf.PdfStamper").init(Local.reader, Local.out);
Local.PdfAction = createObject("java", "com.lowagie.text.pdf.PdfAction");
Local.PdfRect = createObject("java", "com.lowagie.text.Rectangle");
Local.PdfBorderArray = createObject("java", "com.lowagie.text.pdf.PdfBorderArray").init(javacast("float", "0"), javacast("float", "0"), javacast("float", "0"));
Local.newAnnots = [];
// check each page for hyperlinks
// Save the data to a structure then write it to an array
// then delete the hyperlink Annotation
for ( Local.i = 1; Local.i <= Local.reader.getNumberOfPages(); Local.i = Local.i + 1) {
//Get all of the annotations for the current page
Local.page = Local.reader.getPageN( Local.i );
Local.annotations = Local.page.getAsArray( Local.PdfName.ANNOTS ).getArrayList();
// search annotations for links
for (Local.x = arrayLen(Local.annotations); !isNull( Local.annotations) && Local.x > 0; Local.x--) {
// get current properties
Local.current = Local.annotations[ Local.x ];
Local.dictionary = Local.reader.getPdfObject( Local.current );
Local.subType = Local.dictionary.get( Local.PdfName.SUBTYPE );
Local.action = Local.dictionary.get( Local.PdfName.A );
Local.hasLink = true;
//Skip this item if it does not have a link AND action
if (Local.subType != Local.PdfName.LINK || isNull(Local.action)) {
Local.hasLink = false;
}
//Skip this item if it does not have a URI
if ( Local.hasLink && Local.action.get( Local.PdfName.S ) != Local.PdfName.URI ) {
Local.hasLink = false;
}
//If it is a valid URI, update link
if (Local.hasLink) {
// extract file name from URL
Local.oldLink = Local.action.get( Local.pdfName.URI );
Local.newLink = getFileFromPath( Local.oldLink );
Local.Rect = Local.dictionary.Get(PdfName.Rect);
arrayStruct = StructNew();
arrayStruct.rectSTR = Local.Rect.toString();
arrayStruct.link = Local.newLink;
arrayStruct.page = Local.i;
ArrayAppend(Local.newAnnots, arrayStruct);
// Delete
Local.annotations.remove(Local.current);
}
}
}
// Now really remove them!
Local.reader.RemoveUnusedObjects();
// Now loop over the saved annotations and put them back!!
for ( Local.z = 1; Local.z <= ArrayLen(Local.newAnnots); Local.z++) {
// Parse the rect we got save into an Array
theRectArray = ListToArray(ReplaceNoCase(ReplaceNoCase(Local.newAnnots[z].rectSTR, "[", ""), "]", ""));
// Create the GoToR action
theAction = Local.PdfAction.gotoRemotePage(javacast("string", '#Local.newAnnots[z].link#'), javacast("string", '#Local.newAnnots[z].link#'), javacast("boolean", "false"), javacast("boolean", "false"));
// Create the Link Annotation with the above Action and the Rect
theAnnot = Local.annot.createLink(Local.stamper.getWriter(), Local.PdfRect.init(javacast("int", theRectArray[1]), javacast("int", theRectArray[2]), javacast("int", theRectArray[3]), javacast("int", theRectArray[4])), Local.annot.HIGHLIGHT_INVERT, theAction);
// Remove the border the underlying underlined text will flag item as a link
theAnnot.setBorder(Local.PdfBorderArray);
// Add the Annotation to the Page
Local.stamper.addAnnotation(theAnnot, Local.newAnnots[z].page);
}
}
finally {
// cleanup
if (structKeyExists(Local, "reader")) { Local.reader.close(); }
if (structKeyExists(Local, "stamper")) { Local.stamper.close(); }
if (structKeyExists(Local, "out")) { Local.out.close(); }
}
}
公共函数重置链接(字符串源、字符串目标){
试一试{
//初始化对象
Local.reader=createObject(“java”,“com.lowagie.text.pdf.PdfReader”).init(arguments.source);
Local.pdfName=createObject(“java”,“com.lowagie.text.pdf.pdfName”);
Local.annot=createObject(“java”、“com.lowagie.text.pdf.pdfantation”);
Local.out=createObject(“java”,“java.io.FileOutputStream”).init(arguments.destination);
Local.stamper=createObject(“java”,“com.lowagie.text.pdf.PdfStamper”).init(Local.reader,Local.out);
Local.pdp=createObject(“java”,“com.lowagie.text.pdf.pdp”);
Local.PdfRect=createObject(“java”、“com.lowagie.text.Rectangle”);
Local.PdfBorderArray=createObject(“java”、“com.lowagie.text.pdf.PdfBorderArray”).init(javacast(“float”、“0”)、javacast(“float”、“0”)、javacast(“float”、“0”);
Local.newAnnots=[];
//检查每个页面的超链接
//将数据保存到结构中,然后将其写入数组
//然后删除超链接注释
对于(Local.i=1;Local.i 0;Local.x--){
//获取当前属性
Local.current=Local.annotations[Local.x];
Local.dictionary=Local.reader.getPdfObject(Local.current);
Local.subType=Local.dictionary.get(Local.PdfName.subType);
Local.action=Local.dictionary.get(Local.PdfName.A);
Local.hasLink=true;
//如果没有链接和操作,请跳过此项目
if(Local.subType!=Local.PdfName.LINK | | isNull(Local.action)){
Local.hasLink=false;
}
//如果此项目没有URI,请跳过此项目
if(Local.hasLink&&Local.action.get(Local.PdfName.S)!=Local.PdfName.URI){
Local.hasLink=false;
}
//如果它是有效的URI,请更新链接
if(Local.hasLink){
//从URL提取文件名
Local.oldLink=Local.action.get(Local.pdfName.URI);
Local.newLink=getFileFromPath(Local.oldLink);
Local.Rect=Local.dictionary.Get(PdfName.Rect);
arrayStruct=StructNew();
arrayStruct.rectSTR=Local.Rect.toString();