Print PDF to Tiff
Submitted by andre.pereira on Tue, 2007-04-10 19:10.
Dear Sirs,
I am using PDFCreator to convert Doc's and Pdf's to Tiff images. When i try to print a PDF the convertion Stop's, when i manualy close the Acrobat Reader 8 window the process continues. Does anyone has an ideia why the Acrobat Reader 8 window stops the process.
Thank you in advance,
André
Hello Frank,
Hero goes the code in C#.
public class PdfCreator
{
public enum EnumFileType
{
Pdf,
Tiff
}
//Thread wait
private static AutoResetEvent _autoEventEndWaitPDFCreator = new AutoResetEvent(false);
private static TimeSpan _maxWaitTime = new TimeSpan(0,1,0);
private static bool _ready = false;
private static bool _errorReceived = false;
public PdfCreator()
{
}
public static bool ConvertFile(string sourcePath,string destinationPath,string fileName, EnumFileType fileType)
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("PDFCreator");
int timeRunning = 0;
bool isRunning = false;
if (processes.Length > 0)
{
isRunning = true;
}
//Waits 2 seconds
while (isRunning)
{
Thread.Sleep(new TimeSpan(0,0,3));
timeRunning += 2;
processes = System.Diagnostics.Process.GetProcessesByName("PDFCreator");
if ((timeRunning>3) && (processes.Length > 0))
{
foreach(Process process in processes)
{
process.Kill();
}
isRunning = false;
}
processes = System.Diagnostics.Process.GetProcessesByName("PDFCreator");
if (processes.Length == 0)
{
isRunning = false;
}
}
#region Start PdfCreator
PDFCreator.clsPDFCreatorError _pErr = new PDFCreator.clsPDFCreatorError();
PDFCreator.clsPDFCreator _PDFCreator = new PDFCreator.clsPDFCreator();
_PDFCreator.eError += new PDFCreator.__clsPDFCreator_eErrorEventHandler(_PDFCreator_eError);
_PDFCreator.eReady += new PDFCreator.__clsPDFCreator_eReadyEventHandler(_PDFCreator_eReady);
string parameters = "/NoProcessingAtStartup";
if (_PDFCreator.cProgramIsRunning == true)
{
_PDFCreator.cCloseRunningSession();
_PDFCreator.cStart(parameters, true);
}
_PDFCreator.cStart(parameters, false);
#endregion
#region Is Printable
if (!_PDFCreator.cIsPrintable(sourcePath + "//" + fileName))
{
//throw new Exception("Cant convert");
}
#endregion
#region PdfCreator Options
PDFCreator.clsPDFCreatorOptions _pdfCreatorOptions;
_pdfCreatorOptions = _PDFCreator.cOptions;
_pdfCreatorOptions.UseAutosave = 1;
_pdfCreatorOptions.UseAutosaveDirectory = 1;
_pdfCreatorOptions.AutosaveDirectory = destinationPath;
//0-Pdf, 5-Tiff
if (fileType == EnumFileType.Tiff)
{
_pdfCreatorOptions.AutosaveFormat = 5;
_pdfCreatorOptions.BitmapResolution = 600;
_pdfCreatorOptions.TIFFColorscount = 5;
_pdfCreatorOptions.AutosaveFilename = fileName.Remove(fileName.LastIndexOf("."), (fileName.Length - fileName.LastIndexOf(".")));
}
else
{
_pdfCreatorOptions.AutosaveFormat = 0;
_pdfCreatorOptions.AutosaveFilename = fileName.Remove(fileName.LastIndexOf("."), (fileName.Length - fileName.LastIndexOf(".")));
}
_PDFCreator.cOptions = _pdfCreatorOptions;
#endregion
#region PdfCreator Print File
_PDFCreator.cClearCache();
//Thread.Sleep(200);
_PDFCreator.cDefaultPrinter = "PDFCreator";
_PDFCreator.cPrintFile(sourcePath + "\\" + fileName);
_PDFCreator.cPrinterStop = false;
#endregion
#region Acrobat Reader
#endregion
_autoEventEndWaitPDFCreator.WaitOne(_maxWaitTime, false);
//Check if object was created
if (_PDFCreator != null)
{
if (_errorReceived)
{
_pErr = _PDFCreator.cError;
}
_PDFCreator.cPrinterStop = true;
_PDFCreator.cDefaultPrinter = "PDFCreator";
}
//Check if
if (_ready == false)
{
throw new Exception("Expirou o tempo");
}
#region Stop PdfCreator
if (_PDFCreator != null)
{
_PDFCreator.cPrinterStop = true;
_PDFCreator.cDefaultPrinter = "PDFCreator";
_PDFCreator.cClose();
System.Runtime.InteropServices.Marshal.ReleaseComObject(_PDFCreator);
System.Runtime.InteropServices.Marshal.ReleaseComObject(_pErr);
_pErr = null;
Thread.Sleep(200);
GC.Collect();
}
#endregion
if (_ready == false)
{
return false;
}
else
{
return true;
}
}
private static void _PDFCreator_eReady()
{
_ready=true;
_autoEventEndWaitPDFCreator.Set();
}
private static void _PDFCreator_eError()
{
_errorReceived = true;
_autoEventEndWaitPDFCreator.Set();
}
}