目录

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
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
好_快1 分钟前
Lodash源码阅读-findLastIndex
前端·javascript·源码阅读
好_快2 分钟前
Lodash源码阅读-sortedIndex
前端·javascript·源码阅读
Java小白中的菜鸟3 分钟前
SSE Emitter在Spring Boot和Vue中的简单使用
前端·vue.js·spring boot
好_快3 分钟前
Lodash源码阅读-baseSortedIndex
前端·javascript·源码阅读
Java潘老师1 小时前
DeepSeek 全套汇总资料pdf免费下载(最新更新8篇)
pdf·deepseek
智绘前端1 小时前
TypeScript面试题集合【初级、中级、高级】
前端·javascript·面试·typescript
xrkhy3 小时前
CSS平面转换
前端·css·平面
zpjing~.~3 小时前
CSS >子元素选择器和空格
前端·css
百万蹄蹄向前冲7 小时前
三句话四分钟,豆包出题把吾秀
前端·人工智能·豆包marscode
半旧5188 小时前
重构谷粒商城11:node快速入门
java·前端·重构