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
相关推荐
史努比的大头1 小时前
前端开发深入了解webpack
前端
Dovir多多1 小时前
渗透测试入门学习——php与mysql数据库连接、使用session完成简单的用户注册、登录
前端·数据库·后端·mysql·安全·html·php
B.-1 小时前
Remix 学习 - @remix-run/react 中主要的 hooks
前端·javascript·学习·react.js·web
小刘|2 小时前
《实现 HTML 图片轮播效果》
前端·html
罗_三金2 小时前
微信小程序读写NFC标签(实现NFC标签快速拉起小程序)实战
前端·javascript·微信小程序·小程序
大大。3 小时前
el-input 只能输入数字和一个小数点,或者只能输入正整数
前端·javascript·vue.js
盼盼盼3 小时前
如何避免在使用 Context API 时出现状态管理的常见问题?
前端·javascript·react.js
Amd7944 小时前
Nuxt Kit 中的布局管理
前端·web开发·nuxt.js·布局管理·代码示例·addlayout·页面结构
超雄代码狂4 小时前
JavaScript web API完结篇---多案例
开发语言·前端·javascript
程序员小羊!4 小时前
切换淘宝最新镜像源npm详细讲解
前端·npm·node.js