C# 使用 itextsharp 对pdf文件进行分割的几种方法(1)——按页码拷贝分割

本文将介绍如何在C#中利用iTextSharp库按指定页码对PDF文件进行分割,首先需要设置引用和使用iTextSharp的相关语句。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Set up references and Using Statements to use iTextSharp

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

namespace iTextTools
{
    public class PdfExtractorUtility
    {

    }
}


方法一:从已有pdf中拷贝指定的页码范围到一个新的pdf文件中:

1、使用pdfCopyProvider.AddPage()方法

public void ExtractPages(string sourcePdfPath, string outputPdfPath, 
    int startPage, int endPage)
{
    PdfReader reader = null;
    Document sourceDocument = null;
    PdfCopy pdfCopyProvider = null;
    PdfImportedPage importedPage = null;

    try
    {
        
        reader = new PdfReader(sourcePdfPath);

        sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));

        pdfCopyProvider = new PdfCopy(sourceDocument, 
            new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create))
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值