Skip to content

背景

背景指文档页面的底层图层或底纹,用于呈现文档的基础视觉效果。添加背景可以改变文档的外观,使其更具个性化或专业性,可以用于强调品牌,保护版权,或提高文档的阅读体验。

在 PDF 文档中只能存在一个背景,对包含背景的文档页面添加背景会覆盖旧的背景。

设置颜色背景

设置颜色背景的步骤如下:

  1. 获取文档背景对象。

  2. 设置背景类型为颜色。

  3. 设置背景的属性。

  4. 将背景更新到文档上。

以下是设置颜色背景的示例代码:

java
// 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();
kotlin
// 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()
}

设置图片背景

设置图片背景的步骤如下:

  1. 获取文档背景对象。

  2. 设置背景类型为图片。

  3. 设置背景属性。

  4. 将背景更新到文档上。

以下是设置图片背景的代码:

java
// 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();
java
// 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()
}

删除背景

删除背景的步骤如下:

  1. 获取文档背景对象。

  2. 删除文档背景。

以下是删除背景的代码:

java
CPDFBackground background = document.getBackground();
//remove all pages background
background.clear();
kotlin
document.background.clear()