[工具]Pdf加解密

[工具]Pdf加解密

Pdf加解密

將程式包裝一下~


using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Diagnostics;

/// <summary>
/// Pdf加解密
/// </summary>
public class PdfUtil
{
    //執行檔所在位置
    public static string ConnectString = @"C:\Test\pdftk.exe";
    /// <summary>
    /// Encrypts the PDF.
    /// </summary>
    /// <param name="Source">The source.</param>
    /// <param name="Destination">The destination.</param>
    /// <param name="OwnerPw">The owner pw.</param>
    /// <param name="UserPw">The user pw.</param>
    public static void EncryptPdf(string Source, string Destination, string OwnerPw, string UserPw, params string[] Allows)
    {
        /* Allow Options
         * 
         Printing
            Top Quality Printing

         DegradedPrinting
            Lower Quality Printing

         ModifyContents
            Also allows Assembly

         Assembly

         CopyContents
            Also allows ScreenReaders

         ScreenReaders

         ModifyAnnotations
            Also allows FillIn

         FillIn

         AllFeatures
         */
        string Permision = "";
        if (Allows.Length != 0)
        {
            for (int i = 0; i < Allows.Length; i++)
                Permision += " allow " + Allows[i];
        }
        if (UserPw != "")
            Process.Start(ConnectString, Source + " output " + Destination + " owner_pw " + OwnerPw + "  user_pw " + UserPw + Permision);
        else
            Process.Start(ConnectString, Source + " output " + Destination + " owner_pw " + OwnerPw + Permision);
    }
    /// <summary>
    /// Decrypts the PDF.
    /// </summary>
    /// <param name="Source">The source.</param>
    /// <param name="Destination">The destination.</param>
    /// <param name="InputPw">The input pw.</param>
    public static void DecryptPdf(string Source, string Destination, string InputPw)
    {
        Process.Start(ConnectString, Source + " input_pw " + InputPw + " owner_pw " + Destination);
    }
    /// <summary>
    /// Merges the PDF.
    /// </summary>
    /// <param name="Destination">The destination.</param>
    /// <param name="Source">The source.</param>
    public static void MergePdf(string Destination, params string[] Source)
    {
        string FileList = string.Join(" ", Source);
        Process.Start(ConnectString, FileList + " cat output " + Destination);
    }
    /// <summary>
    /// Merges the cut PDF.
    /// </summary>
    /// <param name="Destination">The destination.</param>
    /// <param name="Source">The source.</param>
    /// <param name="Start">The start.</param>
    /// <param name="End">The end.</param>
    /// <returns></returns>
    public static void MergeCutPdf(string Destination, string[] Source, string[] Start, string[] End)
    {
        string Result = "", FileList = "", PageList = "";
        int i, j;
        for (i = 0; i < Source.Length; i++)
        {
            int Count = 65 + i;
            FileList += (char)Count + "=" + Source[i];
            PageList += (char)Count + Start[i] + "-" + End[i] + " ";
        }
        Result = FileList + " cat " + PageList + " output " + Destination;
        Process.Start(ConnectString, Result);
    }
    /// <summary>
    /// Rotatas the PDF90 degree.
    /// </summary>
    /// <param name="Source">The source.</param>
    /// <param name="Destination">The destination.</param>
    public static void RotataPdf90Degree(string Source, string Destination)
    {
        Process.Start(ConnectString, Source + " cat 2-end output " + Destination);
    }

    /// <summary>
    /// Rotatas the PDF180 degree.
    /// </summary>
    /// <param name="Source">The source.</param>
    /// <param name="Destination">The destination.</param>
    public static void RotataPdf180Degree(string Source, string Destination)
    {
        Process.Start(ConnectString, Source + " cat 1-endS " + Destination);
    }
    /// <summary>
    /// Uncompresses the PDF.
    /// </summary>
    /// <param name="Source">The source.</param>
    /// <param name="Destination">The destination.</param>
    public static void UncompressPdf(string Source, string Destination)
    {
        Process.Start(ConnectString, Source + " output " + Destination + " uncompress ");
    }
    /// <summary>
    /// Repairs the PDF.
    /// </summary>
    /// <param name="Source">The source.</param>
    /// <param name="Destination">The destination.</param>
    public static void RepairPdf(string Source, string Destination)
    {
        Process.Start(ConnectString, Source + " output " + Destination);
    }
    /// <summary>
    /// Bursts the PDF.
    /// </summary>
    /// <param name="Source">The source.</param>
    public static void BurstPdf(string Source)
    {
        //Burst a Single PDF Document into Single Pages and Report its Data to doc_data.txt
        Process.Start(ConnectString, Source + " burst ");
    }
    /// <summary>
    /// Reports the PDF info.
    /// </summary>
    /// <param name="Source">The source.</param>
    /// <param name="TxtFile">The TXT file.</param>
    public static void ReportPdfInfo(string Source, string TxtFile)
    {
        //Report on PDF Document Metadata, Bookmarks and Page Labels
        Process.Start(ConnectString, Source + " dump_data output " + TxtFile);
    }
}