/// <summary>
        /// Inserts pages of a pdf file into this document at a specified position.
        /// </summary>
        /// <param name="pageStart">Page to start insert from (after)</param>
        /// <param name="position">One of InsertPositions to start insert from</param>
        /// <param name="fileToMerge">Path of the file to merge with this document</param>
        /// <param name="outputFileName">Path of the new file</param>
        /// <returns></returns>
        public OperationStates InsertPages(PDFDocument document, int pageStart, InsertPositions position, string fileToMerge, ref string outputFileName)
        {
            OperationStates state = OperationStates.Ok;

            if ((pageStart < 0) || (pageStart > document.NumberOfPages))
            {
                state = OperationStates.PageRangeOutOfDocument;
                return state;
            }

            string range = "";

            if ((position == InsertPositions.Beginning) || (pageStart == 0))
            {
                range = "B A";
            }
            else if ((position == InsertPositions.End) || (pageStart == document.NumberOfPages))
            {
                range = "A B";
            }
            else
            {
                range = "A1-" + pageStart.ToString() + " B A" + (pageStart + 1).ToString() + "-end";
            }

            GetValidOutputFileName(ref outputFileName, 1);

            string args = "A=\"" + document.FullName + "\" B=\"" + fileToMerge + "\" cat " +
                range + " output \"" + outputFileName + "\"";

            RunPdftk(args);

            return state;
        }
        /// <summary>
        /// Deletes one or more contiguous pages from a pdf file.
        /// </summary>
        /// <param name="pageStart">First page to delete</param>
        /// <param name="pageEnd">Last page to delete</param>
        /// <param name="outputFileName">Path of the new file (original file without the selected pages)</param>
        /// <param name="overwriteOriginal">Tells if the program should overwrite the original file</param>
        /// <returns></returns>
        public OperationStates DeletePages(PDFDocument document, int pageStart, int pageEnd, ref string outputFileName, bool overwriteOriginal)
        {
            OperationStates state = OperationStates.Ok;

            if ((pageStart < 1) || (pageStart > document.NumberOfPages) ||
                (pageEnd < 1) || (pageEnd > document.NumberOfPages) ||
                (pageStart > pageEnd))
            {
                state = OperationStates.PageRangeOutOfDocument;
                return state;
            }

            string rangeStart = pageStart - 1 > 0 ? "1-" + (pageStart - 1).ToString() : "";
            string rangeEnd = pageEnd < document.NumberOfPages ? (pageEnd + 1).ToString() + "-end" : "";

            if ((rangeStart == "") && (rangeEnd == ""))
            {
                state = OperationStates.PageRangeEntireDocument;
                return state;
            }

            GetValidOutputFileName(ref outputFileName, 1);

            string args = "\"" + document.FullName + "\" cat " +
                rangeStart + " " + rangeEnd + " output \"" + outputFileName + "\"";

            RunPdftk(args);

            return state;
        }
        /// <summary>
        /// Merge some PDF documents togheter.
        /// </summary>
        /// <param name="filesToMerge">List of file to merge</param>
        /// <param name="outputFileName">Path of the new file</param>
        /// <remarks>pdftk can handle only 26 files per command, so I bypass this limit programmatically.
        /// The files are now merged 2 at a time, recursively.</remarks>
        public OperationStates MergeDocuments(PDFDocument[] filesToMerge, ref string outputFileName)
        {
            OperationStates state = OperationStates.Ok;

            _mergeBgWorker.RunWorkerAsync(new MergeBgWorkerArgs()
                {
                    FilesToMerge = filesToMerge,
                    OutputFileName = outputFileName
                });

            return state;
        }
        /// <summary>
        /// Splits a single, input PDF document into individual	pages.
        /// </summary>
        /// <param name="destinationDirectory">Directory where save the burst pages.</param>
        /// <param name="prefix">Prefix used to name the burst pages.</param>
        /// <returns></returns>
        public OperationStates Burst(PDFDocument document, string destinationDirectory, string prefix)
        {
            OperationStates state = OperationStates.Ok;

            if (Directory.Exists(destinationDirectory))
            {
                string inputFileName = document.FullName;

                string args = "\"" + inputFileName + "\" burst output " + "\"" + destinationDirectory + "\\" + prefix + "%04d.pdf\"";

                RunPdftk(args, destinationDirectory);

                // Remove doc_data.txt (report file generated by pdftk)
                File.Delete(Path.Combine(System.IO.Path.GetTempPath(), "doc_data.txt"));
            }

            return state;
        }
        /// <summary>
        /// Decrypt the document with the given password
        /// </summary>
        /// <param name="password">Password to decrypt the document</param>
        /// <returns></returns>
        public OperationStates Decrypt(PDFDocument document, System.Security.SecureString password, ref string outputFileName)
        {
            OperationStates state = OperationStates.Ok;

            string inputFileName = document.FullName;

            IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(password);
            string pwd = System.Runtime.InteropServices.Marshal.PtrToStringAuto(ptr);

            GetValidOutputFileName(ref outputFileName, 1);

            string args = "\"" + inputFileName + "\" input_pw " + pwd + " output \"" + outputFileName + "\" dont_ask";

            pwd = null;

            if (RunPdftk(args) > 0)
            {
                state = OperationStates.WrongPassword;
            }

            return state;
        }
        /// <summary>
        /// Extracts contiguous pages from a pdf file and saves them into a new file.
        /// </summary>
        /// <param name="pageStart">First page to extract</param>
        /// <param name="pageEnd">Last page to extract</param>
        /// <param name="outputFileName">Path of the new file</param>
        /// <returns></returns>
        public OperationStates ExtractPages(PDFDocument document, int pageStart, int pageEnd, ref string outputFileName)
        {
            OperationStates state = OperationStates.Ok;

            if ((pageStart < 1) || (pageStart > document.NumberOfPages) ||
                (pageEnd < 1) || (pageEnd > document.NumberOfPages))
            {
                state = OperationStates.PageRangeOutOfDocument;
                return state;
            }

            string range = pageStart.ToString() + "-" + pageEnd.ToString();

            GetValidOutputFileName(ref outputFileName, 1);

            string args = "\"" + document.FullName + "\" cat " +
                range + " output \"" + outputFileName + "\"";

            RunPdftk(args);

            return state;
        }
        /// <summary>
        /// Try to decrypt the document with the given password
        /// </summary>
        /// <param name="password">Password to decrypt the document</param>
        /// <returns></returns>
        public OperationStates TryDecrypt(PDFDocument document, System.Security.SecureString password)
        {
            OperationStates state = OperationStates.Ok;

            string inputFileName = document.FullName;

            IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(password);
            string pwd = System.Runtime.InteropServices.Marshal.PtrToStringAuto(ptr);

            string args = "\"" + inputFileName + "\" input_pw " + pwd + " dump_data output pw.check dont_ask";

            pwd = null;

            if (RunPdftk(args) > 0)
            {
                state = OperationStates.WrongPassword;
            }

            if (File.Exists("pw.check"))
                File.Delete("pw.check");

            return state;
        }
Exemple #8
0
 public DBOperationMessage(OperationStates operationState)
 {
     _sw.Start();
     OperationState = operationState;
 }
        /// <summary>
        /// Encrypt a PDF document with a user or owner password.
        /// </summary>
        /// <param name="userPassword">Password to open the document. Set null if not needed.</param>
        /// <param name="ownerPassword">Password to edit the document. Set null if not needed.</param>
        /// <returns></returns>
        public OperationStates Encrypt(PDFDocument document, System.Security.SecureString userPassword, System.Security.SecureString ownerPassword,
            bool allowPrinting, bool allowDegradatedPrinting, bool allowModifyContents, bool allowAssembly,
            bool allowCopyContents, bool allowScreenReaders, bool allowModifyAnnotations,
            bool allowFillIn, bool allowAllFeatures, ref string outputFileName)
        {
            OperationStates state = OperationStates.Ok;

            if ((userPassword == null) && (ownerPassword == null)) return state;

            string allow = "";
            if (allowAllFeatures)
            {
                allow = "AllFeatures";
            }
            else
            {
                if (allowPrinting)
                {
                    allow += "Printing ";
                }
                if (allowDegradatedPrinting)
                {
                    allow += "DegradatedPrinting ";
                }
                if (allowModifyContents)
                {
                    allow += "ModifyContents ";
                    allowAssembly = true;
                }
                if (allowAssembly)
                {
                    allow += "Assembly ";
                }
                if (allowCopyContents)
                {
                    allow += "CopyContents ";
                    allowScreenReaders = true;
                }
                if (allowScreenReaders)
                {
                    allow += "ScreenReaders ";
                }
                if (allowModifyAnnotations)
                {
                    allow += "ModifyAnnotations ";
                    allowFillIn = true;
                }
                if (allowFillIn)
                {
                    allow += "FillIn ";
                }
            }

            string passwords = "";
            if (userPassword != null)
            {
                IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(userPassword);

                passwords += "user_pw " + System.Runtime.InteropServices.Marshal.PtrToStringAuto(ptr);
            }
            if (ownerPassword != null)
            {
                IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(ownerPassword);

                passwords += " owner_pw " + System.Runtime.InteropServices.Marshal.PtrToStringAuto(ptr);

                if (allow != "")
                {
                    passwords += " allow " + allow;
                }
            }

            if (passwords != "")
            {
                string inputFileName = document.FullName;
                GetValidOutputFileName(ref outputFileName, 1);

                string args = "\"" + inputFileName + "\" output \"" + outputFileName + "\" " + passwords;

                userPassword = null;
                ownerPassword = null;

                RunPdftk(args);
            }

            return state;
        }
Exemple #10
0
        /// <summary>
        /// Rotates pages from a pdf file and saves them into a new file.
        /// </summary>
        /// <param name="pageStart">First page to rotate</param>
        /// <param name="pageEnd">Last page to rotate</param>
        /// <param name="interval">Page interval (all, even, odd)</param>
        /// <param name="rotation">Rotation type</param>
        /// <param name="outputFileName">Path of the new file</param>
        /// <returns></returns>
        public OperationStates RotatePages(PDFDocument document, int pageStart, int pageEnd, PageIntervals interval,
            Rotations rotation, ref string outputFileName)
        {
            OperationStates state = OperationStates.Ok;

            if ((pageStart < 1) || (pageStart > document.NumberOfPages) ||
                (pageEnd < 1) || (pageEnd > document.NumberOfPages))
            {
                state = OperationStates.PageRangeOutOfDocument;
                return state;
            }

            string s_rotation = "";
            switch (rotation)
            {
                case Rotations.North:
                    s_rotation += "N";
                    break;
                case Rotations.South:
                    s_rotation += "S";
                    break;
                case Rotations.East:
                    s_rotation += "E";
                    break;
                case Rotations.West:
                    s_rotation += "W";
                    break;
                case Rotations.Left:
                    s_rotation += "L";
                    break;
                case Rotations.Right:
                    s_rotation += "R";
                    break;
                case Rotations.Down:
                    s_rotation += "D";
                    break;
            }

            string range = "";

            if (pageStart > 1)
            {
                range += "1-" + (pageStart - 1).ToString() + " ";
            }

            /* pdftk rotates the pages using the "cat" option, which catenates pages from a pdf
             * into another pdf file. If I specify an odd or even range (e.g. 1-10odd) only odd
             * or even pages within that range are catenated in the output file, but that's not
             * what I want. I need ALL pages to be present in the new file and only odd or even
             * pages be rotated. To achieve this I need to break the interval like below. */
            switch (interval)
            {
                case PageIntervals.Even:
                    for (int i = pageStart; i <= pageEnd; i++)
                    {
                        range += i.ToString();
                        range += (i % 2 == 0) ? s_rotation + " " : " ";
                    }
                    break;
                case PageIntervals.Odd:
                    for (int i = pageStart; i <= pageEnd; i++)
                    {
                        range += i.ToString();
                        range += (i % 2 != 0) ? s_rotation + " " : " ";
                    }
                    break;
                case PageIntervals.All:
                    for (int i = pageStart; i <= pageEnd; i++)
                    {
                        range += i.ToString() + s_rotation + " ";
                    }
                    break;
            }

            if (pageEnd < document.NumberOfPages)
            {
                range += " " + (pageEnd + 1).ToString() + "-" + document.NumberOfPages.ToString();
            }

            GetValidOutputFileName(ref outputFileName, 1);

            string args = "\"" + document.FullName + "\" cat " +
                range + " output \"" + outputFileName + "\"";

            RunPdftk(args);

            return state;
        }