接前一篇,已经通过COM调用PowerPoint打开了PPT文件,然后就是循环解析每一页的文本信息,来获取文字使用的字体名称
vector<CSlideTextFontInfo> GetPresentationTextFontInfo(CPPTPresentation& presentation_powerpoint)
{
vector<CSlideTextFontInfo> presentationTextFontInfo;
PresentationPreprocessor::RGBGen rgbgen;
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CPPTSlideRange slideRange(CPPTSlides(presentation_powerpoint.get_Slides()).Range(covOptional));
int count = slideRange.get_Count();
try
{
for (int i = count; i >= 1; i--)
{
CSlideTextFontInfo slideFontInfo1;
CPPTSlide slide(slideRange.Item(COleVariant((LONG)i)));
slideFontInfo1.m_nSlideID = slide.get_SlideID();
CPPTSlideRange DupSlideRange(slide.Duplicate());
CPPTSlide slide2(DupSlideRange.Item(COleVariant((LONG)1)));
presentationTextFontInfo.push_back(slideFontInfo1);
CSlideTextFontInfo& slideFontInfo = presentationTextFontInfo[presentationTextFontInfo.size() - 1];
slide2.MoveTo(count + 1);
slideFontInfo.SlideIndex = i;
slideFontInfo.m_nDuplicateSlideID = slide2.get_SlideID();
try
{
CPPTHyperlinks hyperlinks(slide2.get_Hyperlinks());
for (int j = hyperlinks.get_Count(); j >= 1; j--)
{
CPPTHyperlink hyperlink(hyperlinks.Item(j));
hyperlink.Delete();
}
}
catch (COleDispatchException* pException)
{
char szMsg[1024] = {0};
pException->GetErrorMessage(szMsg, sizeof(szMsg) - 1);
pException->Delete();
}
catch (COleException* pException)
{
char szMsg[1024] = {0};
pException->GetErrorMessage(szMsg, sizeof(szMsg) - 1);
pException->Delete();
}
catch(...)
{
}
CPPTShapes shapes(slide2.get_Shapes());
int count2 = shapes.get_Count();
for (int k = 1; k <= count2; k++)
{
CPPTShape shape(shapes.Item(COleVariant((LONG)k)));
if (shape.get_HasTextFrame() == MsoTriState::msoTrue)
{
CPPTTextFrame textFrame(shape.get_TextFrame());
CPPTTextFrame2 textFrame2(shape.get_TextFrame2());
if (textFrame2.get_AutoSize() == MsoAutoSize::msoAutoSizeShapeToFitText)
{
textFrame2.put_AutoSize(MsoAutoSize::msoAutoSizeNone);
}
CShapeTextFontInfo shapeFontInfo;
shapeFontInfo.m_nShapeIndex = k;
if (textFrame2.get_HasText() == MsoTriState::msoTrue)
{
CPPTTextRange2 textRange(textFrame2.get_TextRange());
while (true)
{
std::string text = (LPCSTR)textRange.get_Text();
std::wstring wtext = util::StringToWString(text);
WCHAR value = L'\u2028';
int num = wtext.find(value);
if (num == std::wstring::npos)
{
break;
}
CPPTTextRange2 textRange2(textRange.get_Characters(num + 1, 1));
textRange2.put_Text(" ");
CPPTShapes shapes2(slide.get_Shapes());
CPPTShape shape2(shapes2.Item(COleVariant((LONG)k)));
CPPTTextFrame2 textFrame3(shape2.get_TextFrame2());
CPPTTextRange2 textRange3(textFrame3.get_TextRange());
CPPTTextRange2 textRange4(textRange3.get_Characters(num + 1, 1));
textRange4.put_Text(" ");
}
CPPTTextRange textRange5(textFrame.get_TextRange());
try
{
CPPTTextRange2 textRange6(textRange.get_Paragraphs(-1, -1));
int count3 = textRange6.get_Count();
for (int l = 1; l <= count3; l++)
{
CPPTTextRange2 textRange7 = textRange.get_Paragraphs(l, 1);
CPPTParagraphFormat2 paragraphFormat = textRange7.get_ParagraphFormat();
CPPTBulletFormat2 bulletFormat = paragraphFormat.get_Bullet();
bulletFormat.put_UseTextColor(MsoTriState::msoFalse);
MyColor::CColor color = rgbgen.NextColor();
color = MyColor::CColor::FromArgb((int)color.B, (int)color.G, (int)color.R);
CPPTFont2 font(bulletFormat.get_Font());
try
{
CPPTColorFormat(CPPTFillFormat(font.get_Fill()).get_ForeColor()).put_RGB(color.ToArgb());
}
catch (COleDispatchException* pException)
{
char szMsg[1024] = {0};
pException->GetErrorMessage(szMsg, sizeof(szMsg) - 1);
pException->Delete();
}
catch (COleException* pException)
{
char szMsg[1024] = {0};
pException->GetErrorMessage(szMsg, sizeof(szMsg) - 1);
pException->Delete();
}
catch(...)
{
CPPTTextRange textRange8(textRange5.Paragraphs(l, 1));
CPPTParagraphFormat paragraphFormat2(textRange8.get_ParagraphFormat());
CPPTBulletFormat bullet(paragraphFormat2.get_Bullet());
CPPTFont font2(bullet.get_Font());
CPPTColorFormat color2(font2.get_Color());
color2.put_RGB(color.ToArgb());
}
CParagraphFontInfo paragraphFontInfo;
paragraphFontInfo.m_nStart = l;
paragraphFontInfo.TempFontColor = color.ToArgb();
paragraphFontInfo.m_strFontName = font.get_Name();
shapeFontInfo.m_ParagraphFontInfo_Vec.push_back(paragraphFontInfo);
}
}
catch (COleDispatchException* pException)
{
char szMsg[1024] = {0};
pException->GetErrorMessage(szMsg, sizeof(szMsg) - 1);
pException->Delete();
}
catch (COleException* pException)
{
char szMsg[1024] = {0};
pException->GetErrorMessage(szMsg, sizeof(szMsg) - 1);
pException->Delete();
}
catch(...)
{
}
CPPTTextRange2 textRange9(textRange.get_Runs(-1, -1));
int count4 = textRange9.get_Count();
int nStartPos = 1;
std::vector<MyTextData> list2;
for (int m = 1; m <= count4; m++)
{
CPPTTextRange2 textRange10(textRange.get_Runs(m, 1));
CPPTTextRange2 textRange11(textRange10.get_Characters(-1, -1));
int count5 = textRange11.get_Count();
MsoLanguageID nlanguageID = (MsoLanguageID)textRange11.get_LanguageID();
if (IsChineseSimplified(nlanguageID) &&
IsFarEastFontNameNotEqualToFontName(textRange11))
{
for (int n = 0; n < count5; n++)
{
MyTextData data;
data.nStart = nStartPos;
data.nLen = 1;
list2.push_back(data);
nStartPos++;
}
}
else
{
MyTextData data;
data.nStart = nStartPos;
data.nLen = count5;
list2.push_back(data);
nStartPos += count5;
}
}
CPPTTextRange2 textRange12(textRange9.get_Characters(-1, -1));
int count6 = textRange12.get_Count();
{
for(int i = 0; i < list2.size(); i++)
{
MyTextData& data = list2[i];
if (data.nStart >= 1)
{
if (data.nStart + data.nLen - 1 <= count6)
{
CPPTTextRange2 textRange13(textRange9.get_Characters(data.nStart, data.nLen));
CPPTFont2 font3(textRange13.get_Font());
CTextRangeFontInfo textRangeFontInfo;
textRangeFontInfo.m_nStart = data.nStart;
textRangeFontInfo.m_nLen = data.nLen;
MsoLanguageID nlanguageID = (MsoLanguageID)textRange13.get_LanguageID();
if (IsKhmerLanguage(nlanguageID))
{
textRangeFontInfo.m_strFontName = font3.get_NameComplexScript();
}
else
{
if (IsChineseSimplified(nlanguageID))
{
if (IsCJKUnifiedIdeographs(textRange13))
{
textRangeFontInfo.m_strFontName = font3.get_NameFarEast();
goto IL_5FF;
}
}
textRangeFontInfo.m_strFontName = font3.get_Name();
}
IL_5FF:
MyColor::CColor color = rgbgen.NextColor();
color = MyColor::CColor::FromArgb((int)color.B, (int)color.G, (int)color.R);
try
{
CPPTFillFormat fill(font3.get_Fill());
CPPTColorFormat foreColor(fill.get_ForeColor());
foreColor.put_RGB(color.ToArgb());
}
catch (COleDispatchException* pException)
{
char szMsg[1024] = {0};
pException->GetErrorMessage(szMsg, sizeof(szMsg) - 1);
pException->Delete();
}
catch (COleException* pException)
{
char szMsg[1024] = {0};
pException->GetErrorMessage(szMsg, sizeof(szMsg) - 1);
pException->Delete();
}
catch(...)
{
CPPTTextRange textRange14(textFrame.get_TextRange());
CPPTTextRange textRange15(textRange14.Characters(data.nStart, data.nLen));
CPPTFont font4(textRange15.get_Font());
CPPTColorFormat color3(font4.get_Color());
color3.put_RGB(color.ToArgb());
}
textRangeFontInfo.TempFontColor = color.ToArgb();
shapeFontInfo.m_TextRangeFontInfo_Vec.push_back(textRangeFontInfo);
}
}
}
}
}
slideFontInfo.m_ShapeTextFontInfo_Vec.push_back(shapeFontInfo);
}
}
}
}
catch (COleDispatchException* pException)
{
char szMsg[1024] = {0};
pException->GetErrorMessage(szMsg, sizeof(szMsg) - 1);
pException->Delete();
}
catch (COleException* pException)
{
char szMsg[1024] = {0};
pException->GetErrorMessage(szMsg, sizeof(szMsg) - 1);
pException->Delete();
}
catch(...)
{
}
return presentationTextFontInfo;
}
本项目还在开发中,已经有能跑通的程序,但还不完善,欢迎有兴趣的朋友互相交流
测试程序下载地址:
https://download.csdn.net/download/cwp0508/88958928?spm=1001.2014.3001.5503