1、删除受保护的pptx文件标志
解压pptx文件,解析ppt/presentation.xml,将文件中的p:modifyVerifier节点删除
bool DeletePPTXFileModifyVerify(std::string strPresentationXML)
{
bool result = true;
do
{
tinyxml2::XMLDocument doc;
int nRet = doc.LoadFile(strPresentationXML.c_str());
if(nRet != tinyxml2::XML_SUCCESS)
{
result = false;
break;
}
tinyxml2::XMLElement* pModifyVerifier = doc.FirstChildElement("p:modifyVerifier");
if(!pModifyVerifier)
{
break;
}
doc.DeleteChild(pModifyVerifier);
doc.SaveFile(strPresentationXML.c_str());
}while(false);
return result;
}
2、删除只读的pptx标志
解压pptx文件,解析docProps/custom.xml文件,将文件中property中的name值为_MarkAsFinal设置成false
bool SetPPTXFileMarkAsFinal(std::string strCustomXML)
{
bool result = false;
tinyxml2::XMLDocument doc;
int nRet = doc.LoadFile(strCustomXML.c_str());
if(nRet != tinyxml2::XML_SUCCESS)
{
return result;
}
tinyxml2::XMLElement* pProperties = doc.FirstChildElement("Properties");
if(!pProperties)
{
return result;
}
tinyxml2::XMLElement* pProperty = pProperties->FirstChildElement("property");
while(pProperty != NULL)
{
std::string strValue = pProperty->Attribute("name");
if(strValue == "_MarkAsFinal")
{
tinyxml2::XMLElement* pPropertyBool = pProperty->FirstChildElement("vt:bool");
if(pPropertyBool)
{
std::string strText = pPropertyBool->GetText();
if(strText != "false")
{
pPropertyBool->SetText("false");
doc.SaveFile(strCustomXML.c_str());
result = true;
}
break;
}
}
pProperty = pProperty->NextSiblingElement();
}
return result;
}
3、解除pptx每一页文件中的ShapeLocks、PictureLocks、GroupShapeLocks标志
bool CheckXMLAttributeNeedChange(tinyxml2::XMLDocument* doc, const char* name)
{
bool bChangeed = false;
tinyxml2::XMLElement* xmlElement = doc->FirstChildElement(name);
while (xmlElement != NULL)
{
for (int i = 0; i < this->m_SlideLocksAttributeNames.size(); i++)
{
std::string strAttributeName = this->m_SlideLocksAttributeNames[i];
std::string strValue = xmlElement->Attribute(strAttributeName.c_str());
if (strValue != "true")
{
if (strValue != "1")
{
continue;
}
}
bChangeed = true;
xmlElement->SetAttribute(strAttributeName.c_str(), "0");
}
}
return bChangeed;
}
bool CheckPPTXSlideXML(std::string strBaseDir)
{
bool bHasChanged = false;
CString csDir = strBaseDir.c_str();
std::set<std::string> DirSet;
MyFile::SEARCHFILE_MAP FilePathMap;
MyFile::GetFileList(csDir, "ppt/slides/slide", DirSet, FilePathMap, false);
MyFile::SEARCHFILE_MAP::iterator iter = FilePathMap.begin();
while(iter != FilePathMap.end())
{
std::string strFileExt = util::GetExtension(iter->second.strFileName.c_str());
if(strFileExt != ".xml")
{
iter = FilePathMap.erase(iter);
}
else
{
iter++;
}
}
//解析ppt/slides/slide开头和.xml结尾的文件,就是各ppt页的xml文件,判断页xml文件中是否有"noUngrp"、"noSelect"、"noMove"、"noResize"、"noTextEdit"项
iter = FilePathMap.begin();
for (; iter != FilePathMap.end(); iter++)
{
bool bChangeed = false;
tinyxml2::XMLDocument doc;
if(doc.LoadFile(iter->second.strSrcFilePath.c_str()) == tinyxml2::XML_SUCCESS)
{
bool bSpLockChange = CheckXMLAttributeNeedChange(&doc, "a:spLocks");
bool bPicLockChange = CheckXMLAttributeNeedChange(&doc, "a:picLocks");
bool bGrpSpLockChange = CheckXMLAttributeNeedChange(&doc, "a:grpSpLocks");
bChangeed = bSpLockChange || bPicLockChange || bGrpSpLockChange;
}
if(bChangeed)
{
bHasChanged = true;
doc.SaveFile(iter->second.strSrcFilePath.c_str());
}
}
return bHasChanged;
}