Archive | Work RSS feed for this section

Extract the title of a PDF using C#

16 Jul

It’s been a while since I did a geeky work post (apologies to friends reading this).

Today I spent ages looking on the web for how to get the title of a PDF out of the file using C#. I couldn’t find anyone that had done it, so I opened the PDF in notepad and the title was just in there, in plain text, really easy to get at. So, here’s the code for the benefit of future googlers on the subject…

using System.Text.RegularExpressions;
using System.IO;
...
private static readonly Regex REGEX_PDFTITLE = new Regex("<< /Title \\((.*?)\\)", RegexOptions.Compiled);
private string GetPDFTitle(string pdfFilePath)
{
  using(StreamReader sr = new StreamReader(pdfFilePath))
  {
  string currentLine = null;
  while((currentLine = sr.ReadLine()) != null)
  {
  Match m = REGEX_PDFTITLE.Match(currentLine);
  if(m.Success)
  {
  return m.Groups[1].Value;
  }
  }
  return String.Empty;
  }
}

Enough Said

30 Jun

I saw this sign in Wharfedale Hospital the other day and it made me laugh. It doesn’t say “and it’s shit and we hate it” between the two sentences, but you can see that’s what the writer meant.

The First Helpdesk

9 May

A good-humoured client sent me this after a 90-minute training-and-troubleshooting conference call :-) .

Use C# to remove Outlook Calendar Duplicates

24 Sep

I keep all my email and calendar items on our Exchange server at work, and my work laptop and home PC both run Outlook against that server. Both instances of Outlook also sync up with my mobile phone using Nokia PC Suite.

When it works, it really works and I can see all my email and calendar items wherever I may be. But sometimes one Outlook can’t tell that an item that has been put on the phone by the other Outlook is the same item it should already know about and creates a duplicate when it syncs with the phone.

So I have created a very simple C# program to identify duplicate calendar items and delete them. To do this, I loop through every item in the calendar, and write the subject, location, start and end date into a hashtable as the key. If I come across the same combination of subject, location and times again, I delete the item instead. Easy.

Here is the code. You must have the Office Interop assemblies installed, and Microsoft Outlook Library referenced. You can install the Office Interop Assemblies separately if you are not able to modify your installation of Office.

using System; 
using System.Collections; 
using System.Diagnostics; 
using Microsoft.Office.Interop.Outlook; 
namespace RemoveOutlookCalendarDuplicates 
{ 
   class MainClass 
   { 
      [STAThread] 
      static void Main(string[] args) 
      { 
         Application olApp = new ApplicationClass(); 
         NameSpace outlookNS = 
            olApp.GetNamespace("MAPI"); 
         MAPIFolder calendarFolder = 
            outlookNS.GetDefaultFolder( 
            OlDefaultFolders.olFolderCalendar); 
         while(true) 
         { 
            bool somethingDeleted = false; 
            Hashtable uniqueItems = new Hashtable(); 
            foreach(AppointmentItem oi in calendarFolder.Items) 
            { 
               string uniqueKey = 
               String.Format("{0} {1} {2:yyyyMMddhhmmss} {3:yyyyMMddhhmmss}", 
               oi.Subject, oi.Location, oi.Start, oi.End); 
               if(uniqueItems.ContainsKey(uniqueKey)) 
               { 
                  Console.WriteLine("DELETED: " + uniqueKey); 
                  oi.Delete(); 
                  somethingDeleted = true; 
               } 
               else 
               { 
                  uniqueItems.Add(uniqueKey, String.Empty); 
               } 
            } 
            if(!somethingDeleted) break; 
         } 
         Console.WriteLine("** PRESS RETURN **"); 
         Console.ReadLine(); 
      } 
   } 
}
Follow

Get every new post delivered to your Inbox.

Join 79 other followers