欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

通過C#實(shí)現(xiàn)裁剪PDF頁面功能

 更新時(shí)間:2024年09月12日 10:15:12   作者:Eiceblue  
在處理PDF文檔時(shí),有時(shí)需要精確地裁剪頁面以適應(yīng)特定需求,比如去除廣告、背景信息或者僅僅是為了簡化文檔內(nèi)容,本文將指導(dǎo)如何使用免費(fèi).NET控件通過C#實(shí)現(xiàn)裁剪PDF頁面,需要的朋友可以參考下

前言

在處理PDF文檔時(shí),有時(shí)需要精確地裁剪頁面以適應(yīng)特定需求,比如去除廣告、背景信息或者僅僅是為了簡化文檔內(nèi)容。

本文將指導(dǎo)如何使用免費(fèi).NET控件通過C#實(shí)現(xiàn)裁剪PDF頁面。

C# 裁剪PDF頁面

Free Spire.PDF for .NET這個(gè)庫提供了一個(gè)非常簡單的接口來實(shí)現(xiàn)裁剪PDF頁面指定區(qū)域,具體操作如下:

  • 通過 LoadFromFile() 方法加載PDF文檔;
  • 獲取指定PDF頁面;
  • 指定一個(gè)區(qū)域,然后通過 PdfPageBase.CropBox 屬性裁剪指定區(qū)域;
  • 通過 SaveToFile() 方法保存裁剪后的PDF文檔。

示例代碼如下:

using System.Drawing;
using Spire.Pdf;

namespace CropPDFPage
{
    class Program
    {
        static void Main(string[] args)
        {
            //加載PDF文檔
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("示例.pdf");

            //獲取第二頁
            PdfPageBase page = pdf.Pages[1];

            //按指定區(qū)域裁剪PDF頁面
            page.CropBox = new RectangleF(270, 130, 400, 480);

            //保存裁剪后的文檔
            pdf.SaveToFile("裁剪PDF.pdf");
            pdf.Close();
        }
    }
}

裁剪前后對比:

拓展:C#實(shí)現(xiàn)pdf按頁分割文件,以及分頁合并

效果

下面就是通過pdf插件進(jìn)行按頁進(jìn)行文件分割輸出

單頁分割

插件命名空間

using iTextSharp.text;
using iTextSharp.text.pdf;

目標(biāo)分割pdf文件、創(chuàng)建輸出文件所在的文件夾、iTextSharp插件操作pdf分割

// 目標(biāo)分割pdf文件
string inputFilePath = @"你自己的pdf文件物理路徑.pdf";

// 創(chuàng)建輸出文件所在文件夾
string outputFolder = "NewFile";
string rootPath = System.IO.Directory.GetCurrentDirectory();
string folderAll = Path.Combine(rootPath, outputFolder);
if (!Directory.Exists(folderAll))
{
    Directory.CreateDirectory(folderAll);
}

// 操作pdf分割
using (PdfReader reader = new PdfReader(inputFilePath))
{
    for (int i = 1; i <= reader.NumberOfPages; i++)
    {
        string newFilePath = Path.Combine(outputFolder, $"page_{i}.pdf");
        
        using (Document document = new Document())
        using (PdfCopy copy = new PdfCopy(document, new FileStream(newFilePath, FileMode.Create)))
        {
            document.Open();
            copy.AddPage(copy.GetImportedPage(reader, i));
            document.Close();
        }
    }
}

Console.WriteLine("PDF 分割完成!");

文件合并

// 目標(biāo)合并pdf文件
string[] sourceFiles = new string[] {
    @"你的pdf文件1.pdf",
    @"你的pdf文件2.pdf"
};

// 創(chuàng)建輸出文件所在文件夾
string outputFolder = "NewFile";
string rootPath = System.IO.Directory.GetCurrentDirectory();
string folderAll = Path.Combine(rootPath, outputFolder);
if (!Directory.Exists(folderAll))
{
    Directory.CreateDirectory(folderAll);
}

using (Document document = new Document())
{
    PdfCopy copy = new PdfCopy(document, new FileStream($"{outputFolder}\\page_1_20_Add_21_40.pdf", FileMode.Create));
    document.Open();

    foreach (string file in sourceFiles)
    {
        using (PdfReader reader = new PdfReader(file))
        {
            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                copy.AddPage(copy.GetImportedPage(reader, i));
            }
        }
    }

    document.Close();
    copy.Close();
}

多頁分割

根據(jù)分頁范圍進(jìn)行分割文件,比如:1-10頁分割一個(gè)文件,即10頁分割一個(gè)文件

    // 目標(biāo)分割pdf文件
    string inputFilePath = @"你自己的pdf文件物理路徑.pdf";

    // 創(chuàng)建輸出文件所在文件夾
    string outputFolder = "NewFile";
    string rootPath = System.IO.Directory.GetCurrentDirectory();
    string folderAll = Path.Combine(rootPath, outputFolder);
    if (!Directory.Exists(folderAll))
    {
        Directory.CreateDirectory(folderAll);
    }

    // 操作pdf分割
    using (PdfReader reader = new PdfReader(inputFilePath))
    {
        int startPage = 1;
        int pageSize = 0;
        int totalPage = 0;
        int unitSize = 20;
        int remainder = 0;
        totalPage = reader.NumberOfPages;
        pageSize = totalPage / unitSize;
        remainder = totalPage % unitSize;

        // 足夠20的分割文件
        int currentIndex = 0;
        for (int index = 0; index < pageSize; index++)
        {
            currentIndex = (index + 1);
            using (Document document = new Document())
            {
                int sv = (startPage + index * unitSize);
                int ev = ((index + 1) * unitSize);
                string newFilePath = Path.Combine(outputFolder, $"page_{sv}_{ev}.pdf");
                PdfCopy copy = new PdfCopy(document, new FileStream(newFilePath, FileMode.Create));
                document.Open();

                for (int i = sv; i <= ev; i++)
                {
                    copy.AddPage(copy.GetImportedPage(reader, i));
                }

                document.Close();
                copy.Close();
            }
        }

        // 不足20頁的文件
        using (Document document = new Document())
        {
            int sv = (startPage + pageSize * unitSize);
            int ev = (pageSize * unitSize + remainder);
            string newFilePath = Path.Combine(outputFolder, $"page_size_{sv}_{ev}.pdf");
            PdfCopy copy = new PdfCopy(document, new FileStream(newFilePath, FileMode.Create));
            document.Open();

            for (int i = sv; i <= ev; i++)
            {
                copy.AddPage(copy.GetImportedPage(reader, i));
            }

            document.Close();
            copy.Close();
        }
    }
}

插件說明

iTextSharp 是一個(gè)開源的 PDF 處理庫,用于在 C# 程序中創(chuàng)建、編輯和處理 PDF 文件。它提供了豐富的功能和 API,使開發(fā)者能夠進(jìn)行各種 PDF 文件操作,包括創(chuàng)建 PDF、添加文本、插入圖片、設(shè)置頁面布局等功能。iTextSharp 庫基于 iText 庫的 C# 版本,是在 C# 平臺上操作 PDF 文件的常用工具之一。

以下是 iTextSharp 的一些基本功能:

1、創(chuàng)建 PDF 文件

使用 iTextSharp 可以在 C# 中輕松地創(chuàng)建新的 PDF 文件,可以通過代碼指定文檔結(jié)構(gòu)、頁面布局、文本樣式等。

2、編輯 PDF 文件內(nèi)容

可以向已有的 PDF 文件中添加文本、圖片、表格等內(nèi)容,也可以修改現(xiàn)有內(nèi)容,實(shí)現(xiàn)文檔內(nèi)容的動(dòng)態(tài)更新。

3、處理 PDF 文件

iTextSharp 提供了豐富的 API,可以處理 PDF 文件中的文本、表格、圖形等元素,實(shí)現(xiàn)對 PDF 內(nèi)容的精確控制和調(diào)整。

4、設(shè)置頁面屬性

可以通過 iTextSharp 設(shè)置頁面尺寸、方向、邊距等屬性,定制化生成的 PDF 文檔格式。

5、添加水印和加密

可以在 PDF 文件中添加水印、數(shù)字簽名,也可以通過 iTextSharp 對 PDF 文件進(jìn)行加密保護(hù),確保 PDF 文件的安全性。

6、PDF 文件合并和拆分

iTextSharp 提供了合并多個(gè) PDF 文件和拆分單個(gè) PDF 文件的功能,方便進(jìn)行文檔的整合和拆分操作。

到此這篇關(guān)于通過C#實(shí)現(xiàn)裁剪PDF頁面功能的文章就介紹到這了,更多相關(guān)C#裁剪PDF頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論