scannerpage.cpp

来自「把doc文档转成pdf后刻录成CD,用VC++开发,用了Nero的SDK和CXI」· C++ 代码 · 共 267 行

CPP
267
字号
// ScannerPage.cpp : implementation file
//

#include "stdafx.h"
#include "Scan2PDF.h"
#include "ScannerPage.h"
#include "PDFList.h"

#include "PDFLib/PDFLib.hpp"
#pragma comment(lib, "PDFLib/PDFLib.lib")

#include "CxImage/xImage.h"
#pragma comment(lib, "CxImage/CxImage.lib")
/*
#pragma comment(lib, "CxImage/Tiff.lib")
#pragma comment(lib, "CxImage/PNG.lib")
#pragma comment(lib, "CxImage/JPEG.lib")
#pragma comment(lib, "CxImage/JASPER.lib")
#pragma comment(lib, "CxImage/ZLib.lib")
*/

#include "Scan2PDFDefs.h"

#define SIZE_OF_CD			700		//700 MB


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CScannerPage property page

IMPLEMENT_DYNCREATE(CScannerPage, CPropertyPageEx)

CScannerPage::CScannerPage() : CPropertyPageEx(CScannerPage::IDD, 0, 
										IDS_SCANNER_HEADER, IDS_SCANNER_SUBHEADER)
										
{
	//{{AFX_DATA_INIT(CScannerPage)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	ImageNum=0;
	PDFTotalSize=0;

	//get temp directory
	GetTempPath(1024, TempDir);
}

CScannerPage::~CScannerPage()
{
}

void CScannerPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPageEx::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CScannerPage)
	DDX_Control(pDX, IDC_PIE_CHART, m_Chart);
	DDX_Control(pDX, IDC_PDF_PROGRESS, m_Progress);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CScannerPage, CPropertyPageEx)
	//{{AFX_MSG_MAP(CScannerPage)
	ON_BN_CLICKED(IDC_SELECT_SCANNER, OnSelectScanner)
	ON_BN_CLICKED(IDC_CONVERT_TO_PDF, OnConvertToPDF)
	ON_BN_CLICKED(IDC_SCAN_NEW_DOCUMENT, OnScanNewDocument)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_PDF_LIST, OnPDFList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScannerPage message handlers

void CScannerPage::OnSelectScanner() 
{
	CTwain::SelectSource();	
}

BOOL CScannerPage::OnSetActive() 
{
	CPropertySheetEx* pParent=(CPropertySheetEx*) GetParent();
	pParent->SetWizardButtons(/*PSWIZB_BACK | */PSWIZB_NEXT);

	//set the size of project
	PDFTotalSize=theConf.m_ProjectSize;

	if (theConf.m_ProjectSize==0)
	{
		m_Chart.AddPiece(RGB(0, 0, 255), RGB(0, 0, 0), 0, "Free");
		m_Chart.AddPiece(RGB(255, 0, 0), RGB(0, 0, 0), 360, "Full");
	}
	else
	{
		float full=360.0 * PDFTotalSize /1024.0 /*KB*/ /1024.0 /*MB*/ / SIZE_OF_CD;
		
		m_Chart.Reset();
		m_Chart.AddPiece(RGB(0, 0, 255), RGB(0, 0, 0), (int) (360 - full), "Free");
		m_Chart.AddPiece(RGB(255, 0, 0), RGB(0, 0, 0), (int) full, "Full");
	}

	return CPropertyPageEx::OnSetActive();
}


void CScannerPage::OnScanNewDocument() 
{
	CTwain::Acquire(TWCPP_ANYCOUNT);
}

BOOL CScannerPage::OnInitDialog() 
{
	CPropertyPageEx::OnInitDialog();
	
	CString ChartTitle;
	ChartTitle.Format("Capacity (%dMB)", SIZE_OF_CD);
	m_Chart.SetTitle(ChartTitle);

	InitTwain(m_hWnd);

	m_Chart.AddPiece(RGB(0, 0, 255), RGB(0, 0, 0), 0, "Free");
	m_Chart.AddPiece(RGB(255, 0, 0), RGB(0, 0, 0), 360, "Full");

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CScannerPage::SetImage(HANDLE hBitmap,TW_IMAGEINFO& info)
{
	//TempDir="C:\Temp\"

	if (hBitmap)
	{
		CString FileName;
		FileName.Format("%s%d.tif", TempDir, ImageNum);
		ImageNum++;

		CxImage newimage;
		newimage.CreateFromHANDLE(hBitmap);
		newimage.Save(FileName, CXIMAGE_FORMAT_TIF);
	}
}

void CScannerPage::OnDestroy() 
{
	CPropertyPageEx::OnDestroy();
	
	CTwain::ReleaseTwain();	
}

BOOL CScannerPage::PreTranslateMessage(MSG* pMsg) 
{
	if (CTwain::SourceEnabled()) 
		return CTwain::ProcessMessage(*pMsg);			//process twain
	else
		return CPropertyPageEx::PreTranslateMessage(pMsg);
}

void CScannerPage::OnConvertToPDF() 
{
	if (ImageNum==0)
		return;
	
	//Save the PDF file name
	CFileDialog dlg (FALSE, _T("pdf"), (LPCTSTR) NULL, OFN_ENABLESIZING | OFN_EXPLORER |
						OFN_LONGNAMES | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | 
						OFN_CREATEPROMPT, _T("Portable Document Format (*.pdf)|*.pdf|"), 
						this);
	
	dlg.m_ofn.lpstrInitialDir = theConf.m_ProjectName;
	dlg.m_ofn.lpstrTitle = _T("Save As PDF");
	
	if (dlg.DoModal()==IDOK)
	{
		CString pdfFileName=dlg.GetPathName();

		theConf.m_PDFs.push_back(pdfFileName);

		//open the PDF file
		PDFlib pdf;
		pdf.set_parameter("compatibility", "1.4");	//Compatible for Acrobat 5
		
		if (pdf.open_file(pdfFileName.GetBuffer(0))==-1)
		{
			printf("Can not create PDF file.\n"
				"May be file opened by another process");
			return;
		}
		
		pdf.set_info("Creator", "ScanWizard");
		pdf.set_info("Author", "A. Riazi");
		pdf.set_info("Title","Image Files Converted to PDF");
		pdf.set_info("Subject","ScanWizard");
		
		m_Progress.SetRange32(0, ImageNum);

		CString ImageFileName;

		for (int i=0; i<ImageNum; i++)
		{
			ImageFileName.Format("%s%d.tif", TempDir, i);
			
			
			int image = pdf.load_image("auto", ImageFileName.GetBuffer(0), "");
			
			if (image != -1) 
			{
				pdf.begin_page_ext(10, 10, "");
				pdf.fit_image(image, 0.0, 0.0, "adjustpage");
				pdf.close_image(image);
				pdf.end_page_ext("");
			}

			m_Progress.SetPos(i+1);

			/*
			int image = pdf.open_image_file("tiff", ImageFileName.GetBuffer(0), "", 0);
			if (image != -1)
			{

				pdf.begin_page(a4_width, a4_height);
				pdf.place_image(image, 0.0, 0.0, 1.0);
				pdf.close_image(image);
				pdf.end_page();
			}
			*/
		}

		pdf.close();

		//delete the temprory image files
		for (int j=0; j<ImageNum; j++)
		{
			CString ImageFileName;
			ImageFileName.Format("%s%d.tif", TempDir, j);

			DeleteFile(ImageFileName);
		}

		//update the Pie Chart
		CFileStatus fs;
		CFile::GetStatus(pdfFileName, fs);

		PDFTotalSize+=fs.m_size;
		theConf.m_ProjectSize+=fs.m_size;

		float full=360.0 * PDFTotalSize /1024.0 /*KB*/ /1024.0 /*MB*/ / SIZE_OF_CD;
		
		m_Chart.Reset();
		m_Chart.AddPiece(RGB(0, 0, 255), RGB(0, 0, 0), (int) (360 - full), "Free");
		m_Chart.AddPiece(RGB(255, 0, 0), RGB(0, 0, 0), (int) full, "Full");

		//reset the counter
		ImageNum=0;
	}
}

void CScannerPage::OnPDFList() 
{
	CPDFList list;
	list.DoModal();	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?