Background
The background refers to the underlying layer or pattern on the document pages, used to present the fundamental visual effect of the document. Adding a background can alter the document's appearance, making it more personalized or professional. It can be used to emphasize a brand, protect copyright, or enhance the reading experience of the document.
In a PDF document, only one background can exist, and adding a new background to pages containing an existing background will overwrite the old background.
Set Color Background
The steps to set a color background are as follows:
- Obtain the document's background object.
- Set the background type to color.
- Configure the properties of the background.
- Update the background of the document.
This example shows how to set the color background:
// Open document from file path.
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath, password);
CPDFBackground background = document.getBackground();
background.setType(CPDFBackground.Type.Color);
background.setColor(Color.BLACK);// Background color (image background does not work).
background.setOpacity(1.0F);// Background transparency, the range of 0~1, with the default of 1.
background.setScale(1.0F);// Background tiling scale.
background.setRotation(0);// Background rotation angle, the range of 0~360, the default is 0 (rotate at the centre of the page).
background.setHorizalign(CPDFBackground.CPDFBackgroundAlign.Left);
background.setVertalign(CPDFBackground.CPDFBackgroundAlign.Bottom);
background.setXOffset(0);// The horizontal offset of the background. Positive numbers are shifted to the right, negative numbers are shifted to the left.
background.setYOffset(0);// The vertical offset of the background. Positive numbers are shifted downwards, negative numbers are shifted upwards.
background.setPages("0,1,2");// Set the page range of the background by string, such as "0,3 or 5-7".
background.update();
background.release();
// Open document from file path.
val document = CPDFDocument(context)
document.open(pdfPath, password)
val background = document.background
background.apply {
type = CPDFBackground.Type.Color
setImage(bitmap, CPDFImageScaleType.SCALETYPE_fitCenter) // Set images.
opacity = 1.0F // Background transparency, the range of 0~1, with the default of 1.
scale = 1.0F // Background tiling scale.
rotation = 0F // Background rotation angle, the range of 0~360, the default is 0 (rotate at the centre of the page).
horizalign = CPDFBackground.CPDFBackgroundAlign.Left
vertalign = CPDFBackground.CPDFBackgroundAlign.Bottom
xOffset = 0F // The horizontal offset of the background. Positive numbers are shifted to the right, negative numbers are shifted to the left.
yOffset = 0F // The vertical offset of the background. Positive numbers are shifted downwards, negative numbers are shifted upwards.
pages = "0,1,2" // Set the page range of the background by string, such as "0,3 or 5-7".
update()
release()
}
Set Image Background
The steps to set an image background are as follows:
- Obtain the document background object.
- Set the background type to an image.
- Specify the background properties.
- Update the background on the document.
This example shows how to set the image background:
// Open document from file path.
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath, password);
CPDFBackground background = document.getBackground();
background.setType(CPDFBackground.Type.Image);
background.setImage(bitmap, CPDFImageScaleType.SCALETYPE_fitCenter);// Set images.
background.setOpacity(1.0F);// Background transparency, the range of 0~1, with the default of 1.
background.setScale(1.0F);// Background tiling scale.
background.setRotation(0);// Background rotation angle, the range of 0~360, the default is 0 (rotate at the centre of the page).
background.setHorizalign(CPDFBackground.CPDFBackgroundAlign.Left);
background.setVertalign(CPDFBackground.CPDFBackgroundAlign.Bottom);
background.setXOffset(0);// The horizontal offset of the background. Positive numbers are shifted to the right, negative numbers are shifted to the left.
background.setYOffset(0);// The vertical offset of the background. Positive numbers are shifted downwards, negative numbers are shifted upwards.
background.setPages("0,1,2");// Set the page range of the background by string, such as "0,3 or 5-7".
background.update();
background.release();
// Open document from file path.
val document = CPDFDocument(context)
document.open(pdfPath, password)
val background = document.background
background.apply {
type = CPDFBackground.Type.Image
setImage(bitmap, CPDFImageScaleType.SCALETYPE_fitCenter) // Set images.
opacity = 1.0F // Background transparency, the range of 0~1, with the default of 1.
scale = 1.0F // Background tiling scale.
rotation = 0F // Background rotation angle, the range of 0~360, the default is 0 (rotate at the centre of the page).
horizalign = CPDFBackground.CPDFBackgroundAlign.Left // Horizontal alignment of the background.
vertalign = CPDFBackground.CPDFBackgroundAlign.Bottom // Background vertical alignment.
xOffset = 0F // The horizontal offset of the background. Positive numbers are shifted to the right, negative numbers are shifted to the left.
yOffset = 0F // The vertical offset of the background. Positive numbers are shifted downwards, negative numbers are shifted upwards.
pages = "0,1,2" // Set the page range of the background by string, such as "0,3 or 5-7".
update()
release()
}
Remove Background
The steps to remove the background are as follows:
- Obtain the document background object.
- Delete the document background.
This example shows how to remove background:
CPDFBackground background = document.getBackground();
//remove all pages background
background.clear();
document.background.clear()