//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- #include #include #include #include using namespace std; using namespace pdftron; using namespace PDF; #include #include #include // Return HDC for the default printer HDC GetDefaultPrinterDC(void); /** * The following sample is a simplest C/C++ program used to illustrate how to print * PDF document using currently selected default printer. In this sample, PDFDraw class * is used to send rasterized data to printer. */ void main () { // HINSTANCE hInstance; // HWND hwnd; PRINTDLG pd; pd.lStructSize = sizeof(pd); //pd.hwndOwner = hwnd; pd.hDevMode = NULL; pd.hDevNames = NULL; pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC | PD_HIDEPRINTTOFILE | PD_NOSELECTION | PD_NOPAGENUMS; //pd.Flags = PD_NOPAGENUMS; pd.nCopies = 1; pd.nFromPage = 0xFFFF; pd.nToPage = 0xFFFF; pd.nMinPage = 1; pd.nMaxPage = 0xFFFF; //pd.hInstance = hInstance; pd.hDC = ::GetDefaultPrinterDC(); // pd.hDC = CreateDC("winspool", "\\\\pdftron\\HLPJ9300", 0, 0); DOCINFO docinfo; memset(&docinfo, 0, sizeof(DOCINFO)); docinfo.cbSize = sizeof(docinfo); docinfo.lpszDocName = "My Test"; docinfo.fwType = 0; docinfo.lpszDatatype = (LPSTR) 0; docinfo.lpszOutput = (LPSTR)0; int nError = StartDoc(pd.hDC, &docinfo); if(nError == SP_ERROR) { MessageBox(NULL, "Error", "Error, Printing", MB_OK | MB_ICONEXCLAMATION); return ; } // Start with PDFNew related stuff ----------------------- PDFNet::Initialize(); PDFNet::SetResourcesPath("../../../resources"); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; PDFDoc doc((input_path + "tiger.pdf").c_str()); doc.InitSecurityHandler(); // PDFNet includes two different rasterizer implementations. // // The two implementations offer a tradeoff between print // speed and accuracy/quality, as well as a tradeoff between // vector and raster output. // // e_GDIPlus rasterizer can be used to render the page // using Windows GDI+, whereas e_BuiltIn rasterizer can // be used to render bitmaps using platform-independent // graphics engine (in this case images are always converted // to bitmap prior to printing). PDFDraw pdfdraw; pdfdraw.SetRasterizerType(PDFRasterizer::e_GDIPlus); // pdfdraw.SetRasterizerType(PDFRasterizer::e_BuiltIn); // Note: If you would like to rasterize page at high resolutions (e.g. more // than 600 DPI), you should use PDFRasterizer or GDI+ rasterizer. pdfdraw.SetDPI(200); // Set DPI (Dots Per Inch). for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next()) { nError = StartPage(pd.hDC); if(nError == SP_ERROR) { MessageBox(NULL, "Error", "Error, Printing", MB_OK | MB_ICONEXCLAMATION); AbortDoc(pd.hDC); return ; } // Obtain the size of printer page (in pixels). PDF::Rect r; bool use_physical_page = false; if (use_physical_page){ // Use the physical page for printing. Note: the physical page is almost always // greater than the printable area of the page, and never smaller. r.x1 = -GetDeviceCaps(pd.hDC, PHYSICALOFFSETX); r.y1 = -GetDeviceCaps(pd.hDC, PHYSICALOFFSETY); r.x2 = GetDeviceCaps(pd.hDC, PHYSICALWIDTH) - GetDeviceCaps(pd.hDC, PHYSICALOFFSETX); r.y2 = GetDeviceCaps(pd.hDC, PHYSICALHEIGHT) - GetDeviceCaps(pd.hDC, PHYSICALOFFSETY); } else { // use the printable area of the page for printing. r.x1 = r.y1 = 0; r.x2 = GetDeviceCaps(pd.hDC, PHYSICALWIDTH) - GetDeviceCaps(pd.hDC, PHYSICALOFFSETX) * 2; r.y2 = GetDeviceCaps(pd.hDC, PHYSICALHEIGHT) - GetDeviceCaps(pd.hDC, PHYSICALOFFSETY) * 2; } // Convert page rectange dimensions to points. One PDF point is 1/72 of an inch. // LOGPIXELSX/Y returns number of pixels per logical inch along the screen width/height. double conv_x2pts = 72.0/GetDeviceCaps(pd.hDC, LOGPIXELSX); double conv_y2pts = 72.0/GetDeviceCaps(pd.hDC, LOGPIXELSY); r.x1 *= conv_x2pts; r.y1 *= conv_y2pts; r.x2 *= conv_x2pts; r.y2 *= conv_y2pts; pdfdraw.DrawInRect(itr.Current(), pd.hDC, r); // Print the page EndPage(pd.hDC); } PDFNet::Terminate (); // Done with PDFNew related stuff -------------------- EndDoc(pd.hDC); DeleteDC(pd.hDC); } HDC GetDefaultPrinterDC(void) { char szPrinter[80]; char *szDevice, *szDriver, *szOutput; GetProfileString("WINDOWS", "DEVICE", ",,,", szPrinter, 80); szDevice = strtok(szPrinter, ","); szDriver = strtok(NULL, ","); szOutput = strtok(NULL, ","); if ( !szDevice || !szDriver || !szOutput ) return 0; else return CreateDC( szDriver, szDevice, szOutput, NULL ); }