Class PDFDocument
- All Implemented Interfaces:
Drawable
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
ConstructorsConstructorDescriptionPDFDocument(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 TypeMethodDescriptionvoiddispose()Disposes of the operating system resources associated with the PDFDocument.doubleReturns the height of the current page in points.doublegetWidth()Returns the width of the current page in points.voidinternal_dispose_GC(long hDC, GCData data) Invokes platform specific functionality to dispose a GC handle.longinternal_new_GC(GCData data) Invokes platform specific functionality to allocate a new GC handle.booleanReturnstrueiff coordinates can be auto-scaled on this drawable andfalseif not.booleanReturnstrueif the PDFDocument has been disposed, andfalseotherwise.voidnewPage()Starts a new page in the PDF document.voidnewPage(double widthInPoints, double heightInPoints) Starts a new page in the PDF document with the specified dimensions.
-
Constructor Details
-
PDFDocument
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 createwidth- the width of each page in device-independent unitsheight- 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
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 PDFDocumentfilename- the path to the PDF file to createwidth- the width of each page in device-independent unitsheight- 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
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_GCin interfaceDrawable- 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
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_GCin interfaceDrawable- Parameters:
hDC- the platform specific GC handledata- the platform specific GC data- Restriction:
- This method is not intended to be referenced by clients.
-
isAutoScalable
public boolean isAutoScalable()Description copied from interface:DrawableReturnstrueiff coordinates can be auto-scaled on this drawable andfalseif not. E.g. aGCmethod 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:
isAutoScalablein interfaceDrawable- Returns:
trueif auto-scaling is enabled for this drawable- Restriction:
- This method is not intended to be referenced by clients.
-
isDisposed
public boolean isDisposed()Returnstrueif the PDFDocument has been disposed, andfalseotherwise.- Returns:
truewhen the PDFDocument is disposed andfalseotherwise
-
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.
-