Skip to content

Commit

Permalink
apache pdfbox demo on W-8BEN irs form
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Lagnada (nfet) committed Oct 16, 2013
1 parent 8312f0d commit 088625a
Show file tree
Hide file tree
Showing 9 changed files with 636 additions and 7 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -35,6 +35,7 @@
<jstl-version>1.2</jstl-version>
<junit-version>4.10</junit-version>
<mail-version>1.4.5</mail-version>
<pdfbox.version>1.8.2</pdfbox.version>
<servlet-api-version>2.5</servlet-api-version>
<slf4j-version>1.5.10</slf4j-version>
<spring-security-version>3.1.3.RELEASE</spring-security-version>
Expand Down Expand Up @@ -304,6 +305,11 @@
<version>${fest-assert.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>${pdfbox.version}</version>
</dependency>

<!-- ============================================================================================== -->
<!-- Runtime only dependencies (runtime scoped) -->
Expand Down
112 changes: 112 additions & 0 deletions src/main/java/com/lagnada/xmx1024/controller/w8/W8Controller.java
@@ -0,0 +1,112 @@
package com.lagnada.xmx1024.controller.w8;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;

import static java.lang.String.format;
import static org.apache.commons.io.IOUtils.closeQuietly;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@Controller
@RequestMapping(value = "/w8")
public class W8Controller
{
@Value("classpath:fw8ben.pdf")
private Resource blankW8Pdf;

@ModelAttribute("w8Model")
private W8Model w8Model()
{
return new W8Model();
}

@RequestMapping(method = GET)
public String handleView()
{
return "w8";
}

@RequestMapping(method = POST)
public String handlePost(@Valid W8Model w8Model, BindingResult bindingResult, HttpServletResponse response) throws Exception
{
if (bindingResult.hasErrors())
{
return "w8";
}

PDDocument pdf = loadPdfDoc();
try
{
PDDocumentCatalog docCatalog = pdf.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField nameOfIndividualOrOrganizationField = acroForm.getField("f1-1.f1-1");
nameOfIndividualOrOrganizationField.setValue(w8Model.getPartOne().getBeneficialOwnerName());

PDField incorporationCountryField = acroForm.getField("f1-2.f1-2");
incorporationCountryField.setValue(w8Model.getPartOne().getIncorporationCountry());

PDField streetField = acroForm.getField("f1-3.f1-3");
streetField.setValue(w8Model.getPartOne().getPermanentResidenceAddress().getStreetAddress());

PDField cityField = acroForm.getField("f1-4.f1-4");
cityField.setValue("Hilsinki");

PDField countryField = acroForm.getField("f1-5.f1-5");
countryField.setValue("Finland");

PDField aCheckbox = acroForm.getField("c1-3.c1-3");
aCheckbox.setValue("Yes");

String test = "that";
System.out.println(test);

response.setContentType("application/pdf");
response.setHeader("Content-Disposition", format("attachment; filename=%s.pdf", "fw8ben_filled"));
pdf.save(response.getOutputStream());
}
finally
{
closePdfQuietly(pdf);
}
return null;
}

private PDDocument loadPdfDoc() throws IOException
{
InputStream is = null;
PDDocument pdfDoc;
try
{
is = blankW8Pdf.getInputStream();
pdfDoc = PDDocument.load(is);
}
finally
{
closeQuietly(is);
}
return pdfDoc;
}

private void closePdfQuietly(PDDocument pdf) throws IOException
{
if (pdf != null)
{
pdf.close();
}
}

}

0 comments on commit 088625a

Please sign in to comment.