Not sure if this is allowed and if not please feel free to remove this thread!
I just want to share how easy it is to create a little PDF pages count program using PDFSharp!
#region pdfSharpPageCount using PDFSharp library // // Authors: // Chanh Ong // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. #endregion using System; using System.Diagnostics; using System.IO; using PdfSharp; using PdfSharp.Drawing; using PdfSharp.Pdf; using PdfSharp.Pdf.IO;
namespace pdfSharpPageCount { class pdfSharpPageCount { static void Main(string[] args) { string filepath; filepath = ".\\"; if (args.Length > 0 ) filepath = args[0]; Console.WriteLine("By: Chanh Ong\n"); getFiles(filepath); }
public static void getFiles(string sfolder) { string fullFilePath, ext, sb, pages, sPath; // Make a reference to a directory. DirectoryInfo Dinfo = new DirectoryInfo(sfolder);
// Get a reference to each file in that directory. FileInfo[] fileInfoArray = Dinfo.GetFiles(); sPath = ""; if (sfolder != null) sPath = Path.GetFullPath(sfolder) ; if (sPath.Substring(sPath.Length-1) != "\\") sPath = sPath + "\\"; using (StreamWriter outFile = new StreamWriter(sPath + @"pagecount.txt"))
// Display the names of the files. foreach (FileInfo oneFileInfo in fileInfoArray) { ext = oneFileInfo.Extension.ToLower(); if (ext == ".pdf") { fullFilePath = sPath + oneFileInfo.Name; pages = GetNoOfPDFPages(fullFilePath).ToString(); sb = oneFileInfo.Name + "," + pages; outFile.WriteLine(sb.ToString()); Console.WriteLine("This \"" + fullFilePath + "\" file has " + pages + " pages"); } } }
public static int GetNoOfPDFPages(string FileName) { PdfDocument document = PdfReader.Open(FileName); int count = document.PageCount; return count; } } }
_________________ http://ongetc.com
|