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

server/2024/9/23 2:26:09/

在 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.DrawingNamespace 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.DrawingNamespace 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.ppmy.cn/server/118380.html

相关文章

Docker部署镜像 发布容器 容器网络互联 前端打包

准备工作 导入相关依赖 <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.7</version></dependency><dependency><groupId>com.baomidou<…

通过JNI创建java对象和访问java属性

一&#xff1a;通过jni创建Java对象 1.NewObject创建对象 在 Android NDK 中&#xff0c;使用 JNI&#xff08;Java Native Interface&#xff09;可以通过 NewObject 函数创建 Java 对象。NewObject 函数允许你在 C/C 代码中实例化 Java 类的对象。 1. 函数说明 NewObject…

Java | Leetcode Java题解之第409题最长回文串

题目&#xff1a; 题解&#xff1a; class Solution {public int longestPalindrome(String s) {int[] count new int[128];int length s.length();for (int i 0; i < length; i) {char c s.charAt(i);count[c];}int ans 0;for (int v: count) {ans v / 2 * 2;if (v …

初始爬虫7

针对数据提取的项目实战&#xff1a; 补充初始爬虫6的一个知识点&#xff1a; etree.tostring能够自动补全html缺失的标签&#xff0c;显示原始的HTML结构 # -*- coding: utf-8 -*- from lxml import etreetext <div> <ul> <li class"item-1">…

Prism库:详解其核心组件和使用方法

Prism库简介 Prism库是一个开源项目&#xff0c;由 Microsoft 社区开发和维护。它是一组用于创建 WPF、UWP 和 Xamarin 应用程序的工具和库&#xff0c;提供了一种基于模块化和依赖注入的架构模式&#xff0c;同时它提供了一系列的工具&#xff0c;帮助开发人员构建可扩展、可…

C# 手动写入日志,过大写入新文件

老项目没有用logf4j等日志框架&#xff0c;使用的是手动写入文件的方式存储日志。当日志过大会出现写入缓慢问题。下面采用IO异步写入以及文件过大分片等方式解决问题。 private static readonly object _lock new object(); private const long MaxFileSize 10 * 1024 * 10…

重生归来之挖掘stm32底层知识(1)——寄存器

概念理解 要使用stm32首先要知道什么是引脚和寄存器。 如下图所示&#xff0c;芯片通过这些金属丝与电路板连接&#xff0c;这些金属丝叫做引脚。一般做软件开发是不需要了解芯片是怎么焊的&#xff0c;只要会使用就行。我们平常通过编程来控制这些引脚的输入和输出&#xff0c…

ubuntu 22.04 ~24.04 如何修改登录背景

ubuntu 22.04 ~24.04 如何修改登录背景 背景&#xff1a;由于22.04 登录gdm的变更&#xff0c;之前的修改登录背景的方案已经无法使用。现在给大家分享新的使用方法&#xff1a; 1&#xff0c;下载如下路径的脚本&#xff1a; https://download.csdn.net/download/xdhyqd/89…