/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2019, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a license // agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2019 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// // DwfUnderlayDialog.cpp : implementation file // #include "stdafx.h" #include "odamfcapp.h" #include "PdfImportDialog.h" #include "DbUnderlayHost.h" #include "DbUnderlayDefinition.h" #include "DbUnderlayReference.h" #include "DbDictionary.h" #include "DbBlockTableRecord.h" #include "Gi\GiRasterWrappers.h" #include "OdPath.h" #if _MSC_VER < 1400 #include #include #endif #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CPdfImportDialog dialog CPdfImportDialog::CPdfImportDialog(OdDbDatabase *pDb, CWnd* pParent /*=NULL*/) : CDialog(CPdfImportDialog::IDD, pParent) , m_pDb(pDb) { //{{AFX_DATA_INIT(CPdfImportDialog) m_Path = _T(""); m_SelectedPageNum = 0; m_LayersUseType = 0; m_Scaling = 1; m_Rotation = 0; m_ImportVectorGeometry = TRUE; m_ImportSolidFills = TRUE; m_ImportTrueTypeText = TRUE; m_ImportRasterImages = TRUE; m_ImportGradientFills = TRUE; m_ImportAsBlock = FALSE; m_JoinLineAndArcSegments = TRUE; m_ConvertSolidsToHatches = TRUE; m_ApplyLineweight = TRUE; m_ImportTrueTypeTextAsGeometry = FALSE; m_UseGeometryOptimization = TRUE; m_ImportType3AsTrueTypeText = FALSE; m_ImportAsRgb = FALSE; //}}AFX_DATA_INIT } OdString CPdfImportDialog::GetCommandString() { return m_CommandString; } void CPdfImportDialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CPdfImportDialog) DDX_Text(pDX, IDC_EDIT_FILENAME, m_Path); DDX_Control(pDX, IDC_LIST_SHEETS, m_SheetList); DDX_Control(pDX, IDC_PDF_PAGE_ROTAION, m_comboRotation); DDX_Radio(pDX, IDC_RADIO_USE_PDF_LAYERS, m_LayersUseType); DDX_Check(pDX, IDC_PDF_IMP_VECTOR_GEOM, m_ImportVectorGeometry); DDX_Check(pDX, IDC_PDF_IMP_SOLID_FILLS, m_ImportSolidFills); DDX_Check(pDX, IDC_PDF_IMP_TT_TEXT, m_ImportTrueTypeText); DDX_Check(pDX, IDC_PDF_IMP_RASTER_IMAGES, m_ImportRasterImages); DDX_Check(pDX, IDC_PDF_IMP_SHADING, m_ImportGradientFills); DDX_Check(pDX, IDC_PDF_AS_BLOCK, m_ImportAsBlock); DDX_Check(pDX, IDC_PDF_JOIN_LINE_ARC, m_JoinLineAndArcSegments); DDX_Check(pDX, IDC_PDF_CONVERT_SOLIDS2HATCHES, m_ConvertSolidsToHatches); DDX_Check(pDX, IDC_PDF_LINEWEIGHT, m_ApplyLineweight); DDX_Check(pDX, IDC_TTF_AS_GEOM, m_ImportTrueTypeTextAsGeometry); DDX_Check(pDX, IDC_PDF_IMP_TYPE3_AS_TT, m_ImportType3AsTrueTypeText); DDX_Check(pDX, IDC_PDF_IMP_USE_2DGEOM_OPTIM, m_UseGeometryOptimization); DDX_Check(pDX, IDC_PDF_IMP_AS_RGB, m_ImportAsRgb); DDX_Text(pDX, IDC_EDIT_SCALE, m_Scaling); DDX_Text(pDX, IDC_PDF_PAGE_ROTAION, m_Rotation); DDX_Text(pDX, IDC_EDIT_PAGE_NUM, m_SelectedPageNum); DDX_Check(pDX, IDC_INSERTION_POINT, m_UseInsertionPoint); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CPdfImportDialog, CDialog) //{{AFX_MSG_MAP(CPdfImportDialog) ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnBrowse) ON_BN_CLICKED(IDOK, OnInsert) ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_SHEETS, OnSelchangeSheetList) ON_EN_KILLFOCUS(IDC_EDIT_PAGE_NUM, OnPageNumChanged) ON_BN_CLICKED(IDC_PDF_IMP_VECTOR_GEOM, OnCheckImportVectorGeometry) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CPdfImportDialog message handlers BOOL CPdfImportDialog::OnInitDialog() { CDialog::OnInitDialog(); CRect rect, rectButton; GetWindowRect(&rect); GetDlgItem(IDC_BUTTON_BROWSE)->GetWindowRect(&rectButton); m_DialogHeight = rect.Height() - rectButton.Height() - 40; rect.bottom = rectButton.bottom + 17; MoveWindow(&rect); m_SheetList.SetBkColor(::GetSysColor(COLOR_GRAYTEXT)); m_comboRotation.ResetContent(); for (OdUInt8 i = 0; i < 4; ++i) { CString rotation_str; rotation_str.Format(_T("%i"), i*90); m_comboRotation.InsertString(i, rotation_str); } m_comboRotation.SetCurSel(0); UpdateData(true); // save and validate m_LayersUseType = AfxGetApp()->GetProfileInt(L"PdfImport", L"PDFIMPORTLAYERS", 0); int import_filter = AfxGetApp()->GetProfileInt(L"PdfImport", L"PDFIMPORTFILTER", 0); m_ImportVectorGeometry = !GETBIT(import_filter, 1); GetDlgItem(IDC_PDF_IMP_SOLID_FILLS)->EnableWindow(m_ImportVectorGeometry); m_ImportSolidFills = !GETBIT(import_filter, 4); m_ImportTrueTypeText = !GETBIT(import_filter, 2); m_ImportRasterImages = !GETBIT(import_filter, 8); m_ImportGradientFills = !GETBIT(import_filter, 16); int import_mode = AfxGetApp()->GetProfileInt(L"PdfImport", L"PDFIMPORTMODE", 46); m_ImportAsBlock = GETBIT(import_mode, 1); m_JoinLineAndArcSegments = GETBIT(import_mode, 4); m_ConvertSolidsToHatches = GETBIT(import_mode, 8); m_ApplyLineweight = GETBIT(import_mode, 2); m_ImportTrueTypeTextAsGeometry = GETBIT(import_mode, 16); m_UseGeometryOptimization = GETBIT(import_mode, 32); m_ImportType3AsTrueTypeText = GETBIT(import_mode, 64); m_ImportAsRgb = GETBIT(import_mode, 128); UpdateData(false); return TRUE; } void CPdfImportDialog::OnBrowse() { // TODO: Add your control notification handler code here CFileDialog dlgFile(true); dlgFile.m_ofn.Flags |= OFN_HIDEREADONLY | OFN_EXPLORER | OFN_FILEMUSTEXIST; dlgFile.m_ofn.lpstrFilter = _T("PDF files (*.pdf)\0*.pdf\0"); dlgFile.m_ofn.lpstrDefExt = _T("pdf"); dlgFile.m_ofn.lpstrTitle = _T("Select pdf file"); if (dlgFile.DoModal() == IDOK) { UpdateData(true); // save and validate m_Path = dlgFile.GetPathName(); UpdateData(false); bool bInit = InitProperties(); if (bInit) { CRect rect; GetWindowRect(&rect); rect.bottom += m_DialogHeight; m_DialogHeight = 0; MoveWindow(&rect); } } } void CPdfImportDialog::OnError(CString str) { MessageBox(str, CString("Error")); } void CPdfImportDialog::OnError(OdResult res) { OdError err(res); CString str((LPCTSTR)err.description()); OnError(str); } void CPdfImportDialog::OnPageNumChanged() { UpdateData(true); m_SheetList.SetItemState(m_SelectedPageNum - 1, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED); m_SheetList.EnsureVisible(m_SelectedPageNum - 1, FALSE); m_SheetList.SetFocus(); UpdateData(false); } bool CPdfImportDialog::InitProperties() { // ask for UnderlayHostPE OdDbUnderlayHostPEPtr pHost; OdDbPdfDefinition::loadPdfUnderlayModule(); pHost = OdDbPdfDefinition::desc()->getX(OdDbUnderlayHostPE::desc()); if(!pHost.get()) { OnError(_T("PdfUnderlayModule is not loaded.")); return false; } OdResult res = pHost->load(*m_pDb, (LPCTSTR)m_Path, "", m_pFile); if(res != eOk) { m_pFile = OdDbUnderlayFilePtr(); if (res == eInvalidPassword) { //check password cache for the valid password. OdPwdCachePtr pCache = m_pDb->appServices()->getPasswordCache(); if (!pCache.isNull()) { OdPwdIteratorPtr pIter = pCache->newIterator(); if (!pIter.isNull()) { for(;!pIter->done() && res != eOk; pIter->next()) { OdPassword password; pIter->get(password); res = pHost->load(*m_pDb, (LPCTSTR)m_Path, password, m_pFile); if (eOk == res) { m_Password = password; break; } } } } // Means that there are no valid password in cache. if (res == eInvalidPassword) { // ask user about valid password OdPassword odPass; if (!m_pDb->appServices()->getPassword((LPCTSTR)m_Path, false, odPass)) { OnError("Password protected file."); return false; } // try to load again with new password res = pHost->load(*m_pDb, (LPCTSTR)m_Path, odPass, m_pFile); if (res == eInvalidPassword) { OnError("Invalid password."); return false; } else { pCache->add(odPass); m_Password = odPass; } } } if(res != eOk) { OnError("OdDbUnderlayHostPE cannot load file"); return false; } } FillSheetList(); return true; } void CPdfImportDialog::FillSheetList() { CString total_text; total_text.Format(_T("Total: %i"), m_pFile->getItemCount()); GetDlgItem(IDC_STATIC_PAGE_COUNT)->SetWindowText(total_text); m_SheetList.SetRedraw(false); m_SheetList.DeleteAllItems(); for (OdInt32 page_num = 0; page_num < m_pFile->getItemCount(); ++page_num) { OdDbUnderlayItemPtr pItem = m_pFile->getItem(page_num); OdGiRasterImagePtr pImage = pItem->getThumbnail(); int width = pImage->pixelWidth(); int height = pImage->pixelHeight(); if (0 == page_num) { m_stateImages.DeleteImageList(); if (1 == m_pFile->getItemCount()) { CRect rect; m_SheetList.GetClientRect(rect); double coef = odmin(((double)rect.Width() - 50) / width, ((double)rect.Height() - 50) / height); width *= coef; height *= coef; } m_stateImages.Create(width, height, ILC_COLOR24, 0, m_pFile->getItemCount()); } if (!pImage.isNull()) { OdSmartPtr pDesc = OdRxObjectImpl::createObject(); pDesc->setPixelWidth(width); pDesc->setPixelHeight(height); pDesc->setColorDepth(24); pDesc->pixelFormat().setBGR(); pDesc->setScanLinesAlignment(4); OdGiRasterImagePtr pCopyImg; pCopyImg = pImage->convert(true, 50.0, 50.0, 0.0, 0, false, false, false, pDesc); if (!pCopyImg.isNull()) { BITMAPINFO* pHeader; OdUInt8Array headerMemCont; headerMemCont.resize(sizeof(BITMAPINFOHEADER) + pCopyImg->paletteDataSize()); pHeader = (BITMAPINFO*)headerMemCont.getPtr(); pHeader->bmiHeader.biBitCount = (WORD)pCopyImg->colorDepth(); pHeader->bmiHeader.biClrImportant = 0; pHeader->bmiHeader.biClrUsed = 0; pHeader->bmiHeader.biCompression = BI_RGB; pHeader->bmiHeader.biHeight = pCopyImg->pixelHeight(); pHeader->bmiHeader.biPlanes = 1; pHeader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pHeader->bmiHeader.biSizeImage = 0; pHeader->bmiHeader.biWidth = pCopyImg->pixelWidth(); pHeader->bmiHeader.biXPelsPerMeter = 0; pHeader->bmiHeader.biYPelsPerMeter = 0; pCopyImg->paletteData((OdUInt8*)pHeader->bmiColors); OdUInt8* pBuf; HBITMAP hBmp = CreateDIBSection(NULL, pHeader, DIB_RGB_COLORS, (void**)&pBuf, NULL, 0); if (pCopyImg->scanLineSize()) pCopyImg->scanLines(pBuf, 0, pCopyImg->pixelHeight()); CBitmap bitmap; bitmap.Attach(hBmp); m_stateImages.Add(&bitmap, RGB(0, 0, 0)); LVITEM lvItem; lvItem.iItem = page_num; lvItem.iImage = page_num; // image index that refers to your image list CString str; str.Format(_T("%i"), page_num + 1); lvItem.pszText = str.GetBuffer(); lvItem.mask = LVIF_IMAGE | LVIF_TEXT; lvItem.lParam = 0; lvItem.iSubItem = 0; m_SheetList.InsertItem(&lvItem); } } } m_SheetList.SetImageList(&m_stateImages, LVSIL_NORMAL); m_SheetList.SetItemState(0, LVIS_SELECTED, LVIS_SELECTED); m_SheetList.SetRedraw(true); } #define BOOL_TO_TEXT(val) val ? OD_T("Yes") : OD_T("No") void CPdfImportDialog::OnInsert() { CWnd* pWnd = GetFocus(); if (GetDlgItem(IDOK) != pWnd) { if (GetDlgItem(IDC_EDIT_PAGE_NUM) == pWnd) { OnPageNumChanged(); } return; } // TODO: Add your control notification handler code here UpdateData(true); // save and validate OdString layers_type(OD_T("UsePDFLayers")); switch (m_LayersUseType) { case 1: layers_type = OD_T("ObjectLayers"); break; case 2: layers_type = OD_T("CurrentLayer"); break; } m_CommandString = OdString().format(OD_T("pdfin File %ls\n %ls %i %ls %ls Yes %ls %ls %ls %ls %ls %ls %ls %ls %ls %ls %ls %ls %ls %ls"), m_Path, m_Password.isEmpty()? OD_T("\n"):m_Password.c_str(), (int)m_SelectedPageNum, m_pDb->formatter().formatLinear(OdaToRadian(m_Rotation)).c_str(), m_pDb->formatter().formatLinear(m_Scaling).c_str(), layers_type.c_str(), BOOL_TO_TEXT(m_ImportVectorGeometry), BOOL_TO_TEXT(m_ImportVectorGeometry && m_ImportSolidFills), BOOL_TO_TEXT(m_ImportTrueTypeText), BOOL_TO_TEXT(m_ImportRasterImages), BOOL_TO_TEXT(m_ImportGradientFills), BOOL_TO_TEXT(m_ImportAsBlock), BOOL_TO_TEXT(m_JoinLineAndArcSegments), BOOL_TO_TEXT(m_ConvertSolidsToHatches), BOOL_TO_TEXT(m_ApplyLineweight), BOOL_TO_TEXT(m_ImportTrueTypeTextAsGeometry), BOOL_TO_TEXT(m_UseGeometryOptimization), BOOL_TO_TEXT(m_ImportType3AsTrueTypeText), BOOL_TO_TEXT(m_ImportAsRgb) ); if (FALSE == m_UseInsertionPoint) m_CommandString += OD_T(" 0.,0.,0."); AfxGetApp()->WriteProfileInt(L"PdfImport", L"PDFIMPORTLAYERS", m_LayersUseType); m_LayersUseType = AfxGetApp()->GetProfileInt(L"PdfImport", L"PDFIMPORTLAYERS", 0); int import_filter(0); SETBIT(import_filter, 1, !m_ImportVectorGeometry); SETBIT(import_filter, 4, !m_ImportSolidFills); SETBIT(import_filter, 2, !m_ImportTrueTypeText); SETBIT(import_filter, 8, !m_ImportRasterImages); SETBIT(import_filter, 16, !m_ImportGradientFills); AfxGetApp()->WriteProfileInt(L"PdfImport", L"PDFIMPORTFILTER", import_filter); int import_mode(0); SETBIT(import_mode, 1, m_ImportAsBlock); SETBIT(import_mode, 4, m_JoinLineAndArcSegments); SETBIT(import_mode, 8, m_ConvertSolidsToHatches); SETBIT(import_mode, 2, m_ApplyLineweight); SETBIT(import_mode, 16, m_ImportTrueTypeTextAsGeometry); SETBIT(import_mode, 32, m_UseGeometryOptimization); SETBIT(import_mode, 64, m_ImportType3AsTrueTypeText); SETBIT(import_mode, 128, m_ImportAsRgb); m_LayersUseType = AfxGetApp()->WriteProfileInt(L"PdfImport", L"PDFIMPORTMODE", import_mode); CPdfImportDialog::OnOK(); } void CPdfImportDialog::OnCheckImportVectorGeometry() { UpdateData(true); GetDlgItem(IDC_PDF_IMP_SOLID_FILLS)->EnableWindow(m_ImportVectorGeometry); UpdateData(false); } void CPdfImportDialog::OnSelchangeSheetList(NMHDR* /*pNMHDR*/, LRESULT* /*pResult*/) { UpdateData( true ); POSITION pos = m_SheetList.GetFirstSelectedItemPosition(); int page_num = 0; if (pos) page_num = m_SheetList.GetNextSelectedItem(pos); else { UpdateData(false); return; } if(m_SelectedPageNum != (page_num + 1)) m_SelectedPageNum = page_num + 1; UpdateData( false ); }