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 添加背景颜色或背景图像

使用 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

C#/VB.NET:为 PDF 添加背景颜色或背景图像

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值