PDFBox does not write the message I want into the page










3















I'm looking for at least an hour at my code but I can't find the bug. I'm using PDFBox to create PDF's (PDFBox HelloWorld Example). To learn how PDFBox works I just wanted to create some pages with "hello world" and the page number like "hello world 1", "hello world 2" and so on. As you can see I created a for loop to create six pages.



private void drawPDF(PDDocument doc, File file) throws IOException 
for (int pageIndex = 0; pageIndex < 6; pageIndex++)
PDPage page = new PDPage(PDRectangle.A4);
doc.addPage(page);
PDFont font = PDType1Font.HELVETICA_BOLD;
String message = "hello world " + (pageIndex + 1);
float stringHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() * FONT_SIZE;
PDRectangle pageSize = page.getMediaBox();
try (PDPageContentStream contents = new PDPageContentStream(doc, page))
contents.beginText();
contents.setFont(font, FONT_SIZE);
contents.setTextMatrix(Matrix.getTranslateInstance(0, pageSize.getHeight() - stringHeight / 1000f));
contents.showText(message);
System.out.println(message + " - " + doc.getNumberOfPages());
contents.endText();


doc.save(file);



In my console I get the following output (first number is pageIndex, second number is doc.getNumberOfPages()):



hello world 1 - 1
hello world 2 - 2
hello world 3 - 3
hello world 4 - 4
hello world 5 - 5
hello world 6 - 6


This is my load function to view the pdf.



private final ObservableList<Image> pdfFilePages = FXCollections.observableArrayList();


private void loadFile(File file) throws FileNotFoundException, IOException
if (file != null)
ByteBuffer buffer = null;
try (RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel())
buffer = ByteBuffer.allocate((int) channel.size());
channel.read(buffer);
buffer.flip();

PDFFile pdfFile = new PDFFile(buffer);
List<Image> pages = new ArrayList<>();
for (int i = 0; i < pdfFile.getNumPages(); i++)
PDFPage page = pdfFile.getPage(i, true);
System.out.println("page: " + i + " - " + pdfFile.getNumPages());
java.awt.geom.Rectangle2D bbox = page.getBBox();
java.awt.geom.Rectangle2D rect = new Rectangle(0, 0, (int) bbox.getWidth(), (int) bbox.getHeight());
BufferedImage buffImage = new BufferedImage((int) (bbox.getWidth() * 2d),
(int) (bbox.getHeight() * 2d), BufferedImage.TYPE_INT_RGB);
java.awt.Image awtImage = page.getImage((int) (bbox.getWidth() * 2.0),
(int) (bbox.getHeight() * 2.0), rect, null, true, true);
java.awt.Graphics2D bufImageGraphics = buffImage.createGraphics();
bufImageGraphics.drawImage(awtImage, 0, 0, null);

Image image = SwingFXUtils.toFXImage(buffImage, null);
pages.add(image);

pdfFilePages.addAll(pages);





This is what I get in my console:



page: 0 - 6
page: 1 - 6
page: 2 - 6
page: 3 - 6
page: 4 - 6
page: 5 - 6


When I load the pdf file to display the content in my application i get "hello world 1 - 1" for the first and second page. The following pages have "hello world 2 - 2" to "hello world 5 - 5". I don't understand why I get two pages of "hello world 1 - 1". I hope someone can explain to me where I made a mistake.










share|improve this question



















  • 2





    Which library do you use for displaying the PDF? PDFFile does not appear to be a PDFBox class. My guess: PDFFile.getPage wants a 1-based page number and in case of a 0 defaults to page 1.

    – mkl
    Nov 12 '18 at 17:39











  • in my loadfile method I turn the pdf file to images and store them in my observablelist (pdfFilesPages is of the type observablelist<image>) of images. To display a page, I pick the desired page and create an image view object which I can add to a pane.

    – Yupp
    Nov 13 '18 at 6:38












  • That's not what I asked. I asked "Which library do you use for displaying the PDF?"

    – mkl
    Nov 13 '18 at 6:55











  • sorry. com.sun.pdfview is the library I use to display the pdf

    – Yupp
    Nov 13 '18 at 7:06






  • 1





    you were right. The solution is to start the for loop at 1 and to go through it until the index is equal to pdfFile.getNumPages(). getPage() jumps to the first page when the integer is 0. Therefore I get the first page twice. thank you very much :)

    – Yupp
    Nov 13 '18 at 7:48















3















I'm looking for at least an hour at my code but I can't find the bug. I'm using PDFBox to create PDF's (PDFBox HelloWorld Example). To learn how PDFBox works I just wanted to create some pages with "hello world" and the page number like "hello world 1", "hello world 2" and so on. As you can see I created a for loop to create six pages.



private void drawPDF(PDDocument doc, File file) throws IOException 
for (int pageIndex = 0; pageIndex < 6; pageIndex++)
PDPage page = new PDPage(PDRectangle.A4);
doc.addPage(page);
PDFont font = PDType1Font.HELVETICA_BOLD;
String message = "hello world " + (pageIndex + 1);
float stringHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() * FONT_SIZE;
PDRectangle pageSize = page.getMediaBox();
try (PDPageContentStream contents = new PDPageContentStream(doc, page))
contents.beginText();
contents.setFont(font, FONT_SIZE);
contents.setTextMatrix(Matrix.getTranslateInstance(0, pageSize.getHeight() - stringHeight / 1000f));
contents.showText(message);
System.out.println(message + " - " + doc.getNumberOfPages());
contents.endText();


doc.save(file);



In my console I get the following output (first number is pageIndex, second number is doc.getNumberOfPages()):



hello world 1 - 1
hello world 2 - 2
hello world 3 - 3
hello world 4 - 4
hello world 5 - 5
hello world 6 - 6


This is my load function to view the pdf.



private final ObservableList<Image> pdfFilePages = FXCollections.observableArrayList();


private void loadFile(File file) throws FileNotFoundException, IOException
if (file != null)
ByteBuffer buffer = null;
try (RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel())
buffer = ByteBuffer.allocate((int) channel.size());
channel.read(buffer);
buffer.flip();

PDFFile pdfFile = new PDFFile(buffer);
List<Image> pages = new ArrayList<>();
for (int i = 0; i < pdfFile.getNumPages(); i++)
PDFPage page = pdfFile.getPage(i, true);
System.out.println("page: " + i + " - " + pdfFile.getNumPages());
java.awt.geom.Rectangle2D bbox = page.getBBox();
java.awt.geom.Rectangle2D rect = new Rectangle(0, 0, (int) bbox.getWidth(), (int) bbox.getHeight());
BufferedImage buffImage = new BufferedImage((int) (bbox.getWidth() * 2d),
(int) (bbox.getHeight() * 2d), BufferedImage.TYPE_INT_RGB);
java.awt.Image awtImage = page.getImage((int) (bbox.getWidth() * 2.0),
(int) (bbox.getHeight() * 2.0), rect, null, true, true);
java.awt.Graphics2D bufImageGraphics = buffImage.createGraphics();
bufImageGraphics.drawImage(awtImage, 0, 0, null);

Image image = SwingFXUtils.toFXImage(buffImage, null);
pages.add(image);

pdfFilePages.addAll(pages);





This is what I get in my console:



page: 0 - 6
page: 1 - 6
page: 2 - 6
page: 3 - 6
page: 4 - 6
page: 5 - 6


When I load the pdf file to display the content in my application i get "hello world 1 - 1" for the first and second page. The following pages have "hello world 2 - 2" to "hello world 5 - 5". I don't understand why I get two pages of "hello world 1 - 1". I hope someone can explain to me where I made a mistake.










share|improve this question



















  • 2





    Which library do you use for displaying the PDF? PDFFile does not appear to be a PDFBox class. My guess: PDFFile.getPage wants a 1-based page number and in case of a 0 defaults to page 1.

    – mkl
    Nov 12 '18 at 17:39











  • in my loadfile method I turn the pdf file to images and store them in my observablelist (pdfFilesPages is of the type observablelist<image>) of images. To display a page, I pick the desired page and create an image view object which I can add to a pane.

    – Yupp
    Nov 13 '18 at 6:38












  • That's not what I asked. I asked "Which library do you use for displaying the PDF?"

    – mkl
    Nov 13 '18 at 6:55











  • sorry. com.sun.pdfview is the library I use to display the pdf

    – Yupp
    Nov 13 '18 at 7:06






  • 1





    you were right. The solution is to start the for loop at 1 and to go through it until the index is equal to pdfFile.getNumPages(). getPage() jumps to the first page when the integer is 0. Therefore I get the first page twice. thank you very much :)

    – Yupp
    Nov 13 '18 at 7:48













3












3








3


0






I'm looking for at least an hour at my code but I can't find the bug. I'm using PDFBox to create PDF's (PDFBox HelloWorld Example). To learn how PDFBox works I just wanted to create some pages with "hello world" and the page number like "hello world 1", "hello world 2" and so on. As you can see I created a for loop to create six pages.



private void drawPDF(PDDocument doc, File file) throws IOException 
for (int pageIndex = 0; pageIndex < 6; pageIndex++)
PDPage page = new PDPage(PDRectangle.A4);
doc.addPage(page);
PDFont font = PDType1Font.HELVETICA_BOLD;
String message = "hello world " + (pageIndex + 1);
float stringHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() * FONT_SIZE;
PDRectangle pageSize = page.getMediaBox();
try (PDPageContentStream contents = new PDPageContentStream(doc, page))
contents.beginText();
contents.setFont(font, FONT_SIZE);
contents.setTextMatrix(Matrix.getTranslateInstance(0, pageSize.getHeight() - stringHeight / 1000f));
contents.showText(message);
System.out.println(message + " - " + doc.getNumberOfPages());
contents.endText();


doc.save(file);



In my console I get the following output (first number is pageIndex, second number is doc.getNumberOfPages()):



hello world 1 - 1
hello world 2 - 2
hello world 3 - 3
hello world 4 - 4
hello world 5 - 5
hello world 6 - 6


This is my load function to view the pdf.



private final ObservableList<Image> pdfFilePages = FXCollections.observableArrayList();


private void loadFile(File file) throws FileNotFoundException, IOException
if (file != null)
ByteBuffer buffer = null;
try (RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel())
buffer = ByteBuffer.allocate((int) channel.size());
channel.read(buffer);
buffer.flip();

PDFFile pdfFile = new PDFFile(buffer);
List<Image> pages = new ArrayList<>();
for (int i = 0; i < pdfFile.getNumPages(); i++)
PDFPage page = pdfFile.getPage(i, true);
System.out.println("page: " + i + " - " + pdfFile.getNumPages());
java.awt.geom.Rectangle2D bbox = page.getBBox();
java.awt.geom.Rectangle2D rect = new Rectangle(0, 0, (int) bbox.getWidth(), (int) bbox.getHeight());
BufferedImage buffImage = new BufferedImage((int) (bbox.getWidth() * 2d),
(int) (bbox.getHeight() * 2d), BufferedImage.TYPE_INT_RGB);
java.awt.Image awtImage = page.getImage((int) (bbox.getWidth() * 2.0),
(int) (bbox.getHeight() * 2.0), rect, null, true, true);
java.awt.Graphics2D bufImageGraphics = buffImage.createGraphics();
bufImageGraphics.drawImage(awtImage, 0, 0, null);

Image image = SwingFXUtils.toFXImage(buffImage, null);
pages.add(image);

pdfFilePages.addAll(pages);





This is what I get in my console:



page: 0 - 6
page: 1 - 6
page: 2 - 6
page: 3 - 6
page: 4 - 6
page: 5 - 6


When I load the pdf file to display the content in my application i get "hello world 1 - 1" for the first and second page. The following pages have "hello world 2 - 2" to "hello world 5 - 5". I don't understand why I get two pages of "hello world 1 - 1". I hope someone can explain to me where I made a mistake.










share|improve this question
















I'm looking for at least an hour at my code but I can't find the bug. I'm using PDFBox to create PDF's (PDFBox HelloWorld Example). To learn how PDFBox works I just wanted to create some pages with "hello world" and the page number like "hello world 1", "hello world 2" and so on. As you can see I created a for loop to create six pages.



private void drawPDF(PDDocument doc, File file) throws IOException 
for (int pageIndex = 0; pageIndex < 6; pageIndex++)
PDPage page = new PDPage(PDRectangle.A4);
doc.addPage(page);
PDFont font = PDType1Font.HELVETICA_BOLD;
String message = "hello world " + (pageIndex + 1);
float stringHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() * FONT_SIZE;
PDRectangle pageSize = page.getMediaBox();
try (PDPageContentStream contents = new PDPageContentStream(doc, page))
contents.beginText();
contents.setFont(font, FONT_SIZE);
contents.setTextMatrix(Matrix.getTranslateInstance(0, pageSize.getHeight() - stringHeight / 1000f));
contents.showText(message);
System.out.println(message + " - " + doc.getNumberOfPages());
contents.endText();


doc.save(file);



In my console I get the following output (first number is pageIndex, second number is doc.getNumberOfPages()):



hello world 1 - 1
hello world 2 - 2
hello world 3 - 3
hello world 4 - 4
hello world 5 - 5
hello world 6 - 6


This is my load function to view the pdf.



private final ObservableList<Image> pdfFilePages = FXCollections.observableArrayList();


private void loadFile(File file) throws FileNotFoundException, IOException
if (file != null)
ByteBuffer buffer = null;
try (RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel())
buffer = ByteBuffer.allocate((int) channel.size());
channel.read(buffer);
buffer.flip();

PDFFile pdfFile = new PDFFile(buffer);
List<Image> pages = new ArrayList<>();
for (int i = 0; i < pdfFile.getNumPages(); i++)
PDFPage page = pdfFile.getPage(i, true);
System.out.println("page: " + i + " - " + pdfFile.getNumPages());
java.awt.geom.Rectangle2D bbox = page.getBBox();
java.awt.geom.Rectangle2D rect = new Rectangle(0, 0, (int) bbox.getWidth(), (int) bbox.getHeight());
BufferedImage buffImage = new BufferedImage((int) (bbox.getWidth() * 2d),
(int) (bbox.getHeight() * 2d), BufferedImage.TYPE_INT_RGB);
java.awt.Image awtImage = page.getImage((int) (bbox.getWidth() * 2.0),
(int) (bbox.getHeight() * 2.0), rect, null, true, true);
java.awt.Graphics2D bufImageGraphics = buffImage.createGraphics();
bufImageGraphics.drawImage(awtImage, 0, 0, null);

Image image = SwingFXUtils.toFXImage(buffImage, null);
pages.add(image);

pdfFilePages.addAll(pages);





This is what I get in my console:



page: 0 - 6
page: 1 - 6
page: 2 - 6
page: 3 - 6
page: 4 - 6
page: 5 - 6


When I load the pdf file to display the content in my application i get "hello world 1 - 1" for the first and second page. The following pages have "hello world 2 - 2" to "hello world 5 - 5". I don't understand why I get two pages of "hello world 1 - 1". I hope someone can explain to me where I made a mistake.







java javafx pdfbox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 6:39







Yupp

















asked Nov 12 '18 at 14:56









YuppYupp

1239




1239







  • 2





    Which library do you use for displaying the PDF? PDFFile does not appear to be a PDFBox class. My guess: PDFFile.getPage wants a 1-based page number and in case of a 0 defaults to page 1.

    – mkl
    Nov 12 '18 at 17:39











  • in my loadfile method I turn the pdf file to images and store them in my observablelist (pdfFilesPages is of the type observablelist<image>) of images. To display a page, I pick the desired page and create an image view object which I can add to a pane.

    – Yupp
    Nov 13 '18 at 6:38












  • That's not what I asked. I asked "Which library do you use for displaying the PDF?"

    – mkl
    Nov 13 '18 at 6:55











  • sorry. com.sun.pdfview is the library I use to display the pdf

    – Yupp
    Nov 13 '18 at 7:06






  • 1





    you were right. The solution is to start the for loop at 1 and to go through it until the index is equal to pdfFile.getNumPages(). getPage() jumps to the first page when the integer is 0. Therefore I get the first page twice. thank you very much :)

    – Yupp
    Nov 13 '18 at 7:48












  • 2





    Which library do you use for displaying the PDF? PDFFile does not appear to be a PDFBox class. My guess: PDFFile.getPage wants a 1-based page number and in case of a 0 defaults to page 1.

    – mkl
    Nov 12 '18 at 17:39











  • in my loadfile method I turn the pdf file to images and store them in my observablelist (pdfFilesPages is of the type observablelist<image>) of images. To display a page, I pick the desired page and create an image view object which I can add to a pane.

    – Yupp
    Nov 13 '18 at 6:38












  • That's not what I asked. I asked "Which library do you use for displaying the PDF?"

    – mkl
    Nov 13 '18 at 6:55











  • sorry. com.sun.pdfview is the library I use to display the pdf

    – Yupp
    Nov 13 '18 at 7:06






  • 1





    you were right. The solution is to start the for loop at 1 and to go through it until the index is equal to pdfFile.getNumPages(). getPage() jumps to the first page when the integer is 0. Therefore I get the first page twice. thank you very much :)

    – Yupp
    Nov 13 '18 at 7:48







2




2





Which library do you use for displaying the PDF? PDFFile does not appear to be a PDFBox class. My guess: PDFFile.getPage wants a 1-based page number and in case of a 0 defaults to page 1.

– mkl
Nov 12 '18 at 17:39





Which library do you use for displaying the PDF? PDFFile does not appear to be a PDFBox class. My guess: PDFFile.getPage wants a 1-based page number and in case of a 0 defaults to page 1.

– mkl
Nov 12 '18 at 17:39













in my loadfile method I turn the pdf file to images and store them in my observablelist (pdfFilesPages is of the type observablelist<image>) of images. To display a page, I pick the desired page and create an image view object which I can add to a pane.

– Yupp
Nov 13 '18 at 6:38






in my loadfile method I turn the pdf file to images and store them in my observablelist (pdfFilesPages is of the type observablelist<image>) of images. To display a page, I pick the desired page and create an image view object which I can add to a pane.

– Yupp
Nov 13 '18 at 6:38














That's not what I asked. I asked "Which library do you use for displaying the PDF?"

– mkl
Nov 13 '18 at 6:55





That's not what I asked. I asked "Which library do you use for displaying the PDF?"

– mkl
Nov 13 '18 at 6:55













sorry. com.sun.pdfview is the library I use to display the pdf

– Yupp
Nov 13 '18 at 7:06





sorry. com.sun.pdfview is the library I use to display the pdf

– Yupp
Nov 13 '18 at 7:06




1




1





you were right. The solution is to start the for loop at 1 and to go through it until the index is equal to pdfFile.getNumPages(). getPage() jumps to the first page when the integer is 0. Therefore I get the first page twice. thank you very much :)

– Yupp
Nov 13 '18 at 7:48





you were right. The solution is to start the for loop at 1 and to go through it until the index is equal to pdfFile.getNumPages(). getPage() jumps to the first page when the integer is 0. Therefore I get the first page twice. thank you very much :)

– Yupp
Nov 13 '18 at 7:48












1 Answer
1






active

oldest

votes


















0














With the help of mkl I found the bug in my code. The for loop has to start at 1 and run until the index is equal to pdfFile.getNumPages(). The method getPage() return the first page when the index is 0. Because the index is 1 in the second iteration, the first page will be passed twice. The last page won't be passed since the loop has been finished. This seems to be the right approach.



private void loadFile(File file) throws FileNotFoundException, IOException 
if (file != null)
ByteBuffer buffer = null;
try (RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel())
buffer = ByteBuffer.allocate((int) channel.size());
channel.read(buffer);
buffer.flip();

PDFFile pdfFile = new PDFFile(buffer);
List<Image> pages = new ArrayList<>();
for (int i = 1; i <= pdfFile.getNumPages(); i++)
PDFPage page = pdfFile.getPage(i, true);
System.out.println("page: " + i + " - " + pdfFile.getNumPages());
java.awt.geom.Rectangle2D bbox = page.getBBox();
java.awt.geom.Rectangle2D rect = new Rectangle(0, 0, (int) bbox.getWidth(), (int) bbox.getHeight());
BufferedImage buffImage = new BufferedImage((int) (bbox.getWidth() * 2d),
(int) (bbox.getHeight() * 2d), BufferedImage.TYPE_INT_RGB);
java.awt.Image awtImage = page.getImage((int) (bbox.getWidth() * 2.0),
(int) (bbox.getHeight() * 2.0), rect, null, true, true);
java.awt.Graphics2D bufImageGraphics = buffImage.createGraphics();
bufImageGraphics.drawImage(awtImage, 0, 0, null);

Image image = SwingFXUtils.toFXImage(buffImage, null);
pages.add(image);

pdfFilePages.addAll(pages);








share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264742%2fpdfbox-does-not-write-the-message-i-want-into-the-page%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    With the help of mkl I found the bug in my code. The for loop has to start at 1 and run until the index is equal to pdfFile.getNumPages(). The method getPage() return the first page when the index is 0. Because the index is 1 in the second iteration, the first page will be passed twice. The last page won't be passed since the loop has been finished. This seems to be the right approach.



    private void loadFile(File file) throws FileNotFoundException, IOException 
    if (file != null)
    ByteBuffer buffer = null;
    try (RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel())
    buffer = ByteBuffer.allocate((int) channel.size());
    channel.read(buffer);
    buffer.flip();

    PDFFile pdfFile = new PDFFile(buffer);
    List<Image> pages = new ArrayList<>();
    for (int i = 1; i <= pdfFile.getNumPages(); i++)
    PDFPage page = pdfFile.getPage(i, true);
    System.out.println("page: " + i + " - " + pdfFile.getNumPages());
    java.awt.geom.Rectangle2D bbox = page.getBBox();
    java.awt.geom.Rectangle2D rect = new Rectangle(0, 0, (int) bbox.getWidth(), (int) bbox.getHeight());
    BufferedImage buffImage = new BufferedImage((int) (bbox.getWidth() * 2d),
    (int) (bbox.getHeight() * 2d), BufferedImage.TYPE_INT_RGB);
    java.awt.Image awtImage = page.getImage((int) (bbox.getWidth() * 2.0),
    (int) (bbox.getHeight() * 2.0), rect, null, true, true);
    java.awt.Graphics2D bufImageGraphics = buffImage.createGraphics();
    bufImageGraphics.drawImage(awtImage, 0, 0, null);

    Image image = SwingFXUtils.toFXImage(buffImage, null);
    pages.add(image);

    pdfFilePages.addAll(pages);








    share|improve this answer



























      0














      With the help of mkl I found the bug in my code. The for loop has to start at 1 and run until the index is equal to pdfFile.getNumPages(). The method getPage() return the first page when the index is 0. Because the index is 1 in the second iteration, the first page will be passed twice. The last page won't be passed since the loop has been finished. This seems to be the right approach.



      private void loadFile(File file) throws FileNotFoundException, IOException 
      if (file != null)
      ByteBuffer buffer = null;
      try (RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel())
      buffer = ByteBuffer.allocate((int) channel.size());
      channel.read(buffer);
      buffer.flip();

      PDFFile pdfFile = new PDFFile(buffer);
      List<Image> pages = new ArrayList<>();
      for (int i = 1; i <= pdfFile.getNumPages(); i++)
      PDFPage page = pdfFile.getPage(i, true);
      System.out.println("page: " + i + " - " + pdfFile.getNumPages());
      java.awt.geom.Rectangle2D bbox = page.getBBox();
      java.awt.geom.Rectangle2D rect = new Rectangle(0, 0, (int) bbox.getWidth(), (int) bbox.getHeight());
      BufferedImage buffImage = new BufferedImage((int) (bbox.getWidth() * 2d),
      (int) (bbox.getHeight() * 2d), BufferedImage.TYPE_INT_RGB);
      java.awt.Image awtImage = page.getImage((int) (bbox.getWidth() * 2.0),
      (int) (bbox.getHeight() * 2.0), rect, null, true, true);
      java.awt.Graphics2D bufImageGraphics = buffImage.createGraphics();
      bufImageGraphics.drawImage(awtImage, 0, 0, null);

      Image image = SwingFXUtils.toFXImage(buffImage, null);
      pages.add(image);

      pdfFilePages.addAll(pages);








      share|improve this answer

























        0












        0








        0







        With the help of mkl I found the bug in my code. The for loop has to start at 1 and run until the index is equal to pdfFile.getNumPages(). The method getPage() return the first page when the index is 0. Because the index is 1 in the second iteration, the first page will be passed twice. The last page won't be passed since the loop has been finished. This seems to be the right approach.



        private void loadFile(File file) throws FileNotFoundException, IOException 
        if (file != null)
        ByteBuffer buffer = null;
        try (RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel())
        buffer = ByteBuffer.allocate((int) channel.size());
        channel.read(buffer);
        buffer.flip();

        PDFFile pdfFile = new PDFFile(buffer);
        List<Image> pages = new ArrayList<>();
        for (int i = 1; i <= pdfFile.getNumPages(); i++)
        PDFPage page = pdfFile.getPage(i, true);
        System.out.println("page: " + i + " - " + pdfFile.getNumPages());
        java.awt.geom.Rectangle2D bbox = page.getBBox();
        java.awt.geom.Rectangle2D rect = new Rectangle(0, 0, (int) bbox.getWidth(), (int) bbox.getHeight());
        BufferedImage buffImage = new BufferedImage((int) (bbox.getWidth() * 2d),
        (int) (bbox.getHeight() * 2d), BufferedImage.TYPE_INT_RGB);
        java.awt.Image awtImage = page.getImage((int) (bbox.getWidth() * 2.0),
        (int) (bbox.getHeight() * 2.0), rect, null, true, true);
        java.awt.Graphics2D bufImageGraphics = buffImage.createGraphics();
        bufImageGraphics.drawImage(awtImage, 0, 0, null);

        Image image = SwingFXUtils.toFXImage(buffImage, null);
        pages.add(image);

        pdfFilePages.addAll(pages);








        share|improve this answer













        With the help of mkl I found the bug in my code. The for loop has to start at 1 and run until the index is equal to pdfFile.getNumPages(). The method getPage() return the first page when the index is 0. Because the index is 1 in the second iteration, the first page will be passed twice. The last page won't be passed since the loop has been finished. This seems to be the right approach.



        private void loadFile(File file) throws FileNotFoundException, IOException 
        if (file != null)
        ByteBuffer buffer = null;
        try (RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel())
        buffer = ByteBuffer.allocate((int) channel.size());
        channel.read(buffer);
        buffer.flip();

        PDFFile pdfFile = new PDFFile(buffer);
        List<Image> pages = new ArrayList<>();
        for (int i = 1; i <= pdfFile.getNumPages(); i++)
        PDFPage page = pdfFile.getPage(i, true);
        System.out.println("page: " + i + " - " + pdfFile.getNumPages());
        java.awt.geom.Rectangle2D bbox = page.getBBox();
        java.awt.geom.Rectangle2D rect = new Rectangle(0, 0, (int) bbox.getWidth(), (int) bbox.getHeight());
        BufferedImage buffImage = new BufferedImage((int) (bbox.getWidth() * 2d),
        (int) (bbox.getHeight() * 2d), BufferedImage.TYPE_INT_RGB);
        java.awt.Image awtImage = page.getImage((int) (bbox.getWidth() * 2.0),
        (int) (bbox.getHeight() * 2.0), rect, null, true, true);
        java.awt.Graphics2D bufImageGraphics = buffImage.createGraphics();
        bufImageGraphics.drawImage(awtImage, 0, 0, null);

        Image image = SwingFXUtils.toFXImage(buffImage, null);
        pages.add(image);

        pdfFilePages.addAll(pages);









        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 7:55









        YuppYupp

        1239




        1239



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264742%2fpdfbox-does-not-write-the-message-i-want-into-the-page%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20