306 lines
10 KiB
Plaintext
306 lines
10 KiB
Plaintext
Type TSDocxParagraphs = Class(TSVbaBase)
|
|
|
|
public
|
|
Function Init(docx);
|
|
Function Operator[](index);
|
|
Function SerializeParagraphs(root, arr, lastNode);
|
|
Function ParseTable(root, arr, lastNode);
|
|
|
|
Function FindNewParagraphPosition(findNode, direction);
|
|
Function AddParagraph(findNode, newNode, direction);
|
|
Function AddTableToParagraphs(findNode, tTable);
|
|
Function DeleteParagraph(findNode);
|
|
|
|
private
|
|
docx_;
|
|
lastnode_;
|
|
paragraphs_; // TOfficeObj("TParagraph")数组
|
|
|
|
public
|
|
// Methods
|
|
Function Add(Range);
|
|
Function CloseUp();
|
|
Function DecreaseSpacing();
|
|
Function IncreaseSpacing();
|
|
Function Indent();
|
|
Function IndentCharWidth(Count);
|
|
Function IndentFirstLineCharWidth(Count);
|
|
Function Item(Index);
|
|
Function OpenOrCloseUp();
|
|
Function OpenUp();
|
|
Function Outdent();
|
|
Function OutlineDemote();
|
|
Function OutlineDemoteToBody();
|
|
Function OutlinePromote();
|
|
Function Reset();
|
|
Function Space1();
|
|
Function Space15();
|
|
Function Space2();
|
|
Function TabHangingIndent(Count);
|
|
Function TabIndent(Count);
|
|
|
|
// Properties
|
|
property AddSpaceBetweenFarEastAndAlpha read ReadAddSpaceBetweenFarEastAndAlpha write WriteAddSpaceBetweenFarEastAndAlpha;
|
|
property AddSpaceBetweenFarEastAndDigit read ReadAddSpaceBetweenFarEastAndDigit write WriteAddSpaceBetweenFarEastAndDigit;
|
|
property Alignment read ReadAlignment write WriteAlignment;
|
|
property AutoAdjustRightIndent read ReadAutoAdjustRightIndent write WriteAutoAdjustRightIndent;
|
|
property BaseLineAlignment read ReadBaseLineAlignment write WriteBaseLineAlignment;
|
|
property Borders read ReadBorders;
|
|
property CharacterUnitFirstLineIndent read ReadCharacterUnitFirstLineIndent write WriteCharacterUnitFirstLineIndent;
|
|
property CharacterUnitLeftIndent read ReadCharacterUnitLeftIndent write WriteCharacterUnitLeftIndent;
|
|
property CharacterUnitRightIndent read ReadCharacterUnitRightIndent write WriteCharacterUnitRightIndent;
|
|
property Count read ReadCount;
|
|
property DisableLineHeightGrid read ReadDisableLineHeightGrid write WriteDisableLineHeightGrid;
|
|
property FarEastLineBreakControl read ReadFarEastLineBreakControl write WriteFarEastLineBreakControl;
|
|
property First read ReadFirst;
|
|
property FirstLineIndent read ReadFirstLineIndent write WriteFirstLineIndent;
|
|
property Format read ReadFormat write WriteFormat;
|
|
property HalfWidthPunctuationOnTopOfLine read ReadHalfWidthPunctuationOnTopOfLine write WriteHalfWidthPunctuationOnTopOfLine;
|
|
property HangingPunctuation read ReadHangingPunctuation write WriteHangingPunctuation;
|
|
property Hyphenation read ReadHyphenation write WriteHyphenation;
|
|
property KeepTogether read ReadKeepTogether write WriteKeepTogether;
|
|
property KeepWithNext read ReadKeepWithNext write WriteKeepWithNext;
|
|
property Last read ReadLast;
|
|
property LeftIndent read ReadLeftIndent write WriteLeftIndent;
|
|
property LineSpacing read ReadLineSpacing write WriteLineSpacing;
|
|
property LineSpacingRule read ReadLineSpacingRule write WriteLineSpacingRule;
|
|
property LineUnitAfter read ReadLineUnitAfter write WriteLineUnitAfter;
|
|
property LineUnitBefore read ReadLineUnitBefore write WriteLineUnitBefore;
|
|
property NoLineNumber read ReadNoLineNumber write WriteNoLineNumber;
|
|
property OutlineLevel read ReadOutlineLevel write WriteOutlineLevel;
|
|
property PageBreakBefore read ReadPageBreakBefore write WritePageBreakBefore;
|
|
property ReadingOrder read ReadReadingOrder write WriteReadingOrder;
|
|
property RightIndent read ReadRightIndent write WriteRightIndent;
|
|
property Shading read ReadShading;
|
|
property SpaceAfter read ReadSpaceAfter write WriteSpaceAfter;
|
|
property SpaceAfterAuto read ReadSpaceAfterAuto write WriteSpaceAfterAuto;
|
|
property SpaceBefore read ReadSpaceBefore write WriteSpaceBefore;
|
|
property SpaceBeforeAuto read ReadSpaceBeforeAuto write WriteSpaceBeforeAuto;
|
|
property Style read ReadStyle write WriteStyle;
|
|
property TabStops read ReadTabStops write WriteTabStops;
|
|
property WidowControl read ReadWidowControl write WriteWidowControl;
|
|
property WordWrap read ReadWordWrap write WriteWordWrap;
|
|
Function WriteWordWrap(value);
|
|
Function ReadWordWrap();
|
|
Function WriteWidowControl(value);
|
|
Function ReadWidowControl();
|
|
Function WriteTabStops(value);
|
|
Function ReadTabStops();
|
|
Function WriteStyle(value);
|
|
Function ReadStyle();
|
|
Function WriteSpaceBeforeAuto(value);
|
|
Function ReadSpaceBeforeAuto();
|
|
Function WriteSpaceBefore(value);
|
|
Function ReadSpaceBefore();
|
|
Function WriteSpaceAfterAuto(value);
|
|
Function ReadSpaceAfterAuto();
|
|
Function WriteSpaceAfter(value);
|
|
Function ReadSpaceAfter();
|
|
Function ReadShading();
|
|
Function WriteRightIndent(value);
|
|
Function ReadRightIndent();
|
|
Function WriteReadingOrder(value);
|
|
Function ReadReadingOrder();
|
|
Function WritePageBreakBefore(value);
|
|
Function ReadPageBreakBefore();
|
|
Function WriteOutlineLevel(value);
|
|
Function ReadOutlineLevel();
|
|
Function WriteNoLineNumber(value);
|
|
Function ReadNoLineNumber();
|
|
Function WriteLineUnitBefore(value);
|
|
Function ReadLineUnitBefore();
|
|
Function WriteLineUnitAfter(value);
|
|
Function ReadLineUnitAfter();
|
|
Function WriteLineSpacingRule(value);
|
|
Function ReadLineSpacingRule();
|
|
Function WriteLineSpacing(value);
|
|
Function ReadLineSpacing();
|
|
Function WriteLeftIndent(value);
|
|
Function ReadLeftIndent();
|
|
Function ReadLast();
|
|
Function WriteKeepWithNext(value);
|
|
Function ReadKeepWithNext();
|
|
Function WriteKeepTogether(value);
|
|
Function ReadKeepTogether();
|
|
Function WriteHyphenation(value);
|
|
Function ReadHyphenation();
|
|
Function WriteHangingPunctuation(value);
|
|
Function ReadHangingPunctuation();
|
|
Function WriteHalfWidthPunctuationOnTopOfLine(value);
|
|
Function ReadHalfWidthPunctuationOnTopOfLine();
|
|
Function WriteFormat(value);
|
|
Function ReadFormat();
|
|
Function WriteFirstLineIndent(value);
|
|
Function ReadFirstLineIndent();
|
|
Function ReadFirst();
|
|
Function WriteFarEastLineBreakControl(value);
|
|
Function ReadFarEastLineBreakControl();
|
|
Function WriteDisableLineHeightGrid(value);
|
|
Function ReadDisableLineHeightGrid();
|
|
Function ReadCount();
|
|
Function WriteCharacterUnitRightIndent(value);
|
|
Function ReadCharacterUnitRightIndent();
|
|
Function WriteCharacterUnitLeftIndent(value);
|
|
Function ReadCharacterUnitLeftIndent();
|
|
Function WriteCharacterUnitFirstLineIndent(value);
|
|
Function ReadCharacterUnitFirstLineIndent();
|
|
Function ReadBorders();
|
|
Function WriteBaseLineAlignment(value);
|
|
Function ReadBaseLineAlignment();
|
|
Function WriteAutoAdjustRightIndent(value);
|
|
Function ReadAutoAdjustRightIndent();
|
|
Function WriteAlignment(value);
|
|
Function ReadAlignment();
|
|
Function WriteAddSpaceBetweenFarEastAndDigit(value);
|
|
Function ReadAddSpaceBetweenFarEastAndDigit();
|
|
Function WriteAddSpaceBetweenFarEastAndAlpha(value);
|
|
Function ReadAddSpaceBetweenFarEastAndAlpha();
|
|
|
|
End;
|
|
|
|
// ============== 实现 ================= //
|
|
Function TSDocxParagraphs.Init(docx);
|
|
Begin
|
|
docx_ := docx;
|
|
paragraphs_ := array();
|
|
lastnode_ := nil;
|
|
self.SerializeParagraphs(docx_.Body().Node(), paragraphs_, lastnode_);
|
|
End;
|
|
|
|
Function TSDocxParagraphs.SerializeParagraphs(root, arr, lastNode);
|
|
Begin
|
|
node := root.FirstChildElement();
|
|
while ifObj(node) do
|
|
begin
|
|
name := node.GetName();
|
|
if name = "w:p" then
|
|
begin
|
|
obj := TOfficeObj("TParagraph");
|
|
obj.Init(node);
|
|
arr[length(arr)] := obj;
|
|
lastNode := node;
|
|
end
|
|
else if name = "w:tbl" then
|
|
begin
|
|
self.ParseTable(node, arr, lastNode);
|
|
lastNode := node;
|
|
end
|
|
node := node.NextElement();
|
|
End;
|
|
End;
|
|
|
|
Function TSDocxParagraphs.ParseTable(root, arr, lastNode);
|
|
Begin
|
|
tr := root.FirstChildElement("w:tr");
|
|
while ifObj(tr) do
|
|
begin
|
|
tc := tr.FirstChildElement("w:tc");
|
|
while ifObj(tc) do
|
|
begin
|
|
SerializeParagraphs(tc, arr, lastNode);
|
|
tc := tc.NextElement("w:tc");
|
|
end
|
|
arr[length(arr)] := TOfficeObj("TParagraph");
|
|
tr := tr.NextElement("w:tr");
|
|
end
|
|
End;
|
|
|
|
Function TSDocxParagraphs.FindNewParagraphPosition(findNode, direction);
|
|
Begin
|
|
pos := length(paragraphs_);
|
|
for i:=0 to length(paragraphs_)-1 do
|
|
begin
|
|
if not ifObj(paragraphs_[i].Root()) then continue;
|
|
if paragraphs_[i].Root().Eq(findNode) then
|
|
begin
|
|
pos := direction = "after" ? i + 1 : i;
|
|
break;
|
|
end
|
|
end
|
|
return pos;
|
|
End;
|
|
|
|
Function TSDocxParagraphs.AddParagraph(findNode, newNode, direction);
|
|
Begin
|
|
pos := self.FindNewParagraphPosition(findNode, direction);
|
|
if pos = length(paragraphs_) then lastnode_ := newNode;
|
|
for i:=length(paragraphs_) downto pos do
|
|
paragraphs_[i] := paragraphs_[i-1];
|
|
obj := TOfficeObj("TParagraph");
|
|
obj.Init(newNode);
|
|
paragraphs_[pos] := obj;
|
|
return pos;
|
|
End;
|
|
|
|
Function TSDocxParagraphs.AddTableToParagraphs(findNode, tTable);
|
|
Begin
|
|
pos := self.FindNewParagraphPosition(findNode, "after");
|
|
// 序列化表格为段落
|
|
node := tTable.Root();
|
|
if pos = length(paragraphs_) then lastnode_ := node;
|
|
arr := array();
|
|
last_node := nil;
|
|
self.ParseTable(node, arr, last_node);
|
|
len := length(arr);
|
|
for i:=length(paragraphs_)-1 downto pos do
|
|
paragraphs_[i+len] := paragraphs_[i];
|
|
for i:=pos to pos + len - 1 do
|
|
paragraphs_[i] := arr[i-pos];
|
|
End;
|
|
|
|
Function TSDocxParagraphs.DeleteParagraph(findNode);
|
|
Begin
|
|
pos := self.FindNewParagraphPosition(findNode, "before");
|
|
if ifObj(findNode) then findNode.Parent().DeleteChild(findNode);
|
|
for i:=pos to length(paragraphs_)-2 do
|
|
paragraphs_[i] := paragraphs_[i+1];
|
|
reindex(paragraphs_, array(length(paragraphs_)-1 : nil));
|
|
End;
|
|
|
|
Function Operator TSDocxParagraphs.[](index);
|
|
Begin
|
|
paragraph_obj := new TSDocxParagraph(self.Application, self.Creator, self.Parent);
|
|
paragraph_obj.Init(docx_, paragraphs_[index - 1], index);
|
|
return paragraph_obj;
|
|
End;
|
|
|
|
// Methods
|
|
Function TSDocxParagraphs.Add(Range);
|
|
Begin
|
|
if Range is Class(TSDocxRange) then
|
|
begin
|
|
Range.AddParagraph();
|
|
end
|
|
else begin
|
|
if lastnode_ then
|
|
node := docx_.Body().Root().InsertAfterChild(lastnode_, "element", "w:p");
|
|
else
|
|
node := docx_.Body().Root().InsertEndChild("element", "w:p");
|
|
lastnode_ := node;
|
|
obj := TOfficeObj("TParagraph");
|
|
obj.Init(node);
|
|
paragraphs_[length(paragraphs_)] := obj;
|
|
return self.Item(length(paragraphs_) - 1);
|
|
end
|
|
End;
|
|
|
|
Function TSDocxParagraphs.Item(Index);
|
|
Begin
|
|
return self[Index];
|
|
End;
|
|
|
|
Function TSDocxParagraphs.ReadCount();
|
|
Begin
|
|
return length(paragraphs_);
|
|
End;
|
|
|
|
Function TSDocxParagraphs.ReadFirst();
|
|
Begin
|
|
return self.Item(1);
|
|
End;
|
|
|
|
Function TSDocxParagraphs.ReadLast();
|
|
Begin
|
|
return self.Item(self.Count);
|
|
End;
|