Spire.PDF for .NET【页面设置】演示:为 PDF 添加背景颜色或背景图像

在 PDF 文档中,背景是指页面内容背后的整体视觉外观。背景可以是简单的纯色,也可以是您选择的图像。向 PDF 添加背景可以帮助您增加文档的视觉趣味,并提高可读性。在本文中,您将学习如何使用Spire.PDF for .NET以编程方式设置 PDF 的背景颜色或图像。

Spire.PDF for .NET 是一款独立 PDF 控件,用于 .NET 程序中创建、编辑和操作 PDF 文档。使用 Spire.PDF 类库,开发人员可以新建一个 PDF 文档或者对现有的 PDF 文档进行处理,且无需安装 Adobe Acrobat。

E-iceblue功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

Spire.PDF for.net下载

安装 Spire.PDF for .NET

首先,您需要将 Spire.PDF for.NET 包中包含的 DLL 文件作为引用添加到您的 .NET 项目中。 可以从此链接下载 DLL 文件,也可以通过NuGet安装。

复制代码
PM> Install-Package Spire.PDF
在 C# 和 VB.NET 中为 PDF 文档添加背景颜色

Spire.PDF for .NET 提供的PdfPageBase.BackgroundColor属性允许您将纯色设置为 PDF 背景。以下是详细步骤。

  • 创建一个PdfDocument实例。
  • **使用PdfDocument.LoadFromFile()**方法加载示例 PDF 文件。
  • 循环遍历所有 PDF 页面并使用PdfPageBase.BackgroundColor属性为每个页面添加背景颜色。
  • 使用PdfPageBase.BackgroudOpacity属性设置背景的不透明度。
  • **使用PdfDocument.SaveToFile()**方法保存结果文档。

【C# 】

复制代码
using Spire.Pdf;
using System.Drawing;

namespace PDFBackgroundColor
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();

//Load a sample PDF file from disk
pdf.LoadFromFile("input.pdf");

//Loop through the pages in the PDF file
foreach (PdfPageBase page in pdf.Pages)
{
//Set the background color for each page
page.BackgroundColor = Color.Yellow;

//Set the opacity of the background
page.BackgroudOpacity = 0.1f;
}

//Save the result PDF file
pdf.SaveToFile("BackgroundColor.pdf");
pdf.Close();

}
}
}

VB.NET

复制代码
Imports Spire.PDF
Imports System.Drawing

Namespace PDFBackgroundColor
Class Program
Private Shared Sub Main(ByVal args() As String)

'Create a PdfDocument instance
Dim pdf As PdfDocument = New PdfDocument

'Load a sample PDF file from disk
pdf.LoadFromFile("input.pdf")

'Loop through the pages in the PDF file
For Each page As PdfPageBase In pdf.Pages

'Set the background color for each page
page.BackgroundColor = Color.Yellow

'Set the opacity of the background
page.BackgroudOpacity = 0.1!
Next

'Save the result PDF file
pdf.SaveToFile("BackgroundColor.pdf")
pdf.Close()
End Sub
End Class
End Namespace
使用 C# 和 VB.NET 将背景图像添加到 PDF 文档

如果要添加图像作为背景以匹配文档主题,可以使用PdfPageBase.BackgroundImage属性。以下是详细步骤。

  • 创建一个PdfDocument实例。
  • **使用PdfDocument.LoadFromFile()**方法加载示例 PDF 文件。
  • 循环遍历所有 PDF 页面并使用PdfPageBase.BackgroundImage属性为每个页面添加背景图片。
  • 使用PdfPageBase.BackgroudOpacity属性设置背景的不透明度。
  • **使用PdfDocument.SaveToFile()**方法保存结果文档。

【C# 】

复制代码
using Spire.Pdf;
using System.Drawing;

namespace PDFBackgroundColor
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();

//Load a sample PDF file from disk
pdf.LoadFromFile("input.pdf");

//Loop through the pages in the PDF file
foreach (PdfPageBase page in pdf.Pages)
{
//Set the background color for each page
page.BackgroundColor = Color.Yellow;

//Set the opacity of the background
page.BackgroudOpacity = 0.1f;
}

//Save the result PDF file
pdf.SaveToFile("BackgroundColor.pdf");
pdf.Close();

}
}
}

VB.NET

复制代码
Imports Spire.PDF
Imports System.Drawing

Namespace PDFBackgroundColor
Class Program
Private Shared Sub Main(ByVal args() As String)

'Create a PdfDocument instance
Dim pdf As PdfDocument = New PdfDocument

'Load a sample PDF file from disk
pdf.LoadFromFile("input.pdf")

'Loop through the pages in the PDF file
For Each page As PdfPageBase In pdf.Pages

'Set the background color for each page
page.BackgroundColor = Color.Yellow

'Set the opacity of the background
page.BackgroudOpacity = 0.1!
Next

'Save the result PDF file
pdf.SaveToFile("BackgroundColor.pdf")
pdf.Close()
End Sub
End Class
End Namespace
相关推荐
不想有bug的小菜鸟7 分钟前
vue3使用iframe全屏展示pdf效果
前端·pdf
m0_748238638 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
u0100559609 分钟前
前端代理,解决跨域问题讲解
前端
quitv14 分钟前
react脚手架配置别名
前端·javascript·react.js
m0_5287238123 分钟前
前端如何进行性能优化
前端·性能优化
化作繁星24 分钟前
在 Vue 3 中,如何缓存和复用动态组件
前端·vue.js·缓存
一粒沙-44 分钟前
iOS 将GIF图分享至微信
前端·ios
graywen1 小时前
从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
前端
吃汤圆的抹香鲸1 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net
Gazer_S2 小时前
【现代前端框架中本地图片资源的处理方案】
前端·javascript·chrome·缓存·前端框架