// PDFCompDemoView.cpp : implementation of the CPDFCompDemoView class // #include "stdafx.h" #include "PDFCompDemo.h" #include "PDFCompDemoDoc.h" #include "PDFCompDemoView.h" #include "PDFCompSetDlg.h" #include "PDFOptions.h" #include "LEADRaster.h" #include #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CPDFCompDemoView IMPLEMENT_DYNCREATE(CPDFCompDemoView, CFormView) BEGIN_MESSAGE_MAP(CPDFCompDemoView, CFormView) //{{AFX_MSG_MAP(CPDFCompDemoView) ON_WM_SIZE() ON_COMMAND(ID_PDFCOMP_OPTIONS_ADD, OnPdfcompOptionsAdd) ON_COMMAND(ID_PDFCOMP_OPTIONS_MOD, OnPdfcompOptionsMod) ON_UPDATE_COMMAND_UI(ID_PDFCOMP_OPTIONS_MOD, OnUpdatePdfcompOptionsMod) ON_UPDATE_COMMAND_UI(ID_PDFCOMP_OPTIONS_ADD, OnUpdatePdfcompOptionsAdd) ON_COMMAND(ID_PDFCOMP_OPTIONS_DEL, OnPdfcompOptionsDel) ON_UPDATE_COMMAND_UI(ID_PDFCOMP_OPTIONS_DEL, OnUpdatePdfcompOptionsDel) ON_COMMAND(ID_FILE_SAVEALL, OnFileSaveall) ON_COMMAND(ID_PDFCOMP_OPTIONS_ADVANCE, OnPdfcompOptionsAdvance) ON_WM_CLOSE() ON_WM_DESTROY() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CPDFCompDemoView construction/destruction CPDFCompDemoView::CPDFCompDemoView() : CFormView(CPDFCompDemoView::IDD) { //{{AFX_DATA_INIT(CPDFCompDemoView) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // TODO: add construction code here } CPDFCompDemoView::~CPDFCompDemoView() { } void CPDFCompDemoView::DoDataExchange(CDataExchange* pDX) { CFormView::DoDataExchange(pDX); //{{AFX_DATA_MAP(CPDFCompDemoView) DDX_Control(pDX, IDC_LEADRASTERVIEW1, m_LEADRasterView); //}}AFX_DATA_MAP } BOOL CPDFCompDemoView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CFormView::PreCreateWindow(cs); } void CPDFCompDemoView::OnInitialUpdate() { CFormView::OnInitialUpdate(); CPDFCompDemoDoc * pDoc = (CPDFCompDemoDoc *)GetDocument(); m_LEADRasterView.GetRaster().SetBitmap(pDoc->m_pLEADRaster->Bitmap); int xExt = (int)pDoc->m_pLEADRaster->BitmapWidth; int yExt = (int)pDoc->m_pLEADRaster->BitmapHeight; // Make the CLEADRasterView control to fit the bitmap and disable CLEADRasterView's scrolling // Make the form scroll if the window becomes smaller than the bitmap SetScrollSizes(MM_TEXT,CSize(xExt,yExt)); // Make the window fit the image GetParentFrame()->RecalcLayout(); ResizeParentToFit(); // now disable the scroll bars SetScrollSizes(MM_TEXT,CSize(1,1)); m_LEADRasterView.SetBackErase(FALSE); DrawPanel(this, pDoc->m_pLEADRaster, pDoc->m_PDFCompSettings); } ///////////////////////////////////////////////////////////////////////////// // CPDFCompDemoView diagnostics #ifdef _DEBUG void CPDFCompDemoView::AssertValid() const { CFormView::AssertValid(); } void CPDFCompDemoView::Dump(CDumpContext& dc) const { CFormView::Dump(dc); } CPDFCompDemoDoc* CPDFCompDemoView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPDFCompDemoDoc))); return (CPDFCompDemoDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CPDFCompDemoView message handlers void CPDFCompDemoView::OnSize(UINT nType, int cx, int cy) { CFormView::OnSize(nType, cx, cy); if (IsWindow(m_LEADRasterView.m_hWnd)) { m_LEADRasterView.SetBackErase(FALSE); m_LEADRasterView.MoveWindow(0, 0, cx, cy); m_LEADRasterView.SetDoubleBuffer(TRUE); } } void CPDFCompDemoView::OnPdfcompOptionsAdd() { CPDFCompDemoDoc* pDoc = GetDocument(); CPDFCompSetDlg SettingDlg; // Get PDF Compressor Setting if(pDoc->m_PDFCompSettings.bAdded) { SettingDlg.m_nBKThreshold = pDoc->m_PDFCompSettings.nBKThreshold ; SettingDlg.m_nCleanSize = pDoc->m_PDFCompSettings.nCleanSize; SettingDlg.m_nCLRThreshold = pDoc->m_PDFCompSettings.nCLRThreshold; SettingDlg.m_nCombThreshold = pDoc->m_PDFCompSettings.nCombThreshold; SettingDlg.m_nSegQuality = pDoc->m_PDFCompSettings.nSegQuality ; SettingDlg.m_bAdded = pDoc->m_PDFCompSettings.bAdded; SettingDlg.m_nPDFComboSel = pDoc->m_PDFCompSettings.nPDFComboSel; SettingDlg.m_nImgQComboSel = pDoc->m_PDFCompSettings.nImgQComboSel; SettingDlg.m_nPageOrder = pDoc->m_PDFCompSettings.nPageNumber; SettingDlg.m_bNOMRC = pDoc->m_PDFCompSettings.m_bNOMRC; } else { SettingDlg.m_bAdded = 0; } int nRet = SettingDlg.DoModal(); // Set PDF Compressor Setting if(nRet != 2) { if(SettingDlg.m_bAdded) { pDoc->m_PDFCompSettings.nBKThreshold = SettingDlg.m_nBKThreshold; pDoc->m_PDFCompSettings.nCleanSize = SettingDlg.m_nCleanSize; pDoc->m_PDFCompSettings.nCLRThreshold = SettingDlg.m_nCLRThreshold; pDoc->m_PDFCompSettings.nCombThreshold = SettingDlg.m_nCombThreshold; pDoc->m_PDFCompSettings.nSegQuality = SettingDlg.m_nSegQuality; pDoc->m_PDFCompSettings.bAdded = SettingDlg.m_bAdded; pDoc->m_PDFCompSettings.nPDFComboSel = SettingDlg.m_nPDFComboSel; pDoc->m_PDFCompSettings.nImgQComboSel = SettingDlg.m_nImgQComboSel; pDoc->m_PDFCompSettings.nPageNumber = SettingDlg.m_nPageOrder; pDoc->m_PDFCompSettings.m_bNOMRC = SettingDlg.m_bNOMRC; } else { pDoc->m_PDFCompSettings.bAdded = FALSE; } CPDFCompDemoApp * pApp = (CPDFCompDemoApp *)AfxGetApp(); POSITION DocTemplatePos = pApp->GetFirstDocTemplatePosition(); CDocTemplate* pDocTemplate = pApp->GetNextDocTemplate(DocTemplatePos); POSITION DocPos = pDocTemplate->GetFirstDocPosition(); while (DocPos!=NULL) { CPDFCompDemoDoc* pDoc = (CPDFCompDemoDoc*)pDocTemplate->GetNextDoc(DocPos); if (!pDoc) break; POSITION pos = pDoc->GetFirstViewPosition(); while (pos != NULL) { CPDFCompDemoView* pMyView = (CPDFCompDemoView *)pDoc->GetNextView(pos); //pMyView->m_LEADRasterView; DrawPanel(pMyView, pDoc->m_pLEADRaster, pDoc->m_PDFCompSettings); } } } } void CPDFCompDemoView::OnPdfcompOptionsMod() { CPDFCompDemoDoc* pDoc = GetDocument(); OnPdfcompOptionsAdd(); } void CPDFCompDemoView::OnUpdatePdfcompOptionsMod(CCmdUI* pCmdUI) { CPDFCompDemoDoc* pDoc = GetDocument(); if(pDoc->m_PDFCompSettings.bAdded) pCmdUI->Enable(TRUE); else pCmdUI->Enable(FALSE); } void CPDFCompDemoView::OnUpdatePdfcompOptionsAdd(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here CPDFCompDemoDoc* pDoc = GetDocument(); if(pDoc->m_PDFCompSettings.bAdded) pCmdUI->Enable(FALSE); else pCmdUI->Enable(TRUE); } void CPDFCompDemoView::OnPdfcompOptionsDel() { CPDFCompDemoDoc* pDoc = GetDocument(); CPDFCompDemoApp * pApp = (CPDFCompDemoApp *)AfxGetApp(); // TODO: Add your command handler code here pDoc->m_PDFCompSettings.bAdded = FALSE; pApp->SetPageOrder(pDoc->m_PDFCompSettings.nPageNumber, FALSE, TRUE, -1); POSITION DocTemplatePos = pApp->GetFirstDocTemplatePosition(); CDocTemplate* pDocTemplate = pApp->GetNextDocTemplate(DocTemplatePos); POSITION DocPos = pDocTemplate->GetFirstDocPosition(); while (DocPos!=NULL) { CPDFCompDemoDoc* pDoc = (CPDFCompDemoDoc*)pDocTemplate->GetNextDoc(DocPos); if (!pDoc) break; POSITION pos = pDoc->GetFirstViewPosition(); while (pos != NULL) { CPDFCompDemoView* pMyView = (CPDFCompDemoView *)pDoc->GetNextView(pos); DrawPanel(pMyView, pDoc->m_pLEADRaster, pDoc->m_PDFCompSettings); } } } void CPDFCompDemoView::OnUpdatePdfcompOptionsDel(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here CPDFCompDemoDoc* pDoc = GetDocument(); if(pDoc->m_PDFCompSettings.bAdded) pCmdUI->Enable(TRUE); else pCmdUI->Enable(FALSE); } void CPDFCompDemoView::ApplySort( pPDFCOMPSETTENGS pPDFSettings, INT nSize ) { PDFCOMPSETTENGS HoldPDFSettings; INT x, y; for(x = 0; x < nSize; x++) { for(y = 0; y < nSize-1; y++) { if(pPDFSettings[y].nPageNumber > pPDFSettings[y+1].nPageNumber) { HoldPDFSettings = pPDFSettings[y+1]; pPDFSettings[y+1] = pPDFSettings[y]; pPDFSettings[y] = HoldPDFSettings; } } } } INT CPDFCompDemoView::ApplyPDFSave( TCHAR *pFileName, pPDFCOMPSETTENGS pPDFSettings, INT nSize ) { INT i; CPDFCompDemoApp * pApp = (CPDFCompDemoApp *)AfxGetApp(); // Init PDF Compressor Object pApp->m_pPDFComp->PDFCompInit(FALSE); for(i=0; i< nSize; i++ ) { pApp->m_pPDFComp->PDFCompSetCompression( (PDFCOMP_1BIT_COMPRESSION_TYPE_ENABLED | PDFCOMP_2BIT_COMPRESSION_TYPE_ENABLED | PDFCOMP_PICTURE_COMPRESSION_TYPE_ENABLED), (OneBitCompressionTypeConstants)pPDFSettings[i].m_n1Bit, (TwoBitCompressionTypeConstants)pPDFSettings[i].m_n2Bit, (PictureCompressionTypeConstants)pPDFSettings[i].m_nPict, pPDFSettings[i].m_nQFactor); if(pPDFSettings[i].m_bNOMRC ) { // Inser Page without MRC pApp->m_pPDFComp->PDFCompInsertNormal(pPDFSettings[i].m_pRaster); } else { // Inser Page with MRC pApp->m_pPDFComp->ImageQuality = PDFCOMPIMAGEQUALITY_USER; pApp->m_pPDFComp->OutputQuality = PDFCOMPOUTPUTQUALITY_USER; pApp->m_pPDFComp->BackGroundThreshold = pPDFSettings[i].nBKThreshold; pApp->m_pPDFComp->CleanSize = pPDFSettings[i].nCleanSize; pApp->m_pPDFComp->ColorThreshold = pPDFSettings[i].nCLRThreshold; pApp->m_pPDFComp->CombineThreshold = pPDFSettings[i].nCombThreshold; pApp->m_pPDFComp->SegmentQuality = pPDFSettings[i].nSegQuality; pApp->m_pPDFComp->Flags = pPDFSettings[i].m_nSeg; if( FALSE == pPDFSettings[i].m_bCheckBackground ) pApp->m_pPDFComp->Flags |= PDFCOMP_WITHOUTBACKGROUND; pApp->m_pPDFComp->PDFCompInsertMRC(pPDFSettings[i].m_pRaster ); } } // Write to External PDf File pApp->m_pPDFComp->PDFCompWrite(pFileName); pApp->m_pPDFComp->PDFCompFree(); return SUCCESS; } void CPDFCompDemoView::OnFileSaveall() { CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd; CMDIChildWnd* pChild = (CMDIChildWnd*)pFrame->GetActiveFrame(); CPDFCompDemoDoc *pDoc= (CPDFCompDemoDoc*)pChild->GetActiveView(); static OPENFILENAME OpenFileName; TCHAR szFileName[_MAX_PATH]=_T(""); pPDFCOMPSETTENGS pPDFSettings=NULL; INT nRet=SUCCESS; INT i=0; do { if (pChild) { pDoc = (CPDFCompDemoDoc*)pChild->GetActiveDocument(); if(pDoc->m_PDFCompSettings.bAdded) { pPDFSettings = (pPDFCOMPSETTENGS) realloc(pPDFSettings,sizeof(PDFCOMPSETTENGS)*(i+1)); memcpy(&pPDFSettings[i], &pDoc->m_PDFCompSettings, sizeof(PDFCOMPSETTENGS)) ; pPDFSettings[i].m_pRaster = pDoc->m_pLEADRaster; i++; } } pChild = (CMDIChildWnd*) pChild->GetWindow(GW_HWNDNEXT); } while (pChild); if(0 == i) { MessageBox(TEXT("No image is added"), TEXT("PDFComp Demo"), MB_ICONEXCLAMATION); } else { OPENFILENAME OpenFileName; TCHAR szSaveFileFilter[] = {TEXT("Acrobat (*.pdf)\0") TEXT("*.pdf\0")}; TCHAR *pFileName=NULL; ZeroMemory( &OpenFileName, sizeof( OPENFILENAME ) ); OpenFileName.lStructSize = sizeof( OPENFILENAME ); OpenFileName.hwndOwner = m_hWnd; OpenFileName.hInstance = NULL; OpenFileName.lpstrFilter = szSaveFileFilter; OpenFileName.lpstrCustomFilter = NULL; OpenFileName.nMaxCustFilter = 0; OpenFileName.lpstrFile = szFileName; OpenFileName.nFilterIndex = 1; OpenFileName.nMaxFile = sizeof( szFileName )/sizeof(TCHAR); OpenFileName.lpstrFileTitle = szFileName; OpenFileName.nMaxFileTitle = sizeof( szFileName )/sizeof(TCHAR); OpenFileName.lpstrInitialDir = NULL; OpenFileName.lpstrTitle = TEXT("Save to PDF"); OpenFileName.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; OpenFileName.nFileOffset = 0; OpenFileName.nFileExtension = 0; OpenFileName.lpstrDefExt = TEXT(""); OpenFileName.lCustData = 0L; OpenFileName.lpfnHook = NULL; OpenFileName.lpTemplateName = NULL; if(!GetSaveFileName( &OpenFileName )) return; //Sort documents depending on page number ApplySort(pPDFSettings, i); nRet = ApplyPDFSave (OpenFileName.lpstrFile, pPDFSettings, i ); if(pPDFSettings) free(pPDFSettings); if(nRet< SUCCESS) MessageBox(TEXT("PDF Save Error"), TEXT("PDFComp Demo"), MB_ICONEXCLAMATION); } } BEGIN_EVENTSINK_MAP(CPDFCompDemoView, CFormView) //{{AFX_EVENTSINK_MAP(CPDFCompDemoView) ON_EVENT(CPDFCompDemoView, IDC_LEADRASTERVIEW1, 7 /* DblClick */, OnDblClickLeadrasterview1, VTS_NONE) ON_EVENT(CPDFCompDemoView, IDC_LEADRASTERVIEW1, 4 /* MouseDown */, OnMouseDownLeadrasterview1, VTS_I2 VTS_I2 VTS_R4 VTS_R4) //}}AFX_EVENTSINK_MAP END_EVENTSINK_MAP() void CPDFCompDemoView::OnDblClickLeadrasterview1() { if(m_StatusRect.PtInRect(m_point)) { OnPdfcompOptionsAdd() ; } } void CPDFCompDemoView::OnMouseDownLeadrasterview1(short Button, short Shift, float x, float y) { m_point.x = (long)x; m_point.y = (long)y; } void CPDFCompDemoView::OnPdfcompOptionsAdvance() { CPDFCompDemoDoc* pDoc = GetDocument(); CPDFOptions OptionsDlg; OptionsDlg.m_n1Bit = pDoc->m_PDFCompSettings.m_n1Bit; OptionsDlg.m_n2Bit = pDoc->m_PDFCompSettings.m_n2Bit; OptionsDlg.m_nPict = pDoc->m_PDFCompSettings.m_nPict; OptionsDlg.m_nSeg = pDoc->m_PDFCompSettings.m_nSeg; OptionsDlg.m_nQFactor = pDoc->m_PDFCompSettings.m_nQFactor; OptionsDlg.m_bCheckBackground = pDoc->m_PDFCompSettings.m_bCheckBackground; if( IDOK == OptionsDlg.DoModal() ) { pDoc->m_PDFCompSettings.m_n1Bit = OptionsDlg.m_n1Bit; pDoc->m_PDFCompSettings.m_n2Bit = OptionsDlg.m_n2Bit; pDoc->m_PDFCompSettings.m_nPict = OptionsDlg.m_nPict; pDoc->m_PDFCompSettings.m_bCheckBackground = OptionsDlg.m_bCheckBackground; pDoc->m_PDFCompSettings.m_nSeg = OptionsDlg.m_nSeg; pDoc->m_PDFCompSettings.m_nQFactor = OptionsDlg.m_nQFactor; } } void CPDFCompDemoView::DrawPanel(CPDFCompDemoView* pMyView, ILEADRaster* pSrcRaster, PDFCOMPSETTENGS PDFCompSettings) { ILEADRaster* KeyRaster; ILEADRaster* pRaster; RECT KeyRect; HDC keyDC; CString pszStatus; CString pszIndex; COLORREF rgbFill; HPEN hOldPen; HDC WndDC; if(pSrcRaster->BitmapWidth >= 150 && pSrcRaster->BitmapHeight >= 150 ) { CPDFCompDemoApp * pApp = (CPDFCompDemoApp *)AfxGetApp(); HRESULT hr = CoCreateInstance(CLSID_LEADRaster, NULL, CLSCTX_ALL, IID_ILEADRaster, (void**)&pRaster); if (FAILED(hr)) return; pRaster->Bitmap = pSrcRaster->Bitmap; pApp->m_pRasterPro->ColorRes(pRaster,24,CRP_BYTEORDERBGR, CRD_NODITHERING, 0); WndDC = (HDC)pRaster->GetBitmapDC (); if(PDFCompSettings.bAdded) { pszStatus = " Status : ADDED\n"; pszIndex.Format(TEXT(" Page Index : %d\n"),PDFCompSettings.nPageNumber); pszStatus += pszIndex; if(!PDFCompSettings.m_bNOMRC) { pszStatus += " Input Quality: "; switch(PDFCompSettings.nPDFComboSel) { case 0: pszStatus += "Auto"; break; case 1: pszStatus += "Noisy"; break; case 2: pszStatus += "Scanned"; break; case 3: pszStatus += "Printed"; break; case 4: pszStatus += "Computer Generated"; break; case 5: pszStatus += "Photo"; break; case 6: pszStatus += "User Defined"; break; } pszStatus += "\n Output Quality : "; switch(PDFCompSettings.nImgQComboSel) { case 0: pszStatus += "Auto"; break; case 1: pszStatus += "Poor"; break; case 2: pszStatus += "Average"; break; case 3: pszStatus += "Good"; break; case 4: pszStatus += "Excellent"; break; case 5: pszStatus += "User Defined"; break; } } } else { pszStatus = " Status : NOT ADDED"; } SetBkMode( WndDC, TRANSPARENT ); LOGFONT LogFont; memset( &LogFont, 0, sizeof( LogFont ) ); LogFont.lfHeight = 18; LogFont.lfWeight = FW_BOLD; lstrcpy( LogFont.lfFaceName, TEXT("Arial") ); DeleteFont(SelectFont( WndDC, CreateFontIndirect( &LogFont ) )); memset( &KeyRect, 0, sizeof( KeyRect ) ); KeyRect.right = (long) pRaster->BitmapWidth; DrawText( WndDC, pszStatus, lstrlen(pszStatus), &KeyRect, DT_LEFT | DT_WORDBREAK | DT_CALCRECT ); hr = CoCreateInstance(CLSID_LEADRaster, NULL, CLSCTX_ALL, IID_ILEADRaster, (void**)&KeyRaster); if (FAILED(hr)) return ; KeyRaster->CreateBitmap((FLOAT)KeyRect.right,(FLOAT)KeyRect.bottom,24); if( PDFCompSettings.bAdded ) rgbFill = RGB(0,255,0); else rgbFill = RGB(255,0,0); pApp->m_pRasterPro->Fill(KeyRaster,rgbFill); keyDC = (HDC)KeyRaster->GetBitmapDC(); DeleteBrush(SelectBrush( keyDC, GetStockBrush( NULL_BRUSH ) )); hOldPen = SelectPen( keyDC, CreatePen( PS_SOLID, 3, 0 ) ); Rectangle( keyDC, 0, 0, KeyRect.right - KeyRect.left, KeyRect.bottom - KeyRect.top ); DeletePen( SelectPen( keyDC, hOldPen ) ); KeyRaster->ReleaseBitmapDC(); pRaster->ReleaseBitmapDC (); pApp->m_pRasterPro->AlphaBlend(pRaster,(float) KeyRect.left,(float) KeyRect.top,(float) KeyRect.right, (float) KeyRect.bottom,KeyRaster, 0, 0, 150); WndDC = (HDC)pRaster->GetBitmapDC (); SetBkMode( WndDC, TRANSPARENT ); DrawText( WndDC, pszStatus, lstrlen(pszStatus), &KeyRect, DT_LEFT | DT_WORDBREAK ); m_StatusRect.CopyRect(&KeyRect); if(KeyRaster) KeyRaster->Release(); pRaster->ReleaseBitmapDC (); pMyView->m_LEADRasterView.GetRaster().SetBitmap(pRaster->Bitmap); if(pRaster) pRaster->Release(); } } void CPDFCompDemoView::OnClose() { OnPdfcompOptionsDel(); CFormView::OnClose(); } void CPDFCompDemoView::OnDestroy() { CPDFCompDemoDoc * pDoc = (CPDFCompDemoDoc *)GetDocument(); if (pDoc->m_PDFCompSettings.bAdded && pDoc->m_bLoaded) OnPdfcompOptionsDel(); CFormView::OnDestroy(); }