当前位置: 首页 > article >正文

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


http://www.kler.cn/news/308560.html

相关文章:

  • python压缩图片的代码
  • 《锐捷AP 胖模式配置示例》
  • UiBot教程:实现复杂流程图的高效方法
  • C++学习笔记(21)
  • solidity-21-call_contract
  • 华为SMU02B1智能通信电源监控单元模块简介
  • 基于SpringBoot+Vue的养老院管理系统
  • 在Ubuntu中编译含有JSON的文件出现报错
  • 【前后端】大文件切片上传
  • 网络安全学习(一)初识kali
  • 【JavaEE初阶】多线程(5 单例模式 \ 阻塞队列)
  • 构建基于 Feign 的微服务:从 Eureka 到负载均衡的实践 --day05
  • 微信支付开发-前端api实现
  • 大模型笔记03--快速体验dify
  • HTTP的强制缓存和协商缓存有什么区别和联系?
  • 《使用 LangChain 进行大模型应用开发》学习笔记(三)
  • 行人动作行为识别系统源码分享
  • LLamaindex基本使用
  • MYSQL数据库基础篇——DDL
  • 第7篇:【系统分析师】计算机网络
  • openwrt wsdd模块介绍
  • C++(重载)
  • Skytower
  • 第二百三十五节 JPA教程 - JPA Lob列示例
  • k8s(kubernetes)的PV / PVC / StorageClass(理论+实践)
  • 数据库的操作:SQL运算符(算法/比较/逻辑/位)
  • 【大模型专栏—进阶篇】智能对话全总结
  • 力扣100题——动态规划(二)
  • React Native防止重复点击
  • 详解 Pandas 的透视表函数