You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Gstöttner Siegfried <si...@sbg.ac.at> on 2016/07/19 12:34:35 UTC

pdf viewer needed

Hi!

I am pretty new to java and currently working on an application for printing serial letters in JavaFX. I would like to give the user an overview of the result in a simple pdf-viewer. Like that one shown in an example prior to ver2.0. Could anybody tell me how to implement this with the actual libry? Examples missing or i could not find them. Are there any experiences with usage of Java FX8 Swingnode class to display such a viewer? Thank you very much.

Kind regards,
Siegfried
-
Siegfried Gstöttner
siegfried.gstoettner@sbg.ac.at


Re: pdf viewer needed

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 19.07.2016 um 14:34 schrieb Gstttner Siegfried:
> Hi!
>
> I am pretty new to java and currently working on an application for printing serial letters in JavaFX. I would like to give the user an overview of the result in a simple pdf-viewer. Like that one shown in an example prior to ver2.0. Could anybody tell me how to implement this with the actual libry? Examples missing or i could not find them. Are there any experiences with usage of Java FX8 Swingnode class to display such a viewer? Thank you very much.

You could look at PDFDebugger source code. Alternatively, here's some 
code modified from the code in 
https://issues.apache.org/jira/browse/PDFBOX-3359 . However this does 
not contain paging, and it isn't with java FX.


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.rendering.PDFRenderer;

public class PDFBox3359PanelTestEnhanced
{

     private static JPanel getTestPanel()
     {
         final PDDocument doc;
         try
         {
             doc = PDDocument.load(new File("....", "XXX.pdf"));
         }
         catch (IOException e)
         {
             e.printStackTrace();
             return null;
         }
         final PDFRenderer renderer = new PDFRenderer(doc);
         JPanel panel = new JPanel()
         {
             int i;

             @Override
             protected void paintComponent(Graphics g)
             {
                 try
                 {
                     g.setColor(Color.red);
                     g.fillRect(0, 0, getWidth(), getHeight());
                     PDPage page = doc.getPage(0);
                     PDRectangle cropBox = page.getCropBox();
                     boolean rot = false;
                     if (page.getRotation() == 90 || page.getRotation() 
== 270)
                     {
                         rot = true;
                     }

                     // 
https://stackoverflow.com/questions/1106339/resize-image-to-fit-in-bounding-box
                     float imgWidth = rot ? cropBox.getHeight() : 
cropBox.getWidth();
                     float imgHeight = rot ? cropBox.getWidth() : 
cropBox.getHeight();
                     float xf = getWidth() / imgWidth;
                     float yf = getHeight() / imgHeight;
                     float scale = Math.min(xf, yf);
                     if (yf < xf)
                     {
                         g.translate((int) ((getWidth() - imgWidth * yf) 
/ 2), 0);
                     }
                     else
                     {
                         g.translate(0, (int) ((getHeight() - imgHeight 
* xf) / 2));
                     }
                     renderer.renderPageToGraphics(0, (Graphics2D) g, 
scale);
                 }
                 catch (IOException e)
                 {
                     e.printStackTrace();
                 }
             }
         };
         return panel;
     }

     public static void main(String[] args) throws Exception
     {
         SwingUtilities.invokeLater(new Runnable()
         {
             @Override
             public void run()
             {
                 JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                 frame.add(getTestPanel());
                 frame.pack();
                 frame.setSize(600, 400);
                 Dimension paneSize = frame.getSize();
                 Dimension screenSize = frame.getToolkit().getScreenSize();
                 frame.setLocation((screenSize.width - paneSize.width) / 
2, (screenSize.height - paneSize.height) / 2);
                 frame.setTitle("Test");
                 frame.setVisible(true);
             }
         });
     }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


AW: pdf viewer needed

Posted by Gstöttner Siegfried <si...@sbg.ac.at>.
Hi John!

Thank you very much. I'lL check it out. Will take some time.

Kind regards,
Siegfried
-
Siegfried Gstöttner
siegfried.gstoettner@sbg.ac.at

-----Ursprüngliche Nachricht-----
Von: John Hewson [mailto:john@jahewson.com] 
Gesendet: Donnerstag, 21. Juli 2016 20:50
An: users@pdfbox.apache.org
Betreff: Re: pdf viewer needed


> On 21 Jul 2016, at 03:49, Gstöttner Siegfried <si...@sbg.ac.at> wrote:
> 
> Hi John!
> 
> Thank you for your quick reply. Here is my code for having multiple labels inside a VBox, which is located in a ScrollPane - with one problem inside the loop...
> 
>    @FXML
>    private Button button;
>    @FXML
>    private Label label;
>    @FXML
>    private VBox scrollBox;
> //    @FXML
> //    private ScrollPane scrollPane;
> 
>        BufferedImage bufImage = null;
>        WritableImage image = null;
>        Label labelGraphics = new Label();
>        Label labelGraphics2 = new Label();
> 
>            PDDocument doc = PDDocument.load(file);
>            int countPages = doc.getNumberOfPages();
>            PDFRenderer renderer = new PDFRenderer(doc);
> 
>            // approach two or more pages per document - for testing only
>            bufImage = renderer.renderImage(0, scale);
>            image = SwingFXUtils.toFXImage(bufImage, null);
>            scrollBox.getChildren().add(labelGraphics);
>            labelGraphics.setGraphic(new ImageView(image));
> 
>            bufImage = renderer.renderImage(1, scale);
>            image = SwingFXUtils.toFXImage(bufImage, null);
>            scrollBox.getChildren().add(labelGraphics2);
>            labelGraphics2.setGraphic(new ImageView(image));
> 
>            // one label per pdf-page
>            for (int i = 0; i < countPages; i++) {
>                bufImage = renderer.renderImage(i, scale);
>                image = SwingFXUtils.toFXImage(bufImage, null);
>                scrollBox.getChildren().add(new Label("labGraph" + i));
>                // how to access Label named "labGraph" + i
>                // to do next acion setGraphic()?
> 
>            }
> 
>            doc.close();
> 
> The static approach for testing works. But how can i access the dynamically created Label to proceed ahed with "'labGraph + i'.setGraphic(new ImageView(image));”?

So you’re creating a new Label which contains the text “labGraph0”.You don’t want that. What you want to do is actually:

Label pageLabel = new Label(“”);
pageLabel.setGraphic(new ImageView(image)); scrollBox.getChildren().add(pageLabel);

> Next problem: graphics are rendered at different sizes according to the content of the document. How could i get the renderer to compute an image from any document  that fits by width in my VBox?

What you want to do is calculate the effective DPI which you want to render at and call renderImageWithDPI. You can calculate this using the width of the PDPage (you want the cropbox) from PDFBox, which is in pt and can be converted to px by multiplying by page dpi / screen dpi, which is usually 72/96 = 0.75. Reverse that calculation to go from px back to pt.

— John

> Thank's.
> Thank's to Tilman for quickly replying too.
> 
> Kind regards,
> Siegfried
> -
> Siegfried Gstöttner
> siegfried.gstoettner@sbg.ac.at
> 
> -----Ursprüngliche Nachricht-----
> Von: John Hewson [mailto:john@jahewson.com]
> Gesendet: Dienstag, 19. Juli 2016 19:12
> An: users@pdfbox.apache.org
> Betreff: Re: pdf viewer needed
> 
> 
>> On 19 Jul 2016, at 05:34, Gstöttner Siegfried <si...@sbg.ac.at> wrote:
>> 
>> Hi!
>> 
>> I am pretty new to java and currently working on an application for printing serial letters in JavaFX. I would like to give the user an overview of the result in a simple pdf-viewer. Like that one shown in an example prior to ver2.0. Could anybody tell me how to implement this with the actual libry? Examples missing or i could not find them. Are there any experiences with usage of Java FX8 Swingnode class to display such a viewer? Thank you very much.
> 
> Assuming you already have a PDDocument, and a Label named “pageView”, you can render individual pages into the Label as follows:
> 
> PDFRenderer renderer = new PDFRenderer(doc); BufferedImage bufImage = 
> renderer.renderImage(0); Image image = 
> SwingFXUtils.toFXImage(bufImage, null); pageView.setGraphic(new 
> ImageView(image));
> 
> I’d recommend taking this approach and letting JavaFX handle any scrolling of the Label. You can then have multiple labels for multiple pages, or a single label and next/prev page buttons.
> 
> — John
> 
>> Kind regards,
>> Siegfried
>> -
>> Siegfried Gstöttner
>> siegfried.gstoettner@sbg.ac.at
>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: pdf viewer needed

Posted by John Hewson <jo...@jahewson.com>.
> On 21 Jul 2016, at 03:49, Gstöttner Siegfried <si...@sbg.ac.at> wrote:
> 
> Hi John!
> 
> Thank you for your quick reply. Here is my code for having multiple labels inside a VBox, which is located in a ScrollPane - with one problem inside the loop...
> 
>    @FXML
>    private Button button;
>    @FXML
>    private Label label;
>    @FXML
>    private VBox scrollBox;
> //    @FXML
> //    private ScrollPane scrollPane;
> 
>        BufferedImage bufImage = null;
>        WritableImage image = null;
>        Label labelGraphics = new Label();
>        Label labelGraphics2 = new Label();
> 
>            PDDocument doc = PDDocument.load(file);
>            int countPages = doc.getNumberOfPages();
>            PDFRenderer renderer = new PDFRenderer(doc);
> 
>            // approach two or more pages per document - for testing only
>            bufImage = renderer.renderImage(0, scale);
>            image = SwingFXUtils.toFXImage(bufImage, null);
>            scrollBox.getChildren().add(labelGraphics);
>            labelGraphics.setGraphic(new ImageView(image));
> 
>            bufImage = renderer.renderImage(1, scale);
>            image = SwingFXUtils.toFXImage(bufImage, null);
>            scrollBox.getChildren().add(labelGraphics2);
>            labelGraphics2.setGraphic(new ImageView(image));
> 
>            // one label per pdf-page
>            for (int i = 0; i < countPages; i++) {
>                bufImage = renderer.renderImage(i, scale);
>                image = SwingFXUtils.toFXImage(bufImage, null);
>                scrollBox.getChildren().add(new Label("labGraph" + i));
>                // how to access Label named "labGraph" + i
>                // to do next acion setGraphic()?
> 
>            }
> 
>            doc.close();
> 
> The static approach for testing works. But how can i access the dynamically created Label to proceed ahed with "'labGraph + i'.setGraphic(new ImageView(image));”?

So you’re creating a new Label which contains the text “labGraph0”.You don’t want that. What you want to do is actually:

Label pageLabel = new Label(“”);
pageLabel.setGraphic(new ImageView(image));
scrollBox.getChildren().add(pageLabel);

> Next problem: graphics are rendered at different sizes according to the content of the document. How could i get the renderer to compute an image from any document  that fits by width in my VBox?

What you want to do is calculate the effective DPI which you want to render at and call renderImageWithDPI. You can calculate this using the width of the PDPage (you want the cropbox) from PDFBox, which is in pt and can be converted to px by multiplying by page dpi / screen dpi, which is usually 72/96 = 0.75. Reverse that calculation to go from px back to pt.

— John

> Thank's.
> Thank's to Tilman for quickly replying too.
> 
> Kind regards,
> Siegfried
> -
> Siegfried Gstöttner
> siegfried.gstoettner@sbg.ac.at
> 
> -----Ursprüngliche Nachricht-----
> Von: John Hewson [mailto:john@jahewson.com] 
> Gesendet: Dienstag, 19. Juli 2016 19:12
> An: users@pdfbox.apache.org
> Betreff: Re: pdf viewer needed
> 
> 
>> On 19 Jul 2016, at 05:34, Gstöttner Siegfried <si...@sbg.ac.at> wrote:
>> 
>> Hi!
>> 
>> I am pretty new to java and currently working on an application for printing serial letters in JavaFX. I would like to give the user an overview of the result in a simple pdf-viewer. Like that one shown in an example prior to ver2.0. Could anybody tell me how to implement this with the actual libry? Examples missing or i could not find them. Are there any experiences with usage of Java FX8 Swingnode class to display such a viewer? Thank you very much.
> 
> Assuming you already have a PDDocument, and a Label named “pageView”, you can render individual pages into the Label as follows:
> 
> PDFRenderer renderer = new PDFRenderer(doc); BufferedImage bufImage = renderer.renderImage(0); Image image = SwingFXUtils.toFXImage(bufImage, null); pageView.setGraphic(new ImageView(image));
> 
> I’d recommend taking this approach and letting JavaFX handle any scrolling of the Label. You can then have multiple labels for multiple pages, or a single label and next/prev page buttons.
> 
> — John
> 
>> Kind regards,
>> Siegfried
>> -
>> Siegfried Gstöttner
>> siegfried.gstoettner@sbg.ac.at
>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


AW: pdf viewer needed

Posted by Gstöttner Siegfried <si...@sbg.ac.at>.
Hi John!

Thank you for your quick reply. Here is my code for having multiple labels inside a VBox, which is located in a ScrollPane - with one problem inside the loop...

    @FXML
    private Button button;
    @FXML
    private Label label;
    @FXML
    private VBox scrollBox;
//    @FXML
//    private ScrollPane scrollPane;

        BufferedImage bufImage = null;
        WritableImage image = null;
        Label labelGraphics = new Label();
        Label labelGraphics2 = new Label();

            PDDocument doc = PDDocument.load(file);
            int countPages = doc.getNumberOfPages();
            PDFRenderer renderer = new PDFRenderer(doc);

            // approach two or more pages per document - for testing only
            bufImage = renderer.renderImage(0, scale);
            image = SwingFXUtils.toFXImage(bufImage, null);
            scrollBox.getChildren().add(labelGraphics);
            labelGraphics.setGraphic(new ImageView(image));

            bufImage = renderer.renderImage(1, scale);
            image = SwingFXUtils.toFXImage(bufImage, null);
            scrollBox.getChildren().add(labelGraphics2);
            labelGraphics2.setGraphic(new ImageView(image));

            // one label per pdf-page
            for (int i = 0; i < countPages; i++) {
                bufImage = renderer.renderImage(i, scale);
                image = SwingFXUtils.toFXImage(bufImage, null);
                scrollBox.getChildren().add(new Label("labGraph" + i));
                // how to access Label named "labGraph" + i
                // to do next acion setGraphic()?
                
            }

            doc.close();

The static approach for testing works. But how can i access the dynamically created Label to proceed ahed with "'labGraph + i'.setGraphic(new ImageView(image));"?

Next problem: graphics are rendered at different sizes according to the content of the document. How could i get the renderer to compute an image from any document  that fits by width in my VBox?

Thank's.
Thank's to Tilman for quickly replying too.

Kind regards,
Siegfried
-
Siegfried Gstöttner
siegfried.gstoettner@sbg.ac.at

-----Ursprüngliche Nachricht-----
Von: John Hewson [mailto:john@jahewson.com] 
Gesendet: Dienstag, 19. Juli 2016 19:12
An: users@pdfbox.apache.org
Betreff: Re: pdf viewer needed


> On 19 Jul 2016, at 05:34, Gstöttner Siegfried <si...@sbg.ac.at> wrote:
> 
> Hi!
> 
> I am pretty new to java and currently working on an application for printing serial letters in JavaFX. I would like to give the user an overview of the result in a simple pdf-viewer. Like that one shown in an example prior to ver2.0. Could anybody tell me how to implement this with the actual libry? Examples missing or i could not find them. Are there any experiences with usage of Java FX8 Swingnode class to display such a viewer? Thank you very much.

Assuming you already have a PDDocument, and a Label named “pageView”, you can render individual pages into the Label as follows:

PDFRenderer renderer = new PDFRenderer(doc); BufferedImage bufImage = renderer.renderImage(0); Image image = SwingFXUtils.toFXImage(bufImage, null); pageView.setGraphic(new ImageView(image));

I’d recommend taking this approach and letting JavaFX handle any scrolling of the Label. You can then have multiple labels for multiple pages, or a single label and next/prev page buttons.

— John

> Kind regards,
> Siegfried
> -
> Siegfried Gstöttner
> siegfried.gstoettner@sbg.ac.at
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: pdf viewer needed

Posted by John Hewson <jo...@jahewson.com>.
> On 19 Jul 2016, at 05:34, Gstöttner Siegfried <si...@sbg.ac.at> wrote:
> 
> Hi!
> 
> I am pretty new to java and currently working on an application for printing serial letters in JavaFX. I would like to give the user an overview of the result in a simple pdf-viewer. Like that one shown in an example prior to ver2.0. Could anybody tell me how to implement this with the actual libry? Examples missing or i could not find them. Are there any experiences with usage of Java FX8 Swingnode class to display such a viewer? Thank you very much.

Assuming you already have a PDDocument, and a Label named “pageView”, you can render individual pages into the Label as follows:

PDFRenderer renderer = new PDFRenderer(doc);
BufferedImage bufImage = renderer.renderImage(0);
Image image = SwingFXUtils.toFXImage(bufImage, null);
pageView.setGraphic(new ImageView(image));

I’d recommend taking this approach and letting JavaFX handle any scrolling of the Label. You can then have multiple labels for multiple pages, or a single label and next/prev page buttons.

— John

> Kind regards,
> Siegfried
> -
> Siegfried Gstöttner
> siegfried.gstoettner@sbg.ac.at
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org