Class PdfViewer
- All Implemented Interfaces:
AttachNotifier,DetachNotifier,HasElement,HasSize,HasStyle,Serializable
setSrc(String), or serve it from the application with
setSrc(DownloadHandler):
PdfViewer viewer = new PdfViewer(
DownloadHandler.forClassResource(getClass(), "report.pdf"));
viewer.setHeight("600px");
You can add your own controls to the toolbar with
getToolbarActions().
This component is experimental and needs to be enabled with the
com.vaadin.experimental.pdfViewerComponent feature flag, either in
the experimental features tab of Copilot or in
src/main/resources/vaadin-featureflags.properties:
com.vaadin.experimental.pdfViewerComponent = true
- Since:
- 25.4
- Author:
- Vaadin Ltd
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classEvent fired when a document could not be loaded.static classEvent fired when a document has been loaded, before its pages are shown.static classEvent fired when the current page changes.static final classContainer for your own controls in thePdfViewertoolbar.static classEvent fired when the toolbar is collapsed or expanded.static classEvent fired when the zoom level changes. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a PDF viewer without a document.PdfViewer(DownloadHandler downloadHandler) Creates a PDF viewer that shows the document served by the given download handler.Creates a PDF viewer that shows the document at the given URL. -
Method Summary
Modifier and TypeMethodDescriptionAdds a listener that is called when a document could not be loaded.Adds a listener that is called when a document has been loaded.Adds a listener that is called when the current page changes.addToolbarCollapsedChangeListener(ComponentEventListener<PdfViewer.ToolbarCollapsedChangeEvent> listener) Adds a listener that is called when the toolbar is collapsed or expanded, either by the user clicking the header or withsetToolbarCollapsed(boolean).Adds a listener that is called when the zoom level changes.voiddownload()Downloads the document in the browser, like the download button of the toolbar does.Gets the file name used bydownload().Gets the title set withsetHeaderTitle(String).getI18n()Gets the i18n object.intgetPage()Gets the current page, which is the page that takes up most of the visible area.intGets the number of pages in the document.getSrc()Gets the URL of the document.Gets the container for your own controls, such as buttons, in the toolbar.getZoom()Gets the zoom level of the pages.booleanGets whether the toolbar shows the download button.booleanGets whether the viewer shows the header above the toolbar.booleanGets whether the toolbar is collapsed.protected voidonAttach(AttachEvent attachEvent) Called when the component is attached to a UI.Gets a read-only signal with the number of pages in the document, or0if no document is loaded.Gets a read-only signal with the current page, which is the page that takes up most of the visible area.voidscrollToPage(int page) Scrolls to the start of the given page.voidsetDownloadButtonVisible(boolean downloadButtonVisible) Sets whether the toolbar shows the download button.voidsetFileName(String fileName) Sets the file name used bydownload()and shown in the header.voidsetHeaderTitle(String headerTitle) Sets the title shown in the header.voidsetHeaderVisible(boolean headerVisible) Sets whether the viewer shows the header above the toolbar.voidsetI18n(PdfViewerI18n i18n) Sets the i18n object that translates the texts of the viewer.voidsetSrc(DownloadHandler downloadHandler) Sets a download handler that serves the document to show, for example one created with the factory methods ofDownloadHandler.voidSets the URL of the document to show.voidsetToolbarCollapsed(boolean toolbarCollapsed) Sets whether the toolbar is collapsed.voidSets the zoom level of the pages:Zoom.PAGE_WIDTHfits the width of the current page to the viewerZoom.PAGE_FITfits the whole current page into the viewerZoom.of(double)scales the pages relative to their actual size The fit modes update the scale when the viewer resizes.Methods inherited from class com.vaadin.flow.component.Component
addListener, bindVisible, findAncestor, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTestId, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, getUIOrThrow, hasListener, isAttached, isTemplateMapped, isVisible, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setTestId, setVisible, whenAttachedMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasSize
bindHeight, bindWidth, getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFullMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, bindClassName, bindClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
-
Constructor Details
-
PdfViewer
public PdfViewer()Creates a PDF viewer without a document. -
PdfViewer
Creates a PDF viewer that shows the document at the given URL.- Parameters:
src- the URL of the document, ornullto show no document- See Also:
-
PdfViewer
Creates a PDF viewer that shows the document served by the given download handler.- Parameters:
downloadHandler- the download handler that serves the document, notnull- See Also:
-
-
Method Details
-
getSrc
Gets the URL of the document.- Returns:
- the URL of the document, or
nullif no document is set
-
setSrc
Sets the URL of the document to show. The current document is replaced, and the new one opens on the first page.PdfViewer.DocumentLoadEventfires when the document has loaded,PdfViewer.DocumentErrorEventif it could not be loaded.The browser fetches the document, so the URL must be on the same origin as the application, or the server must allow it with CORS headers. To serve a document from the application, use
setSrc(DownloadHandler).- Parameters:
src- the URL of the document, ornullto show no document
-
setSrc
Sets a download handler that serves the document to show, for example one created with the factory methods ofDownloadHandler. The framework generates a URL for the handler, whichgetSrc()returns, and the viewer loads it like a URL set withsetSrc(String).- Parameters:
downloadHandler- the download handler that serves the document, notnull
-
getFileName
Gets the file name used bydownload().- Returns:
- the file name, or
nullif the name of the document is used
-
setFileName
Sets the file name used bydownload()and shown in the header. When not set, the name of the document is used.- Parameters:
fileName- the file name, ornullto use the name of the document
-
isDownloadButtonVisible
public boolean isDownloadButtonVisible()Gets whether the toolbar shows the download button. The default istrue.- Returns:
trueif the download button is visible,falseotherwise
-
setDownloadButtonVisible
public void setDownloadButtonVisible(boolean downloadButtonVisible) Sets whether the toolbar shows the download button.download()works also while the button is hidden.- Parameters:
downloadButtonVisible-trueto show the download button,falseto hide it
-
download
public void download()Downloads the document in the browser, like the download button of the toolbar does. If the document is still loading, it is downloaded once loaded. Does nothing if no document is set, it could not be loaded, or the viewer is not attached. -
getPage
Gets the current page, which is the page that takes up most of the visible area. The default value is1.The value comes from the browser and must not be trusted for security decisions.
- Returns:
- the current page, starting from
1
-
scrollToPage
public void scrollToPage(int page) Scrolls to the start of the given page. A page below1scrolls to the first page, and a page above the page count scrolls to the last page.While a document is loading, for example right after
setSrc, it scrolls once the document has loaded, and in a hidden viewer, once the viewer is shown. Does nothing without a document.- Parameters:
page- the page to scroll to, starting from1
-
pageSignal
Gets a read-only signal with the current page, which is the page that takes up most of the visible area.The value comes from the browser and must not be trusted for security decisions.
- Returns:
- a read-only signal with the current page, starting from
1
-
getPageCount
public int getPageCount()Gets the number of pages in the document.The value comes from the browser and must not be trusted for security decisions.
- Returns:
- the number of pages, or
0if no document is loaded
-
pageCountSignal
Gets a read-only signal with the number of pages in the document, or0if no document is loaded.The value comes from the browser and must not be trusted for security decisions.
- Returns:
- a read-only signal with the number of pages
-
isHeaderVisible
public boolean isHeaderVisible()Gets whether the viewer shows the header above the toolbar. The default value istrue.- Returns:
trueif the header is visible,falseotherwise
-
setHeaderVisible
public void setHeaderVisible(boolean headerVisible) Sets whether the viewer shows the header above the toolbar. The header shows the title, seesetHeaderTitle(String), and collapses and expands the toolbar when the user clicks it.While the header is hidden, the toolbar is always shown, also when it is collapsed with
setToolbarCollapsed(boolean).- Parameters:
headerVisible-trueto show the header,falseto hide it
-
getHeaderTitle
Gets the title set withsetHeaderTitle(String).- Returns:
- the title, or
nullif no title is set
-
setHeaderTitle
Sets the title shown in the header. By default, no title is set, and the header shows the file name of the document, or the text fromPdfViewerI18n.setDocument(String)if the document has no file name.- Parameters:
headerTitle- the title, ornullor an empty string to show the file name of the document- See Also:
-
isToolbarCollapsed
@Synchronize(property="toolbarCollapsed", value="toolbar-collapsed-changed") public boolean isToolbarCollapsed()Gets whether the toolbar is collapsed. The default value isfalse.While the header is hidden, the toolbar is shown also when this is
true, seesetHeaderVisible(boolean).- Returns:
trueif the toolbar is collapsed,falseotherwise
-
setToolbarCollapsed
public void setToolbarCollapsed(boolean toolbarCollapsed) Sets whether the toolbar is collapsed. A collapsed toolbar is hidden, and the user can expand it again by clicking the header.The toolbar collapses only while the header is visible, see
setHeaderVisible(boolean).- Parameters:
toolbarCollapsed-trueto collapse the toolbar,falseto expand it
-
getToolbarActions
Gets the container for your own controls, such as buttons, in the toolbar. The controls are placed after the built-in page and zoom controls, before the download button. Empty by default.- Returns:
- the toolbar actions container
-
getI18n
Gets the i18n object.- Returns:
- the i18n object, or
nullif not set
-
setI18n
Sets the i18n object that translates the texts of the viewer. Changes to the object take effect only when you set it again.- Parameters:
i18n- the i18n object, notnull
-
getZoom
Gets the zoom level of the pages. The default value isZoom.PAGE_WIDTH.- Returns:
- the zoom level, not
null
-
setZoom
Sets the zoom level of the pages:Zoom.PAGE_WIDTHfits the width of the current page to the viewerZoom.PAGE_FITfits the whole current page into the viewerZoom.of(double)scales the pages relative to their actual size
The zoom stays when another document is set.
- Parameters:
zoom- the zoom level, notnull
-
addPageChangeListener
public Registration addPageChangeListener(ComponentEventListener<PdfViewer.PageChangeEvent> listener) Adds a listener that is called when the current page changes.- Parameters:
listener- the listener to add, notnull- Returns:
- a registration for removing the listener
-
addZoomChangeListener
public Registration addZoomChangeListener(ComponentEventListener<PdfViewer.ZoomChangeEvent> listener) Adds a listener that is called when the zoom level changes.- Parameters:
listener- the listener to add, notnull- Returns:
- a registration for removing the listener
-
addToolbarCollapsedChangeListener
public Registration addToolbarCollapsedChangeListener(ComponentEventListener<PdfViewer.ToolbarCollapsedChangeEvent> listener) Adds a listener that is called when the toolbar is collapsed or expanded, either by the user clicking the header or withsetToolbarCollapsed(boolean).- Parameters:
listener- the listener to add, notnull- Returns:
- a registration for removing the listener
-
addDocumentLoadListener
public Registration addDocumentLoadListener(ComponentEventListener<PdfViewer.DocumentLoadEvent> listener) Adds a listener that is called when a document has been loaded.- Parameters:
listener- the listener to add, notnull- Returns:
- a registration for removing the listener
-
addDocumentErrorListener
public Registration addDocumentErrorListener(ComponentEventListener<PdfViewer.DocumentErrorEvent> listener) Adds a listener that is called when a document could not be loaded.- Parameters:
listener- the listener to add, notnull- Returns:
- a registration for removing the listener
-
onAttach
Description copied from class:ComponentCalled when the component is attached to a UI.This method is invoked before the
AttachEventis fired for the component. Make sure to callsuper.onAttachwhen overriding this method.
-