I just copied a program from website (I am sure it was written by Bruno) but does not work for me. I just have a very plain pdf fie and interested to insert buttons in it using this program exactly. But it does not insert the button on the file. My original file is in "/WEB-INF/design100.pdf".
package admin_pack;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PushbuttonField;
import com.itextpdf.text.pdf.TextField;
import javax.servlet.annotation.WebServlet;
/**
*
* @author Mushtaq
*/
@WebServlet(name = "FormServlet", urlPatterns = {"/FormServlet"})
public class FormServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/pdf");
try {
// We get a resource from our web app
InputStream is
= getServletContext().getResourceAsStream("/WEB-INF/design100.pdf");
// We create a reader with the InputStream
PdfReader reader = new PdfReader(is, null);
// We create an OutputStream for the new PDF
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Now we create the PDF
PdfStamper stamper = new PdfStamper(reader, baos);
// We add a submit button to the existing form
PushbuttonField button = new PushbuttonField(
stamper.getWriter(), new Rectangle(90, 660, 140, 690), "submit");
button.setText("POST");
button.setBackgroundColor(new GrayColor(0.7f));
button.setVisibility(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
PdfFormField submit = button.getField();
// submit.setAction(PdfAction.createSubmitForm(
// "/book/form", null, PdfAction.SUBMIT_HTML_FORMAT));
stamper.addAnnotation(submit, 1);
stamper.close();
reader.close();
// We write the PDF bytes to the OutputStream
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
} catch (DocumentException e) {
throw new IOException(e.getMessage());
}
}
}