Spire.PDF:如何添加、删除PDF页面以及自定义文档属性

spire.pdf 4.8 破解版 ,使用无限制,无水印-有测试代码.rar

更多资源查看:Spire.XLS工作表教程 | Spire.Doc系列教程 | Spire.PDF系列教程

下载Spire.PDF最新试用版

Spire.PDF是一个专业的PDF组件,能够独立地创建、编写、编辑、操作和阅读PDF文件,支持 .NET、Java、WPF和Silverlight。Spire.PDF的PDF API拥有丰富的功能,如安全设置(包括数字签名)、PDF文本/附件/图片提取、PDF文件合并/拆分、元数据更新、章节和段落优化、图形/图像描绘和插入、表格创建和处理、数据导入等等。

Spire.PDF官方地址:https://www.e-iceblue.cn/spirepdf/working-with-pdf-pages.html


C# 添加或删除 PDF 页面,调整页面顺序

操作PDF文档时,我们通常会需要添加或删除一些PDF页面。接下来将详细描述如何使用Spire.PDF添加或删除PDF页面,以及如何调整PDF里的页面顺序。

▲添加PDF空白页

1.1 在默认位置,即文档末插入一张空白页

C#

   //创建PDF文档1,并加载测试文档
            PdfDocument doc1 = new PdfDocument();
            doc1.LoadFromFile("sample.pdf");
 
            //添加一页空白页到文档(默认在文档最后一页添加)
            doc1.Pages.Add();
            //保存并打开文档
            doc1.SaveToFile("result1.pdf");
            System.Diagnostics.Process.Start("result1.pdf");

1.2 在指定位置插入空白页

C#

           //创建文档2,加载测试文档
            PdfDocument doc2 = new PdfDocument();
            doc2.LoadFromFile("sample.pdf");
 
            //添加一页空白页作为第2页
            doc2.Pages.Insert(1);
 
            //保存并打开文档
            doc2.SaveToFile("result2.pdf");
            System.Diagnostics.Process.Start("result2.pdf");

2. 删除PDF空白页

这里的测试文档中,包含了两页空白页,一页空白页是没有任何内容的;另一页空白页是包含了空白图片的页面,看似没有内容,但是这样的页面实际上也是不需要的。
C#

using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.Graphics;
 
namespace DeleteBlankPage_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //应用许可证
            Spire.License.LicenseProvider.SetLicenseFileName("license.elic.xml");
 
            //创建PdfDocument类对象,并加载PDF文档
            PdfDocument document = new PdfDocument();
            document.LoadFromFile("Test.pdf");
 
            //遍历文档中所有页面 
            for (int i = document.Pages.Count - 1; i >= 0; i--)
            {
                //诊断页面是否为空白页
                if (document.Pages[i].IsBlank())
                {
                    //删除空白页 
                    document.Pages.RemoveAt(i);
                }
                else
                {
                    //将PDF页转换为Bitmap图像
                    Image image = document.SaveAsImage(i, PdfImageType.Bitmap);
 
                    //诊断图片是否为空白图片
                    if (IsImageBlank(image))
                    {
                        //移除包含空白图片的页面
                        document.Pages.RemoveAt(i);
                    }
                }
            }
 
            //保存并打开文档
            document.SaveToFile("RemoveBlankPage.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("RemoveBlankPage.pdf");
        }
 
        //自定义方法IsImageBlank()诊断图片是否为空白图片
        public static bool IsImageBlank(Image image)
        {
            //初始化Bitmap类实例,遍历文档中所有图片
            Bitmap bitmap = new Bitmap(image);
            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    Color pixel = bitmap.GetPixel(i, j);
                    if (pixel.R < 240 || pixel.G < 240 || pixel.B < 240)
                    {
                        return false;
                    }
                }
            }
            return true;
        }
    }
}

附:VB.NET代码(删除PDF空白页)

Imports Spire.Pdf
Imports System.Drawing
Imports Spire.Pdf.Graphics
 
Namespace DeleteBlankPage_PDF
 
    Class Program
 
        Private Shared Sub Main(ByVal args As String())
            Spire.License.LicenseProvider.SetLicenseFileName("license.elic.xml")
            Dim document As PdfDocument = New PdfDocument()
            document.LoadFromFile("Test.pdf")
            For i As Integer = document.Pages.Count - 1 To 0
                If document.Pages(i).IsBlank() Then
                    document.Pages.RemoveAt(i)
                Else
                    Dim image As Image = document.SaveAsImage(i, PdfImageType.Bitmap)
                    If IsImageBlank(image) Then
                        document.Pages.RemoveAt(i)
                    End If
                End If
            Next
 
            document.SaveToFile("RemoveBlankPage.pdf", FileFormat.PDF)
            System.Diagnostics.Process.Start("RemoveBlankPage.pdf")
        End Sub
 
        Public Shared Function IsImageBlank(ByVal image As Image) As Boolean
            Dim bitmap As Bitmap = New Bitmap(image)
            For i As Integer = 0 To bitmap.Width - 1
                For j As Integer = 0 To bitmap.Height - 1
                    Dim pixel As Color = bitmap.GetPixel(i, j)
                    If pixel.R < 240 OrElse pixel.G < 240 OrElse pixel.B < 240 Then
                        Return False
                    End If
                Next
            Next
 
            Return True
        End Function
    End Class
End Namespace

▲添加新页面到已有的PDF文档

Spire.PDF 提供了两个方法来添加新页面,Pages.Add()方法是将页面添加至文档最后一页,Pages.Insert() 方法支持将新页面添加到PDF文档的首页,中间,或者末尾页面。

PdfDocument doc = new PdfDocument();
 doc.LoadFromFile("sample.pdf");
 //将新页面添加到文档末尾
 doc.Pages.Add();

 doc.SaveToFile(“result.pdf”,FileFormat.PDF);
 
 PdfDocument doc2 = new PdfDocument();
 doc2.LoadFromFile(“sample.pdf”);

 //将新页面添加到第二页
 doc2.Pages.Insert(1);

 doc2.SaveToFile (“result2.pdf”,FileFormat.PDF);

添加新页面到最后一页效果图:

Working-with-PDF-pages-1.png

添加新页面到第二页效果图:

Working-with-PDF-pages-2.png

 

▲删除PDF中的页面

我们可以使用Spire.PDF 提供的Pages.RemoveAt()方法删除PDF中我们想要删除的指定页面。

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("result.pdf");

//删除最后一页
doc.Pages.RemoveAt(2);

doc.SaveToFile("删除末页.pdf",FileFormat.PDF);

Working-with-PDF-pages-3.png

 

▲调整PDF文档中的页面顺序

Spire.PDF 提供Rearrange(int[] orderArray) 方法,让我们可以重新排列调整PDF文档中的页面顺序。

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("result2.pdf");

//将空白页调到第一页
doc.Pages.ReArrange(new int[] { 1, 0, 2 });

doc.SaveToFile("页面顺序调整.pdf",FileFormat.PDF);

Working-with-PDF-pages-4.png

 
C# 添加、获取和删除PDF自定义文档属性

在PDF文档中,自定义文档属性可以用来存储特定的元数据类型,例如版本号或公司名称等。我们可以给一个PDF文档添加自定义文档属性,同时也可以查看和删除PDF文档中已有的自定义属性。下面将介绍如何使用Spire.PDF组件在PDF文档中添加、获取和删除自定义文档属性。

▲添加自定义文档属性

//实例化一个PdfDocument对象
PdfDocument doc = new PdfDocument();
//载入PDF文档
doc.LoadFromFile("Input.pdf");

//添加自定义文档属性
doc.DocumentInformation.SetCustomerDefined("版本号", "6.0.5");            
doc.DocumentInformation.SetCustomerDefined("公司名称", "E-iceblue"); 
doc.DocumentInformation.SetCustomerDefined("产品", "Spire.Doc for .NET");           

//保存文档
doc.SaveToFile("添加自定义属性.pdf");

add-get-and-delete-custom-properties-of-pdf-document.png

▲获取自定义文档属性

//实例化一个PdfDocument对象
PdfDocument doc = new PdfDocument();
//载入PDF文档
doc.LoadFromFile("添加自定义属性.pdf");

//获取指定自定义属性
string version = doc.DocumentInformation.GetCustomerDefined("版本号");
string company = doc.DocumentInformation.GetCustomerDefined("公司名称");
string product = doc.DocumentInformation.GetCustomerDefined("产品");

//获取所有自定义文档属性
//DictionaryallCustomProperties = doc.DocumentInformation.GetAllCustomerDefined();            

Console.WriteLine("{0}\n{1}\n{2}\n", "版本号:" + version, "公司名称:" + company, "产品:" + product);

add-get-and-delete-custom-properties-of-pdf-document-2.png

▲删除自定义文档属性

//实例化一个PdfDocument对象
PdfDocument doc = new PdfDocument();
//载入PDF文档
doc.LoadFromFile("添加自定义属性.pdf");            

//删除指定自定义属性
doc.DocumentInformation.RemoveCustomerDefined("版本号");

//保存文档            
doc.SaveToFile("删除自定义属性.pdf");

add-get-and-delete-custom-properties-of-pdf-document-3.png

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
免费Spire.PDF for .NET 是一款由e-iceblue公司开发的专业性的PDF文档创建组件。它能够使用户在不用Adobe Acrobat和其他外部控件的情况下,运用.NET 应用程序阅读,编写和操纵PDF 文档Spire.PDF for .NET不仅可以运用在服端比如:ASP.NET 或者其他环境,还可以应用在Windows Forms 应用程序中。Spire.PDF for .NET 适合应用于所有常见的坏境中,比如:创建好的PDF文档可以存到磁盘中, 还可以在Windows Forms应用程序,ASP.NET 应用程序客户端浏览器中保存为数据流。 Spire.PDF for .NET 功能丰富。 除了基本的功能比如:绘制多种图形,图片,创建窗体字段,插入页眉页脚,输入数据表,自动对大型表格进行分页外,Spire.PDF for .NET还支持PDF数字签名,将HTML转换成PDF格式,提取PDF文档中的文本信息和图片,存为文本格式和各种图片格式,甚至可以将PDF中的附件提取出来。 主要功能 支持嵌入式字体,Truetype 字体和CJK字体。 支持绘图。比如:矩形,环形,弧形,椭圆形,也可以自定笔刷将其填充。 可以将图片从数据流, 磁盘文件中载入到PDF 文档中。 在PDF 文档中既可以绘制梯状图形和矢量图像,还支持掩模和水印图像。 可以在PDF 文档中载入数据表。可以设置表中的行和列的格式,还可以在表内加入图形元素。 自动对PDF 中的大型表格进行分页。 创建窗体字段。比如在PDF 文档中创建按钮,文本框,列表框,复选框等等。 在PDF 中插入页眉页脚。 通过设置所有者密码和用户密码来加密PDF文档。 通过作者的签名来保护PDF文档。 读取当前PDF文档的表格并且填充表格。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值