pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tilman Hausherr <THaush...@t-online.de>
Subject Re: pdf viewer needed
Date Tue, 19 Jul 2016 16:29:21 GMT
Am 19.07.2016 um 14:34 schrieb Gstöttner 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


Mime
View raw message