Replacing a link within a PDF with image

Hi @imran.rafique,

Hope you were able to generate the error by now. Anxiously waiting for your response.

Regards,

@Batrinux,

We have prepared an environment and testing the ColdFusion script. It appears as some logical error in CFM script because there is no error message in the logs. However, the cfloop script is not working as expected. Kindly review the above ColdFusion script and then send us a complete example which could replicate the error of GSave method. Your response is awaited.

Hi,

Below is the complete script:
The text enclosed between ## are variables and can be replaced with any suitable value for testing purposes. The CFIMAGE tag basically generates the signature image.

<cfset filenameSigned = Mid(qryGetDocsAndTemplates.filename,1,Len(qryGetDocsAndTemplates.filename) - Len(ListLast(qryGetDocsAndTemplates.filename,".")) -1) & "_Signed." & ListLast(qryGetDocsAndTemplates.filename,".")>

<cfset newfilepath = "#qryGetDocsAndTemplates.filepath#\#filenameSigned#">

<cfset fileext = ListLast(qryGetDocsAndTemplates.filename,".")>

<cfset imagename = "#session.candidateid#_Sign">
<cfset imagepath = "#qryGetDocsAndTemplates.filepath#\#imagename#.png">

<cfimage source="#form.signatureJSONNew#" destination="#imagepath#" isBase64="yes" action="write" overwrite="true" quality="1">


	<CFOBJECT
		action = "create"
		type="java"
		class="com.aspose.pdf.Document"
		name="oPDF">
	
	<cfset oPDF.init(filepath)>								
	
	<CFOBJECT
		action = "create"
		type="java"
		class="com.aspose.pdf.Page"
		name="page">
		
	<cfset page = oPDF.getPages().get_Item(1)>
	
	<CFOBJECT
		action = "create"
		type="java"
		class="com.aspose.pdf.AnnotationSelector"
		name="selector">
	
	<CFOBJECT
		action = "create"
		type="java"
		class="com.aspose.pdf.LinkAnnotation"
		name="linkAnnotation">
	
	<CFOBJECT
		action = "create"
		type="java"
		class="com.aspose.pdf.Rectangle"
		name="rectangle">
	
	<cfset selector.init(linkAnnotation.init(page, rectangle.getTrivial()))>
	
	<cfset page.accept(selector)>
	
	<cfset list = selector.getSelected()>
	
	<cfset listcount = list.iterator()>
	
	<cfloop condition="list.iterator().hasNext() gt 0">
		
		<cfset a = list.iterator().next()>
		
		<!---<cfset a = i.next()>--->

		<CFOBJECT
			action = "create"
			type="java"
			class="com.aspose.pdf.TextAbsorber"
			name="textAbsorber">
			
		<cfset textAbsorber.getTextSearchOptions().setLimitToPageBounds(true)>
		<cfset textAbsorber.getTextSearchOptions().setRectangle(a.getRect())>
		<cfset page.accept(textAbsorber)>
		<cfset textVal = textAbsorber.getText()>
		<cfif textVal is "Click here to Sign">
			<cfset annot = a>
			
			<cfobject
				action="create"
				type="java"
				class="java.io.FileInputStream"
				name="imageStream">
			
			<cfset imageStream.init(imagepath)>
			
			<cfset page.getResources().getImages().add(imageStream)>
			
			<CFOBJECT
				action = "create"
				type="java"
				class="com.aspose.pdf.Operator"
				name="Operator">
												
			<cfset page.getContents().add(Operator.GSave())>
			
			<CFOBJECT
				action = "create"
				type="java"
				class="com.aspose.pdf.Rectangle"
				name="rectangle">
			
			<cfset rectangle.init(annot.getRect().getLLX(), annot.getRect().getLLY(), annot.getRect().getLLX() + 80, annot.getRect().getLLY() + 30)>
			
			<CFOBJECT
				action = "create"
				type="java"
				class="com.aspose.pdf.Matrix"
				name="matrix">
			
			<cfset matrix.init(rectangle.getURX() - rectangle.getLLX(), 0, 0, rectangle.getURY() -  rectangle.getLLY(), rectangle.getLLX(), rectangle.getLLY())>
			
			<cfset page.getContents().add(CreateObject("java","com.aspose.pdf.Operator").ConcatenateMatrix(matrix))>
			
			<cfset ximage = page.getResources().getImages().get_Item(page.getResources().getImages().size())>
			
			<cfset page.getContents().add(CreateObject("java","com.aspose.pdf.Operator").Do(ximage.getName()))>
			
			<cfset page.getContents().add(CreateObject("java","com.aspose.pdf.Operator").GRestore())>
			
			<cfset page.getAnnotations().delete(annot)>
			
			<cfset oPDF.save(newfilepath)>
			
		</cfif>
	</cfloop>
				
		
<CFOBJECT
	action = "create"
	type="java"
	class="com.aspose.pdf.Document"
	name="docPDF">

<CFOBJECT
	action = "create"
	type="java"
	class="com.aspose.pdf.TextFragmentAbsorber"
	name="textFragmentAbsorber">
	
	<cfset docPDF.init(newfilepath)>
	<cfset pageCount = docPDF.getPages().size()>
												
	<cfset textFragmentAbsorber.init("[CandidateSignatureDate]")>
	<cfloop from="1" to="#pageCount#" index="eachPage">
	
		<cfset docPDF.getPages().get_Item(eachPage).Accept(textFragmentAbsorber)>
		<cfset textFragmentCollection = textFragmentAbsorber.getTextFragments()>
		<cfset textFragmentCount = textFragmentAbsorber.getTextFragments().size()>
		
		<cfset currdate = DateFormat(now(),"mm/dd/yyyy")>
		
		<cfloop from="1" to="#textFragmentCount#" index="eachItem">
			<cfset textFragmentAbsorber.getTextFragments().get_Item(eachItem).setText(javacast("string",currdate))>
		</cfloop>
		
	</cfloop>
	
	<cfset docPDF.save(newfilepath)>
								
	<cfset docPDF.close()>

Kindly let me know what is incorrect in the above code.
Regards,
Tariq

@Batrinux,

<cfobject action = "create" type="java" class="com.aspose.pdf.Operator" name="Operator">
<cfset page.getContents().add(Operator.GSave())>

The ColdFusion script is not an alternative of this line of code:
page.getContents().add(new com.aspose.pdf.Operator.GSave());

We managed to replicate the problem of GSave error in our environment. It has been logged (with all details) under the ticket ID PDFJAVA-37454 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.

Hi,

Is there any workaround? Any alternate piece of code that does not make use of the statement that causes the error?

Regards,

@Batrinux,

We are afraid that there is no workaround at the moment. We recommend our clients to post their critical issues in the paid support forums. In order to escalate the priority, you can also post the linked ticket Id in the paid support forum. Please refer to this help topic: Aspose - Paid Support Helpdesk

Hi @imran.rafique,

While we figure out the rest, I will resort to the method of putting the image over the link just so that the link gets overlapped with the signature image. However the signatures are not being placed at the correct location. I think it has something to do with the way aspose calculates coordinates.
I am using the following code to get the coordinates for the text:

					<CFOBJECT
						action = "create"
						type="java"
						class="com.aspose.pdf.Document"
						name="docPDF">
					
					<CFOBJECT
						action = "create"
						type="java"
						class="com.aspose.pdf.TextFragmentAbsorber"
						name="textFragmentAbsorber">
						

						<cfset docPDF.init(filepath)>
						<cfset pageCount = docPDF.getPages().size()>
						<cfset textFragmentAbsorber.init("[CandidateSignature]")>
						<cfloop from="1" to="#pageCount#" index="eachPage">
						
							<cfset docPDF.getPages().get_Item(eachPage).Accept(textFragmentAbsorber)>
							<cfset textFragmentCollection = textFragmentAbsorber.getTextFragments()>
							<cfset textFragmentCount = textFragmentAbsorber.getTextFragments().size()>
							<cfset positionList = "">
							
							<cfloop from="1" to="#textFragmentCount#" index="eachItem">
								<cfset position = textFragmentAbsorber.getTextFragments().get_Item(eachItem).getRectangle().getLLX() & "," & textFragmentAbsorber.getTextFragments().get_Item(eachItem).getRectangle().getLLY() & "," & textFragmentAbsorber.getTextFragments().get_Item(eachItem).getRectangle().getURX() & "," & textFragmentAbsorber.getTextFragments().get_Item(eachItem).getRectangle().getURY()>
								<cfset positionList = ListAppend(positionList,"#eachPage#^#position#",";")>
								<cfset candidateSignatureFieldExists = "1">
							</cfloop>
							
						</cfloop>

And I am using the below code to place the image over the text [CandidateSignature] using the coordinates thus determined:

However the signature image gets placed correctly if the document is single page. If the document has multiple pages and signatures are to be placed on lets say the second page, the image is being placed somewhere else.

I think the place where I get the coordinates, they are not in reference to each page but rather the whole document. However when I place the signatures it is placing in reference to the page number.

Can you kindly suggest how I can place the image correctly with coordinates for the whole document and not page wise?

Regards

@Batrinux,

Kindly send us your multi-page PDF document, signature images and highlight the target places. We will investigate this scenario and share our findings with you.