useStampAnnotationCreate
Returns a function that places an image on a page as a stamp annotation and resolves with its snapshot. To let users place an image by clicking the page instead, switch the interaction tool to { type: 'addImage', image } (see useSetInteractionMode).
const createStampAnnotation = useStampAnnotationCreate()
const stamp = await createStampAnnotation({ pageId, image, x, y, width?, height? })The input is a UseAddStampAnnotationProps object:
| Prop | Meaning |
|---|---|
pageId | The page to place the image on. |
image | A StagedImage returned by stageImage. |
x, y | The bottom-left corner of the stamp in user space (origin at the bottom-left corner of the page). |
width, height | Optional placement size in user-space units (the ImagePlacementSize type), resolved as described below. |
The placement size:
- With both
widthandheight, the image is stretched to exactly that size. - With one of them, the other is completed from the image's aspect ratio.
- With neither, the image takes its natural size, one image pixel per user-space unit, scaled down to fit the page when larger.
Behavior
- The resolved value is a
StampAnnotationsnapshot (typed asBlock):id,pageId,type: 'annotation',subtype: 'Stamp', the boxx,y,width, andheight(AnnotationGeometry), andimageId. The id is session-scoped: don't store it across page reloads. imageId(anImageIdstring) identifies the placed picture. Stamps loaded from the document haveimageId: null: they keep their saved appearance.- The function is asynchronous because the target page may not be loaded yet and the image has to be stored first.
- An unknown
pageIdthrowsInvalidInputError; with no document loaded, the call throwsDocumentNotLoadedError.
Example
import { stageImage, useStampAnnotationCreate } from '@sdk-pdf/react'
function useInsertLogo() {
const createStampAnnotation = useStampAnnotationCreate()
return async (pageId: string, file: File) => {
const image = await stageImage(file)
// A 120-point wide logo near the top-left corner of a US Letter page; the height
// follows the aspect ratio.
return createStampAnnotation({ pageId, image, x: 36, y: 700, width: 120 })
}
}To delete the stamp, see useBlockDelete.