PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Jun 04, 2024 8:15 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Tue Apr 27, 2010 3:31 pm 
Offline

Joined: Wed Apr 21, 2010 7:44 pm
Posts: 4
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 121 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group