// PDFConsole_Lib.cpp : Defines the entry point for the console application.
/*
This sample project illustrates how to print the Amyuni PDF Converter.
*/

#include "stdafx.h"
#include "PDFConsole_Lib.h"
#include "CdIntf600.h"

#pragma comment (lib, "CDIntf600.lib")

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


#define NoPrompt			0x00000001	// do not prompt for file name
#define UseFileName			0x00000002	// use file name set by SetDefaultFileName else use document name
#define RTFExport            0x8000000 //Export document to RTF format


#define STRLICENSETO			"Amyuni PDF Converter Evaluation"
#define STRACTIVATIONCODE		"07EFCDAB0100010025AFF1806AB8441306C5739F7DC52654D393BA9CECBA2ADE79E3762A66FFC3540A8A5F4A5811BE3204A0A439F5BA"
#define AMYUNI_PRINTER_NAME		"Amyuni PDF Converter"


// The one and only application object

CWinApp theApp;
using namespace std;
void PrintSomeThing()
{
	HDC hDC = CreateDC( "WINSPOOL" , AMYUNI_PRINTER_NAME, NULL, NULL);
	if(hDC == NULL)
	{
		CHAR buf[255];
		GetLastErrorMsg( buf, sizeof(buf) );
		::MessageBox( NULL, CString(buf), _T("Error in PrintSomeThing"),
					MB_OK | MB_ICONWARNING );
	}
	DOCINFO di;
	::ZeroMemory (&di, sizeof(DOCINFO));
	di.cbSize = sizeof(DOCINFO);

	di.lpszDocName = _T("myDocTitle");
	//di.lpszOutput = _T("c:\\temp\\amyuni.pdf");
	
	cout << "Before StartDoc()....." << endl;
	StartDoc(hDC, &di);
	StartPage(hDC);
	TextOut(hDC, 0, 200, _T("Hello World"), lstrlen (_T("Hello World")));
	

	TextOut(hDC, 0, 400, _T("Amyuni Technologies"), lstrlen (_T("Amyuni Technologies")));
	EndPage(hDC);
	EndDoc(hDC);
	cout << "After EndDoc()....." << endl;
	DeleteDC(hDC);


}
void SetPrinter()
{
	//Get a handle to Amyuni PDF Converter
	HANDLE hPrinter = DriverInit(AMYUNI_PRINTER_NAME);

	if ( NULL == hPrinter )
	{
		// cannot initialize new printer, get the error message from CDINTF
		CHAR buf[255];
		GetLastErrorMsg( buf, sizeof(buf) );
		// CString will convert error message to Unicode if necessary
		::MessageBox( NULL, CString(buf), _T("Error initializing printer"),
					MB_OK | MB_ICONWARNING );
	}

	//Set the Amyuni PDF COnverter as the default printer
	CDISetDefaultPrinter ( hPrinter );

	//Print Something
	cout << "Before PrintSomeThing()....." << endl;

	SetFileNameOptions ( hPrinter, NoPrompt | UseFileName );

	SetDefaultFileName (hPrinter,  _T("c:\\temp\\PrintSomeThing.pdf") );

	//This needs to be done before each print out
	//There is a 20 second time-out value.
	EnablePrinter ( hPrinter, STRLICENSETO, STRACTIVATIONCODE);

	PrintSomeThing();
	cout << "After PrintSomeThing()....." << endl;

	//Clean Up
	RestoreDefaultPrinter( hPrinter );
	SetFileNameOptions ( hPrinter, 0 );
	DriverEnd( hPrinter );	
}


//This function creates an RTF document at Runtime
void PrintPdfRtf() 
{
	//Get a handle to Amyuni PDF Converter
	HANDLE hPrinter = DriverInit(AMYUNI_PRINTER_NAME);


	//Check if retrieved printer handle
	if ( NULL == hPrinter )
	{
		// cannot initialize new printer, get the error message from CDINTF
		CHAR buf[255];
		GetLastErrorMsg( buf, sizeof( buf ) );

		// CString will convert error message to Unicode if necessary
		::MessageBox( NULL, CString(buf), _T( "Error initializing printer" ),
					MB_OK | MB_ICONWARNING );
	}


	//Set PDF Converter as Default Printer
	CDISetDefaultPrinter( hPrinter );

	//Produces both a PDF and RTF files
	SetDefaultFileName ( hPrinter, _T("c:\\temp\\PrintSomeThing_rtf_export.pdf") );


	/*////////////////////////////////////////////////////////////
	'RTF Format Integer Option for formatting the RTF output
		'0 – Advanced RTF
		'1 – Full RTF
		'2 – Formatted Text
		'3 – Non-formatted Text Only
	'Example: SetPrinterParamInt( m_hPrinter, _T("RTF Format"),  0);
	

	'////////////////////////////////////////////////////////////
	'Use Tabs Integer Option to replace tabs by spaces
		'0 – Do not replace
		'1 – Replace
	'Example: SetPrinterParamInt( m_hPrinter, _T("Use Tabs"),  0);

	'////////////////////////////////////////////////////////////////
	'    Optimization Level OptimizeLevel value
	'    No optimization 0
	'    Line optimization (Recommended) 1
	'    Paragraph optimization 2 
	'Example: SetPrinterParamInt( m_hPrinter, _T("RTF Optimization"),  1);
	*/

	SetPrinterParamInt( hPrinter, _T("RTF Format"),  0);
	SetPrinterParamInt( hPrinter, _T("Use Tabs"),  0);
	SetPrinterParamInt( hPrinter, _T("RTF Optimization"),  1);

	SetFileNameOptions ( hPrinter, NoPrompt | UseFileName | RTFExport );

	//Need to do this each time output is sent to the PDF Converter
	EnablePrinter ( hPrinter , STRLICENSETO  , STRACTIVATIONCODE);
	
	//print something to the PDF Converter
	PrintSomeThing();

	RestoreDefaultPrinter ( hPrinter );
	DriverEnd ( hPrinter );
	
}



void Pdf2rtf() 
{
	SetLicenseKeyA ( STRLICENSETO, STRACTIVATIONCODE);

	/*///////////////////////////
	Option						Option value (Hex)
	Advanced RTF:				0000
	Full RTF:					001
	Formatted Text only			0002
	Simple text					0003
	Simple text non-formatted	10003

	Optimization					Level 
	No optimization					0
	Line optimization (Recommended) 1
	Paragraph optimization			2
	*/


	/*
	Option						              Option value (Hex)
	Default :200 DPI, CCITT Fax compression   0x00C8000A
	Low : 150 DPI, CCITT Fax compression      0x0096000a
	Medium: 300 DPI, CCITT Fax compression    0x012C000A
	High : 600 DPI, CCITT Fax compression     0x0258000A
	*/


	if ( PDF2RTF( _T("c:\\temp\\PrintSomeThing_rtf_export.pdf"), _T(""), _T("c:\\temp\\sample_new.rtf"), RTFOPTION_ADVANCED, 1) == FALSE)
	{
				CHAR buf[255];
		GetLastErrorMsg( buf, sizeof( buf ) );

		// CString will convert error message to Unicode if necessary
		::MessageBox( NULL, CString(buf), _T( "Error initializing printer" ),
					MB_OK | MB_ICONWARNING );
	}
}


void PrintPDF()
{
	EXTDOCHANDLE doc;			// Handle to Document
	LPBYTE passWord = NULL;
	DocOpenA ( &doc, _T("C:\\temp\\sample.pdf"), passWord);
	DocPrintA (doc, "", 1, 1, 1);
	DocClose(doc);
}



int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		nRetCode = 1;
	}
	else
	{

		cout << "Start printing....." << endl;

		//SetPrinter();
		//PrintPdfRtf();
		//Pdf2rtf();
		PrintPDF();

		cout << "End printing....." << endl;
	
	}

	return nRetCode;
}
