Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the most complicated scenario I can imagine: I have a PDF file created with Ilustrator and modified with Acrobat to have AcroFields (AcroForm) that I'm going to fill with data with this Java code, the result of that PDF file with the data in the fields is modified adding a Document.</p> <p>Actually in this case I'm dynamically generating a background that is added to a PDF that is also dynamically generated with a Document with an unknown amount of data or pages.</p> <p>I'm using JBoss and this code is inside a JSP file (should work in any JSP webserver).</p> <p>Note: if you are using IExplorer you must submit a HTTP form with POST method to be able to download the file. If not you are going to see the PDF code in the screen. This does not happen in Chrome or Firefox. </p> <pre><code>&lt;%@ page import="java.io.*, com.lowagie.text.*, com.lowagie.text.pdf.*" %&gt;&lt;% response.setContentType("application/download"); response.setHeader("Content-disposition","attachment;filename=listaPrecios.pdf" ); // -------- FIRST THE PDF WITH THE INFO ---------- String str = ""; // lots of words for(int i = 0; i &lt; 800; i++) str += "Hello" + i + " "; // the document Document doc = new Document( PageSize.A4, 25, 25, 200, 70 ); ByteArrayOutputStream streamDoc = new ByteArrayOutputStream(); PdfWriter.getInstance( doc, streamDoc ); // lets start filling with info doc.open(); doc.add(new Paragraph(str)); doc.close(); // the beauty of this is the PDF will have all the pages it needs PdfReader frente = new PdfReader(streamDoc.toByteArray()); PdfStamper stamperDoc = new PdfStamper( frente, response.getOutputStream()); // -------- THE BACKGROUND PDF FILE ------- // in JBoss the file has to be in webinf/classes to be readed this way PdfReader fondo = new PdfReader("listaPrecios.pdf"); ByteArrayOutputStream streamFondo = new ByteArrayOutputStream(); PdfStamper stamperFondo = new PdfStamper( fondo, streamFondo); // the acroform AcroFields form = stamperFondo.getAcroFields(); // the fields form.setField("nombre","Avicultura"); form.setField("descripcion","Esto describe para que sirve la lista "); stamperFondo.setFormFlattening(true); stamperFondo.close(); // our background is ready PdfReader fondoEstampado = new PdfReader( streamFondo.toByteArray() ); // ---- ADDING THE BACKGROUND TO EACH DATA PAGE --------- PdfImportedPage pagina = stamperDoc.getImportedPage(fondoEstampado,1); int n = frente.getNumberOfPages(); PdfContentByte background; for (int i = 1; i &lt;= n; i++) { background = stamperDoc.getUnderContent(i); background.addTemplate(pagina, 0, 0); } // after this everithing will be written in response.getOutputStream() stamperDoc.close(); %&gt; </code></pre> <p>There is another solution much simpler, and solves your problem. It depends the amount of text you want to add.</p> <pre><code>// read the file PdfReader fondo = new PdfReader("listaPrecios.pdf"); PdfStamper stamper = new PdfStamper( fondo, response.getOutputStream()); PdfContentByte content = stamper.getOverContent(1); // add text ColumnText ct = new ColumnText( content ); // this are the coordinates where you want to add text // if the text does not fit inside it will be cropped ct.setSimpleColumn(50,500,500,50); ct.setText(new Phrase(str, titulo1)); ct.go(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload