PDFBox does not write the message I want into the page
up vote
3
down vote
favorite
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
|
show 1 more comment
up vote
3
down vote
favorite
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
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
|
show 1 more comment
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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
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
java javafx pdfbox
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
|
show 1 more comment
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
|
show 1 more comment
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);
}
}
}
add a comment |
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);
}
}
}
add a comment |
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);
}
}
}
add a comment |
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);
}
}
}
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);
}
}
}
answered yesterday
Yupp
1239
1239
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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