PDFBox does not write the message I want into the page











up vote
3
down vote

favorite
1












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
    yesterday










  • 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
    yesterday












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










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






  • 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
    yesterday















up vote
3
down vote

favorite
1












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
    yesterday










  • 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
    yesterday












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










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






  • 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
    yesterday













up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





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 yesterday

























asked yesterday









Yupp

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
    yesterday










  • 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
    yesterday












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










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






  • 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
    yesterday














  • 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
    yesterday










  • 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
    yesterday












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










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






  • 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
    yesterday








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
yesterday




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
yesterday












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
yesterday






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
yesterday














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




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












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




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




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
yesterday




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
yesterday












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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',
    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
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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

























      up vote
      0
      down vote













      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























        up vote
        0
        down vote










        up vote
        0
        down vote









        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 yesterday









        Yupp

        1239




        1239






























             

            draft saved


            draft discarded



















































             


            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




















































































            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents