import PDFDocument from 'src/api/PDFDocument' ; import PDFPage from 'src/api/PDFPage' ; import PDFFont from 'src/api/PDFFont' ; import PDFImage from 'src/api/PDFImage' ; import { ImageAlignment } from 'src/api/image/alignment' ; import { AppearanceProviderFor , normalizeAppearance , defaultButtonAppearanceProvider , } from 'src/api/form/appearances' ; import PDFField , { FieldAppearanceOptions , assertFieldAppearanceOptions , } from 'src/api/form/PDFField' ; import { rgb } from 'src/api/colors' ; import { degrees } from 'src/api/rotations' ; import { PDFRef , PDFStream , PDFAcroPushButton , PDFWidgetAnnotation , } from 'src/core' ; import { assertIs , assertOrUndefined , assertPositive } from 'src/utils' ; /**
 * Represents a button field of a [[PDFForm]].
 *
 * [[PDFButton]] fields are interactive controls that users can click with their
 * mouse. This type of [[PDFField]] is not stateful. The purpose of a button
 * is to perform an action when the user clicks on it, such as opening a print
 * modal or resetting the form. Buttons are typically rectangular in shape and
 * have a text label describing the action that they perform when clicked.
 */ export default class PDFButton extends PDFField { param acroPushButton param ref param doc static of = ( acroPushButton , ref , doc ) => new PDFButton ( acroPushButton , ref , doc ) ; /** The low-level PDFAcroPushButton wrapped by this button. */ readonly acroField : PDFAcroPushButton ; private constructor ( acroPushButton , ref , doc ) { super ( acroPushButton , ref , doc ) ; assertIs ( acroPushButton , 'acroButton' , [ [ PDFAcroPushButton , 'PDFAcroPushButton' ] , ] ) ; this . acroField = acroPushButton ; } param image param alignment setImage ( image , alignment = ImageAlignment . Center ) { const widgets = this . acroField . getWidgets ( ) ; for ( let idx = 0 , len = widgets . length ; idx < len ; idx ++ ) { const widget = widgets [ idx ] ; const streamRef = this . createImageAppearanceStream ( widget , image , alignment ) ; this . updateWidgetAppearances ( widget , { normal : streamRef } ) ; } this . markAsClean ( ) ; } param fontSize setFontSize ( fontSize ) { assertPositive ( fontSize , 'fontSize' ) ; this . acroField . setFontSize ( fontSize ) ; this . markAsDirty ( ) ; } param text param page param options addToPage ( text , page , options ? ) { assertOrUndefined ( text , 'text' , [ 'string' ] ) ; assertOrUndefined ( page , 'page' , [ [ PDFPage , 'PDFPage' ] ] ) ; assertFieldAppearanceOptions ( options ) ; const widget = this . createWidget ( { x : ( options ?. x ?? 0 ) - ( options ?. borderWidth ?? 0 ) / 2 , y : ( options ?. y ?? 0 ) - ( options ?. borderWidth ?? 0 ) / 2 , width : options ?. width ?? 100 , height : options ?. height ?? 50 , textColor : options ?. textColor ?? rgb ( 0 , 0 , 0 ) , backgroundColor : options ?. backgroundColor ?? rgb ( 0.75 , 0.75 , 0.75 ) , borderColor : options ?. borderColor , borderWidth : options ?. borderWidth ?? 0 , rotate : options ?. rotate ?? degrees ( 0 ) , caption : text , hidden : options ?. hidden , page : page . ref , } ) ; const widgetRef = this . doc . context . register ( widget . dict ) ; this . acroField . addWidget ( widgetRef ) ; const font = options ?. font ?? this . doc . getForm ( ) . getDefaultFont ( ) ; this . updateWidgetAppearance ( widget , font ) ; page . node . addAnnot ( widgetRef ) ; } returns needsAppearancesUpdate ( ) { if ( this . isDirty ( ) ) return true ; const widgets = this . acroField . getWidgets ( ) ; for ( let idx = 0 , len = widgets . length ; idx < len ; idx ++ ) { const widget = widgets [ idx ] ; const hasAppearances = widget . getAppearances ( ) ?. normal instanceof PDFStream ; if ( ! hasAppearances ) return true ; } return false ; } param font defaultUpdateAppearances ( font ) { assertIs ( font , 'font' , [ [ PDFFont , 'PDFFont' ] ] ) ; this . updateAppearances ( font ) ; } param font param provider updateAppearances ( font , provider ? ) { assertIs ( font , 'font' , [ [ PDFFont , 'PDFFont' ] ] ) ; assertOrUndefined ( provider , 'provider' , [ Function ] ) ; const widgets = this . acroField . getWidgets ( ) ; for ( let idx = 0 , len = widgets . length ; idx < len ; idx ++ ) { const widget = widgets [ idx ] ; this . updateWidgetAppearance ( widget , font , provider ) ; } } private updateWidgetAppearance ( widget , font , provider ? ) { const apProvider = provider ?? defaultButtonAppearanceProvider ; const appearances = normalizeAppearance ( apProvider ( this , widget , font ) ) ; this . updateWidgetAppearanceWithFont ( widget , font , appearances ) ; } }