📄 pdftotiffdlg.cpp
字号:
// PDFToTIFFDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PDFToTIFF.h"
#include "PDFToTIFFDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPDFToTIFFDlg dialog
CPDFToTIFFDlg::CPDFToTIFFDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPDFToTIFFDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPDFToTIFFDlg)
m_filepath = _T("");
m_dpi = 96;
m_height = 100;
m_width = 100;
m_removemargin = FALSE;
m_pagecount = 0;
m_scale = 100.0f;
m_keepratio = FALSE;
m_destfile = _T("");
m_pagerange = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CPDFToTIFFDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPDFToTIFFDlg)
DDX_Control(pDX, IDC_KEEPRATIO, m_pagekeepratio);
DDX_Control(pDX, IDC_WIDTH, m_pagewidth);
DDX_Control(pDX, IDC_SCALE, m_pagescale);
DDX_Control(pDX, IDC_HEIGHT, m_pageheight);
DDX_Control(pDX, IDC_DPI, m_pagedpi);
DDX_Control(pDX, IDC_SETTIFFSIZE, m_settiffsize);
DDX_Control(pDX, IDC_ROTATE, m_rotate);
DDX_Control(pDX, IDC_PRINTCONTENT, m_printcontent);
DDX_Control(pDX, IDC_COMPRESS, m_compress);
DDX_Control(pDX, IDC_BITPERPIXEL, m_bitperpixel);
DDX_Text(pDX, IDC_FILEPATH, m_filepath);
DDX_Text(pDX, IDC_DPI, m_dpi);
DDX_Text(pDX, IDC_HEIGHT, m_height);
DDX_Text(pDX, IDC_WIDTH, m_width);
DDX_Check(pDX, IDC_REMOVEMARGIN, m_removemargin);
DDX_Text(pDX, IDC_PAGECOUNT, m_pagecount);
DDX_Text(pDX, IDC_SCALE, m_scale);
DDX_Check(pDX, IDC_KEEPRATIO, m_keepratio);
DDX_Text(pDX, IDC_FILEPATH2, m_destfile);
DDX_Text(pDX, IDC_PAGERANGE, m_pagerange);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPDFToTIFFDlg, CDialog)
//{{AFX_MSG_MAP(CPDFToTIFFDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
ON_BN_CLICKED(IDC_PDFTOTIFF, OnPdftotiff)
ON_WM_DESTROY()
ON_CBN_SELCHANGE(IDC_SETTIFFSIZE, OnSelchangeSettiffsize)
ON_BN_CLICKED(IDC_DESTBROWSE, OnDestbrowse)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPDFToTIFFDlg message handlers
BOOL CPDFToTIFFDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CoInitialize(NULL);
// TODO: Add extra initialization here
try
{
m_pPDFToImage.CreateInstance(CLSID_PDF2Image);
m_pPDFToImage->Initialize("",0);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
bFileOpend = FALSE;
nScaleOrSize = 0;
m_bitperpixel.SetCurSel(3);
m_compress.SetCurSel(1);
m_printcontent.SetCurSel(0);
m_rotate.SetCurSel(0);
m_settiffsize.SetCurSel(0);
m_pagescale.EnableWindow(TRUE);
m_pagedpi.EnableWindow(TRUE);
m_pagewidth.EnableWindow(FALSE);
m_pageheight.EnableWindow(FALSE);
m_pagekeepratio.EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CPDFToTIFFDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CPDFToTIFFDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CPDFToTIFFDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPDFToTIFFDlg::OnBrowse()
{
// TODO: Add your control notification handler code here
CString strFilePath;
CFileDialog PDFDlg(TRUE, _T("pdf"), NULL, OFN_FILEMUSTEXIST|OFN_OVERWRITEPROMPT, _T("PDF Files(*.pdf)|*.pdf||"));
VARIANT vrFileName;
if(PDFDlg.DoModal() == IDOK)
{
V_VT(&vrFileName) = VT_BSTR;
strFilePath = PDFDlg.GetPathName();
V_BSTR(&vrFileName) = strFilePath.AllocSysString();
m_QueryPassword.Initialize(m_pPDFToImage,DIID__IPDF2ImageEvents);
try
{
m_pPDFToImage->OpenFile(vrFileName,"");
m_QueryPassword.Uninitialize(m_pPDFToImage,DIID__IPDF2ImageEvents);
m_pagecount = m_pPDFToImage->GetPageCount();
m_filepath = strFilePath;
strFilePath.Replace(".pdf",".tiff");
m_destfile = strFilePath;
bFileOpend = TRUE;
UpdateData(FALSE);
}
catch (_com_error e)
{
AfxMessageBox(e.Description());
}
VariantClear(&vrFileName);
}
}
void CPDFToTIFFDlg::OnPdftotiff()
{
// TODO: Add your control notification handler code here
// CString strFilePath;
CFileDialog ImageFileDlg(FALSE,_T("TIFF File"),NULL,OFN_FILEMUSTEXIST|OFN_OVERWRITEPROMPT,_T("TIFF Files(*.tiff)|*.tiff||"));
int nBitPerPixel,nCompress,nRotate,nContent;
long lWidth,lHeight;
BPPEnum enumBPP;
TIFFCompressEnum enumCompress;
UpdateData(TRUE);
if(bFileOpend == FALSE)
{
AfxMessageBox("You must open a pdf file!",MB_OK,1);
}
else if(m_pPDFToImage != NULL)
{
nBitPerPixel = m_bitperpixel.GetCurSel();
switch(nBitPerPixel)
{
case 0:
enumBPP = BPP_1;
break;
case 1:
enumBPP = BPP_4;
break;
case 2:
enumBPP = BPP_8;
break;
case 3:
enumBPP = BPP_24;
break;
}
nCompress = m_compress.GetCurSel();
switch(nCompress)
{
case 0:
enumCompress = TIFF_NO_COMPRESS;
break;
case 1:
enumCompress = TIFF_CCITT_G3;
break;
case 2:
enumCompress = TIFF_CCITT_G4;
break;
case 3:
enumCompress = TIFF_LZW;
break;
case 4:
enumCompress = TIFF_JPEG_24B;
break;
case 5:
enumCompress = TIFF_ZIP;
break;
}
nRotate = m_rotate.GetCurSel();
switch(nRotate)
{
case 0:
m_pPDFToImage->put_rotation(Rotate_0);
break;
case 1:
m_pPDFToImage->put_rotation(Rotate_90);
break;
case 2:
m_pPDFToImage->put_rotation(Rotate_180);
break;
case 3:
m_pPDFToImage->put_rotation(Rotate_270);
break;
case 4:
m_pPDFToImage->put_rotation(Rotate_Neg_90);
break;
case 5:
m_pPDFToImage->put_rotation(Rotate_Neg_180);
break;
case 6:
m_pPDFToImage->put_rotation(Rotate_Neg_270);
break;
}
nContent = m_printcontent.GetCurSel();
switch(nContent)
{
case 0:
m_pPDFToImage->put_content(PC_ContentAndForm);
break;
case 1:
m_pPDFToImage->put_content(PC_ContentOnly);
break;
case 2:
m_pPDFToImage->put_content(PC_FormOnly);
break;
}
m_pPDFToImage->PutremoveMargin(m_removemargin);
if(nScaleOrSize == 0)
{
m_pPDFToImage->SetScale(m_scale,m_dpi);
}
if(nScaleOrSize == 1)
{
m_pPDFToImage->SetSize(m_width,m_height,m_keepratio);
}
if(m_destfile.IsEmpty())
{
if(ImageFileDlg.DoModal() == IDOK)
{
m_destfile = ImageFileDlg.GetPathName();
UpdateData(FALSE);
}
}
if(!m_destfile.IsEmpty())
{
try
{
m_pPDFToImage->PrintToMultiTIFF((LPSTR)(LPCSTR)m_pagerange,(LPSTR)(LPCSTR)m_destfile,enumBPP,enumCompress,0,&lWidth,&lHeight);
ShellExecute(NULL, "open", m_destfile, NULL, NULL, SW_SHOW);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
}
}
}
void CPDFToTIFFDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
if(m_pPDFToImage != NULL)
{
m_pPDFToImage->CloseFile();
m_pPDFToImage->Uninitialize();
m_pPDFToImage.Release();
m_pPDFToImage = NULL;
}
CoUninitialize();
}
void CPDFToTIFFDlg::OnCancel()
{
// TODO: Add extra cleanup here
if(m_pPDFToImage != NULL)
{
m_pPDFToImage->CloseFile();
m_pPDFToImage->Uninitialize();
m_pPDFToImage.Release();
m_pPDFToImage = NULL;
}
CoUninitialize();
CDialog::OnCancel();
}
void CPDFToTIFFDlg::OnSelchangeSettiffsize()
{
// TODO: Add your control notification handler code here
int nSelect = m_settiffsize.GetCurSel();
switch(nSelect)
{
case 0:
nScaleOrSize = 0;
m_pagescale.EnableWindow(TRUE);
m_pagedpi.EnableWindow(TRUE);
m_pagewidth.EnableWindow(FALSE);
m_pageheight.EnableWindow(FALSE);
m_pagekeepratio.EnableWindow(FALSE);
break;
case 1:
nScaleOrSize = 1;
m_pagescale.EnableWindow(FALSE);
m_pagedpi.EnableWindow(FALSE);
m_pagewidth.EnableWindow(TRUE);
m_pageheight.EnableWindow(TRUE);
m_pagekeepratio.EnableWindow(TRUE);
break;
}
}
void CPDFToTIFFDlg::OnDestbrowse()
{
// TODO: Add your control notification handler code here
CFileDialog ImageFileDlg(FALSE,_T("TIFF File"),NULL,OFN_FILEMUSTEXIST|OFN_OVERWRITEPROMPT,_T("TIFF Files(*.tiff)|*.tiff||"));
if(ImageFileDlg.DoModal() == IDOK)
{
m_destfile = ImageFileDlg.GetPathName();
UpdateData(FALSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -