PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jul 14, 2024 9:58 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Tue Aug 03, 2010 5:30 pm 
Offline

Joined: Tue Aug 03, 2010 5:20 pm
Posts: 1
Hey guys,

I'm trying to print files from a directory. Basically, I have a web page that generates reports into PDFs and writes them to a shared directory. I also have a Windows service that monitors that directory. When a file is saved to that directory, it should print the file and then delete it. I'm running into some problems with the timing between the printing and the deleting of the files and I'm wondering if anyone can help. Here's what I have so far:

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using PdfSharp;
using PdfSharp.Pdf.Printing;
using System.Threading;

namespace InvoiceMonitor
{
    public partial class InvoiceMonitor : ServiceBase
    {
        private const string _targetDirectory = @"C:\report\";
        private const string _adobeDirectory = @"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe";
        private const string _printer1Name = "HP LaserJet M4345 MFP PS";

        public InvoiceMonitor()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            CreateWatcher();
        }

        protected override void OnStop()
        {
        }

        public void CreateWatcher()
        {
            // Create a new FileSystemWatcher.
            FileSystemWatcher invoiceWatcher = new FileSystemWatcher();

            // Set the filter to only catch PDF files.
            invoiceWatcher.Filter = "*.pdf";

            // Subscribe to the Created event.
            invoiceWatcher.Created += new FileSystemEventHandler(watcher_FileCreated);

            // Set the path
            invoiceWatcher.Path = _targetDirectory;

            // Enable the FileSystemWatcher events.
            invoiceWatcher.EnableRaisingEvents = true;
        }

        public void watcher_FileCreated(object sender, FileSystemEventArgs e)
        {
            try
            {
                PrintDocument(_targetDirectory, e.FullPath);
                DeleteFile(_targetDirectory, e.FullPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void PrintDocument(string targetDirectory, string fileName)
        {
            if (Directory.Exists(targetDirectory))
            {
                FileInfo file = new FileInfo(fileName);

                if (file.Exists)
                {
                    PdfFilePrinter.AdobeReaderPath = _adobeDirectory;
                    PdfFilePrinter printer = new PdfFilePrinter(fileName, _printer1Name);
                    printer.Print();
                }
                else
                {
                    // Do some logging.
                }
            }
            else
            {
                // Do some logging.
            }
        }

        private void DeleteFile(string targetDirectory, string fileName)
        {
            if (Directory.Exists(targetDirectory))
            {
                FileInfo file = new FileInfo(fileName);

                if (file.Exists)
                {
                    file.Delete();
                }
                else
                {
                    // Do some logging.
                }
            }
            else
            {
                // Do some logging
            }
        }
    }
}


I think what happens is that it doesn't have enough time to set up the print job before the file gets removed from the directory. Could anyone provide any advice/guidance on solving my problem? Thanks


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 02, 2010 8:45 pm 
Offline

Joined: Thu Dec 02, 2010 8:41 pm
Posts: 2
Hey astraldragon,

I'm facing a similar issue and was curious if you were able to get this working. I'm developing a windows service that accepts data and builds a PDF form...from there I need it to print automatically in the warehouse. When I build it using a windows form, everything works fine, but when I then move it into the windows service I can't seem to get it to work. I'm seeing in other forums that it could be an issue with the printer names not being there for all user accounts, however I'm running the service as my username and trying to send the pdf to a local printer.

Any help you can provide would be greatly appreciated.

Thanks!


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 29 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