Class PdfViewer

java.lang.Object
com.codename1.ui.Component
com.codename1.pdf.ui.PdfViewer
All Implemented Interfaces:
Animation, Editable, StyleListener

public class PdfViewer extends Component

Displays a PDF document: pages stacked vertically and scrolled continuously, fitted to the width of the component and zoomed by pinch or double tap.

Pages are drawn by a pure-Java renderer, so a document looks the same on every platform and needs no native viewer:

PdfDocument doc = new PdfDocument();
for (int i = 1; i <= 5; i++) {
    doc.addPage(PdfPageSize.A4).getCanvas().drawText("Page " + i, 72, 72);
}
byte[] pdfBytes = doc.toByteArray();
Form form = new Form("Document", new BorderLayout());
PdfViewer viewer = new PdfViewer(pdfBytes);
form.add(BorderLayout.CENTER, viewer);
viewer.setPage(3);

Pages render one at a time on the EDT, interleaved with other events, and only the pages near the view are kept as images. A page that is not drawn yet shows as a blank sheet.

The renderer covers vector graphics with dashes, blend modes, transparency and knockout groups and soft masks; every shading type; embedded TrueType, OpenType, CFF, Type 1 and Type 3 fonts, horizontal and vertical; images in every PDF filter, JPEG 2000, JBIG2 and CCITT fax included; and annotation appearances. Fonts that are not embedded are drawn with a platform font of the same family, so their shapes differ from the original.

  • Constructor Details

    • PdfViewer

      public PdfViewer(byte[] data) throws IOException

      A viewer for the PDF in data.

      Throws
      • IOException: when the data is not a PDF, or it is encrypted with a user password.
      Throws:
      IOException
    • PdfViewer

      public PdfViewer(byte[] data, String password) throws IOException

      A viewer for the PDF in data, opened with password (the user or the owner password) when it is encrypted.

      Throws
      • IOException: when the data is not a PDF or the password is wrong.
      Throws:
      IOException
  • Method Details

    • open

      public static PdfViewer open(InputStream in) throws IOException

      A viewer for the PDF read from in, which is closed.

      Throws
      • IOException: when reading fails or the data is not a PDF.
      Throws:
      IOException
    • getPageCount

      public int getPageCount()
      The number of pages.
    • getZoom

      public float getZoom()
      The zoom factor, 1 when a page fits the width.
    • setZoom

      public void setZoom(float z)
      Sets the zoom factor, clamped to 1..5, keeping the middle of the view in place.
    • getPage

      public int getPage()
      The page at the middle of the view, zero based.
    • setPage

      public void setPage(int index)
      Scrolls so that page index (zero based) is at the top of the view.
    • renderPage

      public Image renderPage(int index, double scale)
      Renders page index (zero based) to an image at scale pixels per point, white background, annotations included. Usable without showing the viewer, for a thumbnail or a share sheet.
    • isScrollableY

      public boolean isScrollableY()
      Description copied from class: Component

      Indicates whether the component should/could scroll on the Y axis

      Returns

      whether the component is scrollable on the X axis

      Overrides:
      isScrollableY in class Component
    • isScrollableX

      public boolean isScrollableX()
      Description copied from class: Component

      Indicates whether the component should/could scroll on the X axis

      Returns

      whether the component is scrollable on the X axis

      Overrides:
      isScrollableX in class Component
    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Description copied from class: Component

      Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

      Returns

      the calculated preferred size based on component content

      Overrides:
      calcPreferredSize in class Component
    • laidOut

      protected void laidOut()
      Description copied from class: Component
      This is a callback method to inform the Component when it's been laidout on the parent Container
      Overrides:
      laidOut in class Component
    • paint

      public void paint(Graphics g)
      Description copied from class: Component

      This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API's to let the PLAF perform the rendering.

      Parameters
      • g: the component graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class Component
    • pinch

      protected boolean pinch(float scale)
      Description copied from class: Component

      Invoked by subclasses interested in handling pinch to zoom events, if true is returned other drag events will not be broadcast

      Parameters
      • scale: @param scale the scaling of the pinch operation a number larger than 1 means scaling up and smaller than 1 means scaling down. It is recommended that code would threshold the number (so a change between 1.0 and 1.02 shouldn't necessarily trigger zoom). Notice that this number is relevant to current zoom levels and unaware of them so you should also enforce limits of maximum/minimum zoom levels.
      Returns

      false by default

      Overrides:
      pinch in class Component
    • pinchReleased

      protected void pinchReleased(int x, int y)
      Description copied from class: Component

      To be implemented by subclasses interested in being notified when a pinch zoom has ended (i.e the user has removed one of their fingers, but is still dragging).

      Parameters
      • x: The x-coordinate of the remaining finger in the drag. (Absolute)

      • y: The y-coordinate of the remaining finger in the drag. (Absolute)

      Overrides:
      pinchReleased in class Component
    • pointerReleased

      public void pointerReleased(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer released event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerReleased in class Component
    • deinitialize

      protected void deinitialize()
      Description copied from class: Component
      Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy. This allows the component to deregister animators and cleanup after itself. This method is the opposite of the initComponent() method.
      Overrides:
      deinitialize in class Component