zoukankan      html  css  js  c++  java
  • C# 合并及拆分PDF文件

    C# 合并及拆分PDF文件

    有时我们可能会遇到下图这样一种情况 — 我们需要的资料或教程被分成了几部分存放在多个PDF文件中,不管是阅读还是保存都不是很方便,这时我们肯定想要把这些PDF文件合并为一个PDF文件。相对应的,有时候我们也需要拆分一个大的PDF文件,来从中获取我们需要的那一部分资料。这篇文章主要分享如何使用C#来将多个PDF文件合并为一个PDF文件以及将一个PDF文件拆分为多个PDF文件。

       

                       

    合并PDF文件

    合并PDF文件的代码很简单,主要分为三步,首先获取需要合并的PDF文件,然后调用public static PdfDocumentBase MergeFiles(string[] InputFiles)方法,将这些PDF文件合并,然后保存文件。

    代码如下:

    using System;
    using Spire.Pdf;
    namespace 合并PDF文件
    {
        class Program
        {
            static void Main(string[] args)
            {
                String[] files = new String[] { "文件1.pdf", "文件2.pdf", "文件3.pdf" };
                string outputFile = "输出.pdf";
                PdfDocumentBase doc = PdfDocument.MergeFiles(files);
                doc.Save(outputFile, FileFormat.PDF);
                System.Diagnostics.Process.Start(outputFile);
            }
        }
    }
    

    合并前:

    合并后:

     

    拆分PDF文件

    在拆分PDF文件时,我们可以选择将文件的每一页单独拆分为一个PDF文件,还可以设定页码范围,将其拆分为多个PDF文件。下面将分两个部分来介绍。

    一、将PDF文件的每一页拆分为一个单独的PDF文件

    在上一个部分中,合并后的PDF文件一共有4页,这里我将它的每一页拆分为一个单独的PDF文件。

    代码如下:

    using System;
    using Spire.Pdf;
    
    namespace 拆分PDF文件1
    {
        class Program
        {
            static void Main(string[] args)
            {
                PdfDocument doc = new PdfDocument("输出.pdf");
                String pattern = "拆分-{0}.pdf";
                doc.Split(pattern);
                doc.Close();
            }
        }
    }
    

    效果图:

     

    二、根据指定页面范围拆分PDF文件

    这里我将一个18页的PDF文件的前10页拆分为一个PDF文件,后8页拆分为另一个PDF文件。

     

    代码如下:

    using System.Drawing;
    using Spire.Pdf;
    using Spire.Pdf.Graphics;
    
    namespace 拆分PDF文件2
    {
        class Program
        {
            static void Main(string[] args)
            {
                PdfDocument pdf = new PdfDocument();
                pdf.LoadFromFile("各种点心的做法.pdf");
     
                PdfDocument pdf1 = new PdfDocument();
                PdfPageBase page;
                for (int i = 0; i < 10; i++)
                {
                    page = pdf1.Pages.Add(pdf.Pages[i].Size, new PdfMargins(0));
                    pdf.Pages[i].CreateTemplate().Draw(page, new PointF(0, 0));
                }
                pdf1.SaveToFile("DOC_1.pdf"); 
    
                PdfDocument pdf2 = new PdfDocument();
                for (int i = 10; i < 18; i++)
                {
                    page = pdf2.Pages.Add(pdf.Pages[i].Size, new PdfMargins(0));
                    pdf.Pages[i].CreateTemplate().Draw(page, new PointF(0, 0));
                }
                pdf2.SaveToFile("DOC_2.pdf");
            }
        }
    }
    

    拆分前:

    拆分后:

    Note: 这里我使用了一个PDF组件Spire.PDF.

  • 相关阅读:
    pip 安装指定版本
    译文:A Robust and Modular Multi-Sensor Fusion ApproachApplied to MAV Navigation
    泡泡一分钟:Perception-aware Receding Horizon Navigation for MAVs
    泡泡一分钟: A Linear Least Square Initialization Method for 3D Pose Graph Optimization Problem
    泡泡一分钟:LandmarkBoost: Efficient Visual Context Classifiers for Robust Localization
    泡泡一分钟:Visual Odometry Using a Homography Formulation with Decoupled Rotation and Translation Estimation Using Minimal Solutions
    小程序在同一行
    oracle分页查询原理浅析
    css实现圆形头像
    纯css控制文字2行显示多余部分隐藏
  • 原文地址:https://www.cnblogs.com/Yesi/p/5604166.html
Copyright © 2011-2022 走看看