Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hey guys,

im am trying do send a pdf file to a multi-functional kyocera network printer (kyocera taskalfa 400ci) in my company. The printer is not shared over the network, but it is included as a local printer (kyocera printer driver, protocoll is raw, gdi enable) for security reasons. I can print files without any problems on my own.

Now I want to print automatically generated pdf files. I tried different solution for example like printing via adopbe reader but i dont want to use other software.

I found this example to send data directly to a gdi printer:[^]

I used this code:
BOOL RawDataToPrinter(_WSTRING szPrinterName, LPBYTE lpData, DWORD dwCount) { 
    BOOL       bStatus = FALSE;
    HANDLE     hPrinter = NULL;
    DOC_INFO_1 DocInfo;
    DWORD      dwJob = 0L;
    DWORD      dwBytesWritten = 0L;

    // Open a handle to the printer. 
    bStatus = OpenPrinter( &szPrinterName[0], &hPrinter, NULL );
    if (bStatus) {
        // Fill in the structure with info about this "document." 
        DocInfo.pDocName = (LPTSTR)_T("test");
        DocInfo.pOutputFile = NULL;
        DocInfo.pDatatype = (LPTSTR)_T("RAW");

        // Inform the spooler the document is beginning. 
        dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo );
        if (dwJob > 0) {
            // Start a page. 
            bStatus = StartPagePrinter( hPrinter );
            if (bStatus) {
                // Send the data to the printer. 
                bStatus = WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten);
                EndPagePrinter (hPrinter);
            // Inform the spooler that the document is ending. 
            EndDocPrinter( hPrinter );
        // Close the printer handle. 
        ClosePrinter( hPrinter );
	// Check to see if correct number of bytes were written. 
    if (!bStatus || (dwBytesWritten != dwCount)) {
        bStatus = FALSE;
    } else {
        bStatus = TRUE;

    return bStatus;

long getFileSize(FILE *file) {
	long lCurPos, lEndPos;
	lCurPos = ftell(file);
	fseek(file, 0, 2);
	lEndPos = ftell(file);
	fseek(file, lCurPos, 0);
	return lEndPos;

bool sendFile(_WSTRING szFileName, _WSTRING szPrinterName) {
	BYTE *fileBuf;          // Pointer to our buffered data
	FILE *file = NULL;      // File pointer
	// Open the file in binary mode using the "rb" format string
	// This also checks if the file exists and/or can be opened for reading correctly
	if ((file = _wfopen(szFileName.c_str(), L"rb")) == NULL)
		printf("Could not open specified file \n");
		printf("File opened successfully \n");

	// Get the size of the file in bytes
	long fileSize = getFileSize(file);
	// Allocate space in the buffer for the whole file
	fileBuf = new BYTE[fileSize];

	// Read the file in to the buffer
	fread(fileBuf, fileSize, 1, file);
	if(RawDataToPrinter(szPrinterName, fileBuf, fileSize)) {
		wprintf(L"print successfull [%s] \n", szFileName.c_str());	
	} else {
		printf("print not successfull \n");	
    return 0;

After every print job i get "print successfull". The printer seems to be find and it can also send data ( bStatus && (dwBytesWritten == dwCount --> TRUE), but nothing will be printed.

Any ideas where i should find the error?

Thanks for your help
Best regards
Chris Meech 29-Aug-12 11:02am    
Just guessing, but it seems like you are sending the PDF directly to the printer and bypassing all printing operations. Some software, ie. Adobe or others, will still be required to render the PDF in order to print it.

As Chris Meech said in the comment(he guessed), I am not guessing, you send pdf file content, I can see in your code. Printer does not know how to process a pdf file. Either you will have to parse and create the printable information or as Chris Meech says, you will have use any software that can perform it.

after creating the pdf file you can open your with the default pdf reader and print your data.
Share this answer
Member 4462457 4-Apr-13 14:44pm    
I want to use WritePrinter API
How do I specify the Location of Text to Printed...I mean is there any Equivalent of VB6 Functions Currentx and CurrentY
you use rawprinterhelper class to send data or document to your printer
refer this
How to send raw data to a printer by using Visual C# .NET
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900