Class PDFDocument

java.lang.Object
org.eclipse.swt.printing.PDFDocument
All Implemented Interfaces:
Drawable

public class PDFDocument extends Object implements Drawable
Instances of this class are used to create PDF documents. Applications create a GC on a PDFDocument using new GC(pdfDocument) and then draw on the GC using the usual graphics calls.

A PDFDocument object may be constructed by providing a filename and the page dimensions. After drawing is complete, the document must be disposed to finalize the PDF file.

Application code must explicitly invoke the PDFDocument.dispose() method to release the operating system resources managed by each instance when those instances are no longer required.

Note: On Windows, this class uses the built-in "Microsoft Print to PDF" printer which is available on Windows 10 and later.

The following example demonstrates how to use PDFDocument:

    PDFDocument pdf = new PDFDocument("output.pdf", 612, 792); // Letter size in points
    GC gc = new GC(pdf);
    gc.drawText("Hello, PDF!", 100, 100);
    gc.dispose();
    pdf.dispose();
 
Since:
3.133
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    PDFDocument(String filename, double width, double height)
    Constructs a new PDFDocument with the specified filename and page dimensions.
    PDFDocument(Device device, String filename, double width, double height)
    Constructs a new PDFDocument with the specified filename and page dimensions, associated with the given device.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Disposes of the operating system resources associated with the PDFDocument.
    double
    Returns the height of the current page in points.
    double
    Returns the width of the current page in points.
    void
    internal_dispose_GC(long hDC, GCData data)
    Invokes platform specific functionality to dispose a GC handle.
    long
    Invokes platform specific functionality to allocate a new GC handle.
    boolean
    Returns true iff coordinates can be auto-scaled on this drawable and false if not.
    boolean
    Returns true if the PDFDocument has been disposed, and false otherwise.
    void
    Starts a new page in the PDF document.
    void
    newPage(double widthInPoints, double heightInPoints)
    Starts a new page in the PDF document with the specified dimensions.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PDFDocument

      public PDFDocument(String filename, double width, double height)
      Constructs a new PDFDocument with the specified filename and page dimensions.

      You must dispose the PDFDocument when it is no longer required.

      Parameters:
      filename - the path to the PDF file to create
      width - the width of each page in device-independent units
      height - the height of each page in device-independent units
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if filename is null
      • ERROR_INVALID_ARGUMENT - if width or height is not positive
      SWTError -
      • ERROR_NO_HANDLES - if the PDF printer is not available
      See Also:
    • PDFDocument

      public PDFDocument(Device device, String filename, double width, double height)
      Constructs a new PDFDocument with the specified filename and page dimensions, associated with the given device.

      You must dispose the PDFDocument when it is no longer required.

      Parameters:
      device - the device to associate with this PDFDocument
      filename - the path to the PDF file to create
      width - the width of each page in device-independent units
      height - the height of each page in device-independent units
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if filename is null
      • ERROR_INVALID_ARGUMENT - if width or height is not positive
      SWTError -
      • ERROR_NO_HANDLES - if the PDF printer is not available
      See Also:
  • Method Details

    • newPage

      public void newPage()
      Starts a new page in the PDF document.

      This method should be called after completing the content of one page and before starting to draw on the next page. The new page will have the same dimensions as the initial page.

      Throws:
      SWTException -
      • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
    • newPage

      public void newPage(double widthInPoints, double heightInPoints)
      Starts a new page in the PDF document with the specified dimensions.

      This method should be called after completing the content of one page and before starting to draw on the next page.

      Note: On Windows, changing page dimensions after the document has been started may not be fully supported by all printer drivers.

      Parameters:
      widthInPoints - the width of the new page in points (1/72 inch)
      heightInPoints - the height of the new page in points (1/72 inch)
      Throws:
      IllegalArgumentException -
      • ERROR_INVALID_ARGUMENT - if width or height is not positive
      SWTException -
      • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
    • getWidth

      public double getWidth()
      Returns the width of the current page in points.
      Returns:
      the width in points (1/72 inch)
      Throws:
      SWTException -
      • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
    • getHeight

      public double getHeight()
      Returns the height of the current page in points.
      Returns:
      the height in points (1/72 inch)
      Throws:
      SWTException -
      • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
    • internal_new_GC

      public long internal_new_GC(GCData data)
      Invokes platform specific functionality to allocate a new GC handle.

      IMPORTANT: This method is not part of the public API for PDFDocument. It is marked public only so that it can be shared within the packages provided by SWT. It is not available on all platforms, and should never be called from application code.

      Specified by:
      internal_new_GC in interface Drawable
      Parameters:
      data - the platform specific GC data
      Returns:
      the platform specific GC handle
      Restriction:
      This method is not intended to be referenced by clients.
    • internal_dispose_GC

      public void internal_dispose_GC(long hDC, GCData data)
      Invokes platform specific functionality to dispose a GC handle.

      IMPORTANT: This method is not part of the public API for PDFDocument. It is marked public only so that it can be shared within the packages provided by SWT. It is not available on all platforms, and should never be called from application code.

      Specified by:
      internal_dispose_GC in interface Drawable
      Parameters:
      hDC - the platform specific GC handle
      data - the platform specific GC data
      Restriction:
      This method is not intended to be referenced by clients.
    • isAutoScalable

      public boolean isAutoScalable()
      Description copied from interface: Drawable
      Returns true iff coordinates can be auto-scaled on this drawable and false if not. E.g. a GC method should not auto-scale the bounds of a figure drawn on a Printer device, but it may have to auto-scale when drawing on a high-DPI Display monitor.
      Specified by:
      isAutoScalable in interface Drawable
      Returns:
      true if auto-scaling is enabled for this drawable
      Restriction:
      This method is not intended to be referenced by clients.
    • isDisposed

      public boolean isDisposed()
      Returns true if the PDFDocument has been disposed, and false otherwise.
      Returns:
      true when the PDFDocument is disposed and false otherwise
    • dispose

      public void dispose()
      Disposes of the operating system resources associated with the PDFDocument. Applications must dispose of all PDFDocuments that they allocate.

      This method finalizes the PDF file and writes it to disk.