textState = textObject.GetTextState(page);
# 获取词间距
wordSpace = textState.wordspace;
# 获取当前字体的空格原始宽度
spaceWidth_original = textState.font.GetCharWidth(ord(' ')) / 1000;
# 根据词间距spaceWidth_original,字体大小font_size,
#以及文本的横向缩放Matrix.a比例, 计算出实际PDF中 该文本对象的空格实际宽度
spaceWidth = 1 * spaceWidth_original * textState.font_size * textRow福昕PDF SDK 软件开发工具包 > 福昕PDF SDK(桌面/服务器).GetMatrix().a + wordSpace
计算单个文本对象,字符所占的左右两侧的坐标。
#左侧坐标:
textObj.GetCharPos(0).x;
#右侧坐标:
textObj.GetCharPos(textObj.GetCharCount()-1).x+textObj.GetCharWidthByIndex(textObj.GetCharCount()-1);
完整代码:
def GetTextObjBoundary(textObj,is_left=True):
if is_left:
return textObj.GetCharPos(0).x;
else:
return textObj.GetCharPos(textObj.GetCharCount()-1).x+textObj.GetCharWidthByIndex(textObj.GetCharCount()-1);
#textRow为一行已从左往右排好序的文本对象数组。
def GetTextInRow(textRow, page):
str=""
prePosX=0;
textRow = sorted(textRow, key=lambda x: x.GetRect().left, reverse=False);
for i in range(0, len(textRow)):
textStr=textRow福昕PDF SDK 软件开发工具包 > 福昕PDF SDK(桌面/服务器).GetText();
rect=textRow福昕PDF SDK 软件开发工具包 > 福昕PDF SDK(桌面/服务器).GetRect()
textObj=textRow福昕PDF SDK 软件开发工具包 > 福昕PDF SDK(桌面/服务器)
if i==0:
str=textStr;
prePosX = GetTextObjBoundary(textObj,False);
continue;
distance=GetTextObjBoundary(textObj,True)-prePosX
GetTextObjBoundary(textObj,True);
textState = (textRow福昕PDF SDK 软件开发工具包 > 福昕PDF SDK(桌面/服务器).GetTextState(page));
# 获取词间距
wordSpace = textState.wordspace;
# 获取当前字体的空格原始宽度
spaceWidth_original = textState.font.GetCharWidth(ord(' ')) / 1000;
# 根据词间距spaceWidth_original,字体大小font_size,文本的横向缩放Matrix.a
# 计算出实际PDF中 该文本对象的空格宽度
spaceWidth = 1 * spaceWidth_original * textState.font_size * textRow福昕PDF SDK 软件开发工具包 > 福昕PDF SDK(桌面/服务器).GetMatrix().a + wordSpace
#根据相邻文本的间距,来计算空格符的个数
spaceCount=(int)(distance/spaceWidth+0.5)
for space in range(0,spaceCount):
str=str+" "
str=str+textStr;
prePosX = GetTextObjBoundary(textObj,False);
return str;