PPT转H5系列(二)----预处理pptx文件

文章介绍了如何通过编程技术,包括使用TinyXML2库,删除PowerPointPPTX文件的受保护标志(如修改器验证)、只读状态以及解除每一页中的ShapeLocks、PictureLocks和GroupShapeLocks。
摘要由CSDN通过智能技术生成

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;
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值