Merge xfdf and fdf to PDF in Java | Aspose.PDF for Java | Code is not working

HI Team

We have purchased Aspose pdf SDK for Java. We would like to merge xfdf and fdf to pdf.

with code we were able merge xfdf to pdf but with fdf it’s not working.

here is the code we are using

case “fdf”:
appendToReport("Importing fdf - " + noteFile);
formEditor = new Form();
formEditor.bindPdf(tmp + docFileName);
FileInputStream fdfInputStream = new FileInputStream(tmp + noteFile);
formEditor.importFdf(fdfInputStream);
fdfInputStream.close();
successfulImport = true;
break;

This seems to be not working.

Question
How can we merge fdf with pdf using aspose pdf for Java

Thank you

Pinal Shah

@pinalshah17657,

The FDF file contains form field data and you can import FDF data into the PDF document in the same way as you shared in the code example. Kindly send us the source PDF, FDF, code and the details of problematic behavior. We will investigate and share our findings with you.

Thanks for the response. Here is the complete code. It’s working for xfdf but not for fdf. here is the method.

private void merge(IDfSession session, String parentId, List<String> childIds) {
	LOGGER.debug(ENTER + " merge()");
	try {
		
		//tmp stores pdf to work on
		String tmp = "tmp\\";
		File file = new File(tmp);
		if(!file.exists()) {
			file.mkdir();
		}
		appendToReport("Current parent id: " + parentId + " - child ids: " + childIds.toString());
		//get pdf document from docbase
		IDfSysObject pdfDoc = (IDfSysObject) session.getObject(new DfId(parentId));
		String docFileName = getDocFile(pdfDoc, tmp, "pdf");
		if(docFileName == null) {
			return;
		}
		
		PdfAnnotationEditor editor = null;
		Form formEditor = null;
		
		boolean successfulImport, containsSuccess=false;
		
		String successfulImports = "";
		List<String[]> errors = new ArrayList<String[]>(); 
		
		IDfSysObject note;
		
		for(String id : childIds) {
			//get note from docbase
			note = (IDfSysObject) session.getObject(new DfId(id));
			String contentType = note.getString("a_content_type");
			String noteFile = getDocFile(note, tmp, contentType);
			
			if(noteFile != null) {
				
				try {
					successfulImport = false;
					
					switch(contentType) {
					case "xfdf":
						editor = new PdfAnnotationEditor();
						appendToReport("Importing xfdf - " + noteFile);
						editor.bindPdf(tmp + docFileName);
						try {
							editor.importAnnotationFromXfdf(tmp + noteFile);
						}catch(Exception e) {
							formEditor = new Form();
							formEditor.bindPdf(tmp+docFileName);
							FileInputStream xfdfInputStream = new FileInputStream(tmp + noteFile);
							formEditor.importXfdf(xfdfInputStream);
							xfdfInputStream.close();
						}
						successfulImport = true;
						break;
					case "fdf":
						appendToReport("Importing fdf - " + noteFile);
						formEditor = new Form();
						formEditor.bindPdf(tmp + docFileName);
						FileInputStream fdfInputStream = new FileInputStream(tmp + noteFile);
						formEditor.importFdf(fdfInputStream);
						fdfInputStream.close();
						successfulImport = true;
						break;
					default:
						break;
					}
					if(successfulImport) {
						editor.save(tmp + docFileName);
						containsSuccess = true;
						appendToReport("File imported to " + docFileName + ": parentId - " + parentId + " childId - " + id);
						successfulImports += (id + "-" + noteFile + "|");
					}
					else {
						errors.add(new String[] {docFileName, noteFile, "Unsuccessful Import!"});
						appendToReport("Import failed: parentId - " + parentId + " childId - " + id );
					}
				}catch(Exception ex) {
					LOGGER.error("Error importing note - " + noteFile + ": " + ex.toString());
					appendToReport("Error importing note - " + noteFile);
					
					errors.add(new String[] {docFileName, noteFile, ex.toString()});
				}
				finally {
					if(editor != null) {
						editor.close();
					}
				}
			}
			if(containsSuccess) {
				pdfDoc.addRenditionEx2(new File(tmp+docFileName).getAbsolutePath(), "pdf", 0, page_mod, null, false, false, false);
				pdfDoc.save();
				writeToCSVFile(parentId, docFileName, successfulImports);
			}
		
			for(String[] e : errors) {
				report.writeToErrorCSV(e[0], e[1], e[2]);
			}
		}
	}catch(DfException e) {
		LOGGER.error("Exception processing query!", e);
		appendToReport("!!ERROR!! There was an error during processing. For additional information please check the log files");
	}

}

private String getDocFile(IDfSysObject docObj, String dest, String ext) {
	appendToReport("Getting document from docbase...");
	String fileName = null;
	try {
		appendToReport("Doc extension - " + docObj.getContentType());
		if(docObj.getContentType().toString().equals(ext)) {
			fileName = docObj.getObjectName()+"."+ext;	
			docObj.getFileEx(dest+fileName, ext, 0, false);
		}
	} catch (DfException e) {
		appendToReport("Could not get file.: " + e.getMessage());
		LOGGER.debug("!!ERROR!!Could not get file docName!");
	}
	appendToReport("Got document - " + fileName);
	return fileName;
}

@pinalshah17657,

Kindly send us your source PDF and FDF files as well as please also simplify the code example because it contains unknown objects. We recommend our clients to share all details of the use case so that we could replicate the problem in our environment.

HI Imran

We have sent you all information last week. Can we get an update on this one?

It’s showstopper for us.

Thank you

Pinal Shah

As there is no way to attach files here we have sent sample files via email.

@pinalshah17657,

I have not received your email message in my inbox. There is an upload button in the header of the post editor and you can upload the Zip of all files in your post. Your response is awaited.

Test Pdf.pdf (42.5 KB)
Test Pdf.zip (41.4 KB)

HI Imran

Please find attached Test pdf and fdp for your reference.
Thank you for your support, Please lets know it’s very urgent for us.

Thank you and have a wonderful day.

1 Like

Hi @imran.rafique

Kindly expedite our request.

Thanks.

@pinalshah17657,

There is no form in your source PDF. Are you seeking to import comments? Kindly send us your expected output PDF document. We will take a closer look, and then share our findings with you.

Yes we would like to import comments from the attached fdf to attached pdf

Can you please let’s know.

@pinalshah17657,

The importFdf method of the Form class is not working as expected. We have logged an investigation under the ticket ID PDFJAVA-37276 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.

HI Imran
Any update on this bug?

Can we get an ETA for the fix?

Normally how much time it takes from engineering side to review and provide fix?

Thank you
Pinal Shah

@pinalshah17657,

The linked ticket ID PDFJAVA-37276 has just been identified and pending for the analysis phase. Our product team will investigate as per their development schedules. We have logged an ETA request under the same ticket ID PDFJAVA-37276. We will let you know once a significant progress has been made in this regard.

1 Like

HI Imran

Any update on ETA?

@pinalshah17657,

There is no update on the linked ticket ID PDFJAVA-37276. It is difficult for us to share an estimate before the completion of the analysis phase. We will let you know once we are able to share an estimate.

@imran.rafique

Otherwise, Do we have any option to convert FDF into XFDF or FDF into Intermediate Format into XFDF?

For Ex.:

@mspsrikt,

There is no way to convert FDF to XFDF. We have logged an investigation add its support under the ticket PDFJAVA-37293 in our issue tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.

@imran.rafique Kindly provide an update. Thanks a lot.

@mspsrikt,

The linked ticket IDs PDFJAVA-37276 and PDFJAVA-37293 have just been identified. Our product team will investigate as per their development schedules. We will let you know once a significant progress has been made in this regard.