/// <summary> /// Get the informations from the pdf file /// </summary> /// <param name="pdfFilePath">Path of the pdf file to get information from</param> private void LoadInfo(string pdfFilePath) { PDFFileInfo info = PDFActions.GetPDFFileInfo(pdfFilePath); this.NumberOfPages = info.NumberOfPages; this.PageLabelStart = info.PageLabelStart; this.PageRanges = new List <string>(); this.HasInfo = true; if (this.NumberOfPages == 0) { this.HasInfo = false; } }
/// <summary> /// Gets informations about the pdf file specified by <paramref name="path"/> /// using the "dump_data" function of pdftk. /// </summary> /// <param name="path">Full path of the file that you want to get information from.</param> /// <returns>PDFFileInfo object storing basic information about PDF document.</returns> public static PDFFileInfo GetPDFFileInfo(string path) { PDFFileInfo info = new PDFFileInfo(); //Creates a process for pdftk and sets its parameters. Process p = new Process(); //Handles possible errors of pdftk. try { p.StartInfo.FileName = "pdftk.exe"; p.StartInfo.WorkingDirectory = BASE_DIR; p.StartInfo.CreateNoWindow = true; p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //Sets pdftk command line arguments. //You must use quotation marks to specify command line arguments that have //spaces inside them, or they will be considered as different arguments. p.StartInfo.Arguments = "\"" + path + "\" dump_data output \"" + DUMP_DATA_FILE + "\" dont_ask"; p.Start(); p.WaitForExit(); //Console.WriteLine("pdftk exit code: " + p.ExitCode.ToString()); p.Close(); if (File.Exists(DUMP_DATA_FILE)) { info.FullName = path; //Searches the informations inside pdftk dump_data file. using (StreamReader sr = new StreamReader(DUMP_DATA_FILE)) { String line; while ((line = sr.ReadLine()) != null) { if (line.StartsWith("NumberOfPages")) { info.NumberOfPages = short.Parse(line.Remove(0, 15)); } if (line.StartsWith("PageLabelStart")) { info.PageLabelStart = short.Parse(line.Remove(0, 16)); } } } // PageLabelStart is used to get the effective number of pages, // so it must be greater than 0 if (info.PageLabelStart == 0) info.PageLabelStart = 1; } } catch { } finally { if (File.Exists(DUMP_DATA_FILE)) File.Delete(DUMP_DATA_FILE); } return info; }