zoukankan      html  css  js  c++  java
  • Merge PDF File using itextsharp library

    using System;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    public class PdfMerge
    {
    public static void MergeFiles(string destinationFile, string[] sourceFiles)
    {
    try
    {
    int f = 0;
    // we create a reader for a certain document
    PdfReader reader = new PdfReader(sourceFiles[f]);
    // we retrieve the total number of pages
    int n = reader.NumberOfPages;
    //Console.WriteLine("There are " + n + " pages in the original file.");
    // step 1: creation of a document-object
    Document document = new Document(reader.GetPageSizeWithRotation(1));
    // step 2: we create a writer that listens to the document
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
    // step 3: we open the document
    document.Open();
    PdfContentByte cb = writer.DirectContent;
    PdfImportedPage page;
    int rotation;
    // step 4: we add content
    while (f < sourceFiles.Length)
    {
    int i = 0;
    while (i < n)
    {
    i++;
    document.SetPageSize(reader.GetPageSizeWithRotation(i));
    document.NewPage();
    page = writer.GetImportedPage(reader, i);
    rotation = reader.GetPageRotation(i);
    if (rotation == 90 || rotation == 270)
    {
    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
    }
    else
    {
    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
    }
    //Console.WriteLine("Processed page " + i);
    }
    f++;
    if (f < sourceFiles.Length)
    {
    reader = new PdfReader(sourceFiles[f]);
    // we retrieve the total number of pages
    n = reader.NumberOfPages;
    //Console.WriteLine("There are " + n + " pages in the original file.");
    }
    }
    // step 5: we close the document
    document.Close();
    }
    catch(Exception e)
    {
    string strOb = e.Message;
    }
    }

    public int CountPageNo(string strFileName)
    {
    // we create a reader for a certain document
    PdfReader reader = new PdfReader(strFileName);
    // we retrieve the total number of pages
    return reader.NumberOfPages;
    }
    }
  • 相关阅读:
    Application,Session,Cookie,ViewState,Cache对象用法、作用域的区别
    leetcode系列---3Sum C#code
    leetcode系列---atoiFunction C#code
    leetcode系列---Two Sum C#code
    .NET webAPI中集成swagger
    js去空格
    clip 属性剪裁绝对定位元素
    css实现隐藏显示
    无阻塞加载脚本的方案
    打乱数字数组的顺序
  • 原文地址:https://www.cnblogs.com/top5/p/2875709.html
Copyright © 2011-2022 走看看