java - pdfBox add different lines to pdf -


i'm looking generating pdf-document. @ moment i'm trying out different approaches. want more 1 line in pdf-document. using helloworld code example came ...

package org.apache.pdfbox.examples.pdmodel;  import java.io.ioexception;  import org.apache.pdfbox.pdmodel.pddocument; import org.apache.pdfbox.pdmodel.pdpage;  import org.apache.pdfbox.pdmodel.pdpagecontentstream;  import org.apache.pdfbox.pdmodel.font.pdfont; import org.apache.pdfbox.pdmodel.font.pdtype1font;  /**  * creates "hello world" pdf using built-in helvetica font.  *  * example taken pdf file format specification.  */ public final class helloworld {     private helloworld()     {     }      public static void main(string[] args) throws ioexception     {          string filename = "line.pdf";         string message = "line";          pddocument doc = new pddocument();         try         {             pdpage page = new pdpage();             doc.addpage(page);              pdfont font = pdtype1font.helvetica_bold;              pdpagecontentstream contents = new pdpagecontentstream(doc, page);             contents.begintext();             contents.setfont(font, 12);             // loop create 25 lines of text             (int y = 0; y< 25; y++) {                 int ty = 700 + y * 15;                 contents.newlineatoffset(100, ty);                 //contents.newlineatoffset(125, ty);                 //contents.showtext(integer.tostring(i));                 contents.showtext(message + " " + integer.tostring(i));                 system.out.println(message + " " + integer.tostring(i));             }             contents.endtext();             contents.close();              doc.save(filename);         }                 {             doc.close();             system.out.println("helloworld finished after 'doc.close()'.");         }     } } 

but looking @ resulting document see "line 0" once, , no other lines. doing wrong?

pdf document 1 line

your issue think pdpagecontentstream.newlineatoffset uses absolute coordinates. not case, uses relative coordinates, cf. javadocs:

/**  * td operator.  * move start of next line, offset start of current line (tx, ty).  *  * @param tx x translation.  * @param ty y translation.  * @throws ioexception if there error writing stream.  * @throws illegalstateexception if method not allowed called @ time.  */ public void newlineatoffset(float tx, float ty) throws ioexception 

so additional lines way off visible page area.

thus, might want this:

... contents.begintext(); contents.setfont(font, 12); contents.newlineatoffset(100, 700); // loop create 25 lines of text (int = 0; < 25; i++) {     contents.showtext(message + " " + integer.tostring(i));     system.out.println(message + " " + integer.tostring(i));     contents.newlineatoffset(0, -15); } contents.endtext(); ... 

here start @ 100, 700 , move down each line 15.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -