I have been trying to figure out how to fill a PDF form with contact data and I found a helpful post here. I followed the instructions but it didn't say what to do with the code or how to set up the button. I tried putting the first two blocks of code in an apex class and then the third as a Visualforce page. I then tried to add a button on the contact to pull up that page but it doesn't let me choose the page.

Here is my Apex Class.

public class myXFDFController {

Private String getXmlString(Contact c) {
    String s = '<?xml version="1.0" encoding="UTF-8"?>' +
        '<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">' +
        '<f href="https://www.riverblufftech.com/wp-content/uploads/2015/11/myForm.pdf"/>' +
        '<fields>' +
        '<field name="First Name, Middle Name, Last Name & Suffix"><value>' + c.firstname + ' ' + c.lastname + '</value></field>' +
        '<field name="Email address"><value>' + c.Email + '</value></field>' +
        '</fields><ids original="E1D407DB3A404A389E73EFCE11A5CE0B" modified="46721BE9257A47A3A1F3CCEE45E9F19A"/>' +
    return s;

public PageReference XFDFInit() {
    Contact c = [SELECT Id, firstname, lastname, email FROM Contact WHERE id = :ApexPages.currentPage().getParameters().get('id')];
    String xmlContent = getXmlString(c);
    Attachment attachment = new Attachment();
        attachment.Body = Blob.valueOf(xmlContent);
        attachment.Name = c.lastname + c.firstname + '.XFDF';
        attachment.ParentId = c.Id;
        insert attachment;

    PageReference contactPage = new PageReference('/' + c.id);
    return contactPage;

And this is my Visualforce Page, I feel like there should be more here but the post I linked to earlier seemed to say you only need this.

<apex:page showHeader="true" controller="myXFDFController" action="{!XFDFInit}"></apex:page>
  • Hi. Have you managed to make this form work? I am trying to do the same but to no avail.
    – Saranya
    Mar 26, 2018 at 9:30

1 Answer 1


Try to use this code as your page.

<apex:page showHeader="true" standardController="Contact"
       extensions="myXFDFController" action="{!XFDFInit}"></apex:page>

To add a page to a button(that is linked to an object), the page needs the object as a standardcontroller. By using extensions, you'll still be able to use all functionalities of your custom controller.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .