支持添加图片

This commit is contained in:
csh 2023-12-27 09:00:59 +08:00
parent 5a002f9767
commit ffbde2f964
7 changed files with 301 additions and 21 deletions

View File

@ -13,6 +13,8 @@ private
full_name_;
tables_;
paragraphs_;
tables_of_contents_;
inline_shapes_;
params_; // 存一些全局参数
@ -591,10 +593,10 @@ Begin
file_name_ := name;
full_name_ := fullname;
collection_ := collection;
tables_ := new TSDocxTables(self.Application, self.Creator, self);
tables.Init(docx_);
paragraphs_ := new TSDocxParagraphs(self.Application, self.Creator, self);
paragraphs_.Init(docx_);
tables_ := nil;
paragraphs_ := nil;
tables_of_contents_ := nil;
inline_shapes_ := nil;
params_ := array();
End;
@ -678,17 +680,40 @@ End;
Function TSDocxDocument.ReadTables(index);
Begin
if ifnil(tables_) then
begin
tables_ := new TSDocxTables(self.Application, self.Creator, self);
tables_.Init(docx_);
end
return ifnil(index) ? tables_ : tables.Item(index);
End;
Function TSDocxDocument.ReadParagraphs(index);
Begin
if ifnil(paragraphs_) then
begin
paragraphs_ := new TSDocxParagraphs(self.Application, self.Creator, self);
paragraphs_.Init(docx_);
end
return ifnil(index) ? paragraphs_ : paragraphs_.Item(index);
End;
Function TSDocxDocument.ReadTablesOfContents();
Begin
tables_of_contents_obj := new TSDocxTablesOfContents(self.Application, self.Create, self);
tables_of_contents_obj.Init(docx_);
return tables_of_contents_obj;
if ifnil(tables_of_contents_) then
begin
tables_of_contents_ := new TSDocxTablesOfContents(self.Application, self.Create, self);
tables_of_contents_.Init(docx_);
end
return tables_of_contents_;
End;
Function TSDocxDocument.ReadInlineShapes();
Begin
if ifnil(inline_shapes_) then
begin
inline_shapes_ := new TSDocxInlineShapes(self.Application, self.Create, self);
inline_shapes_.Init(docx_);
end
return inline_shapes_;
End;

125
docx/TSDocxInlineShape.tsf Normal file
View File

@ -0,0 +1,125 @@
Type TSDocxInlineShape = Class(TSVbaBase)
public
Function Init(docx, trun);
Function Apply();
private
docx_;
trun_;
base_;
public
// Methods
Function ConvertToShape();
Function Delete();
Function Reset();
Function Select();
// Properties
property AlternativeText read ReadAlternativeText write WriteAlternativeText;
property Borders read ReadBorders;
property Chart read ReadChart;
property Field read ReadField;
property Fill read ReadFill;
property Glow read ReadGlow;
property GroupItems read ReadGroupItems;
property HasChart read ReadHasChart;
property HasSmartArt read ReadHasSmartArt;
property Height read ReadHeight write WriteHeight;
property HorizontalLineFormat read ReadHorizontalLineFormat;
property Hyperlink read ReadHyperlink;
property IsPictureBullet read ReadIsPictureBullet;
property Line read ReadLine;
property LinkFormat read ReadLinkFormat;
property LinkFormat read ReadLinkFormat;
property LockAspectRatio read ReadLockAspectRatio write WriteLockAspectRatio;
property OLEFormat read ReadOLEFormat;
property PictureFormat read ReadPictureFormat;
property Range read ReadRange;
property Reflection read ReadReflection;
property ScaleHeight read ReadScaleHeight write WriteScaleHeight;
property ScaleWidth read ReadScaleWidth write WriteScaleWidth;
property Script read ReadScript;
property Shadow read ReadShadow;
property SmartArt read ReadSmartArt;
property SoftEdge read ReadSoftEdge;
property TextEffect read ReadTextEffect;
property Title read ReadTitle write WriteTitle;
property Type read ReadType;
property Width read ReadWidth write WriteWidth;
Function WriteWidth(value);
Function ReadWidth();
Function ReadType();
Function WriteTitle(value);
Function ReadTitle();
Function ReadTextEffect();
Function ReadSoftEdge();
Function ReadSmartArt();
Function ReadShadow();
Function ReadScript();
Function WriteScaleWidth(value);
Function ReadScaleWidth();
Function WriteScaleHeight(value);
Function ReadScaleHeight();
Function ReadReflection();
Function ReadRange();
Function ReadPictureFormat();
Function ReadOLEFormat();
Function WriteLockAspectRatio(value);
Function ReadLockAspectRatio();
Function ReadLinkFormat();
Function ReadLine();
Function ReadIsPictureBullet();
Function ReadHyperlink();
Function ReadHorizontalLineFormat();
Function WriteHeight(value);
Function ReadHeight();
Function ReadHasSmartArt();
Function ReadHasChart();
Function ReadGroupItems();
Function ReadGlow();
Function ReadFill();
Function ReadField();
Function ReadChart();
Function ReadBorders();
Function WriteAlternativeText(value);
Function ReadAlternativeText();
End;
// ============== 实现 ================= //
Function TSDocxInlineShape.Init(docx, trun);
Begin
docx_ := docx;
trun_ := trun;
base_ := 12700;
End;
Function TSDocxInlineShape.Apply();
Begin
trun_.Update();
End;
// Properties
Function TSDocxInlineShape.WriteHeight(value);
Begin
cy := value * base_;
trun_.Drawing.WInline.cy := cy;
trun_.Drawing.WInline.Graphic.GraphicData.Pic.spPr.XFrm.Ext.CY := cy;
End;
Function TSDocxInlineShape.ReadHeight();
Begin
return trun_.Drawing.WInline.Value("cy") / base_;
End;
Function TSDocxInlineShape.WriteWidth(value);
Begin
cx := value * base_;
trun_.Drawing.WInline.cx := cx;
trun_.Drawing.WInline.Graphic.GraphicData.Pic.spPr.XFrm.Ext.CX := cx;
End;
Function TSDocxInlineShape.ReadWidth();
Begin
return trun_.Drawing.WInline.Value("cx") / base_;
End;

117
docx/TSDocxInlineShapes.tsf Normal file
View File

@ -0,0 +1,117 @@
Type TSDocxInlineShapes = Class(TSVbaBase)
public
Function Init(docx);
Function Operator[](index);
Function SerializeInlineShapes();
Function FindInlineShapePosition(node);
private
docx_;
inline_shapes_;
document_;
public
// Methods
Function AddChart2(Style, Type, Range, NewLayout);
Function AddHorizontalLine(FileName, Range);
Function AddHorizontalLineStandard(Range);
Function AddOLEControl(ClassType, Range);
Function AddOLEObject(ClassType, FileName, LinkToFile, DisplayAsIcon, IconFileName, IconIndex, IconLabel, Range);
Function AddPicture(FileName, LinkToFile, SaveWithDocument, Range);
Function AddPictureBullet(FileName, Range);
Function AddSmartArt(Layout, Range);
Function AddWebVideo(EmbedCode, VideoWidth, VideoHeight, PosterFrameImage, Url, Range);
Function Item(Index);
Function New(Range);
// Properties
property Count read ReadCount;
Function ReadCount();
End;
// ============== 实现 ================= //
Function TSDocxInlineShapes.Init(docx);
Begin
docx_ := docx;
inline_shapes_ := array();
document_ := self.Parent;
End;
Function Operator TSDocxInlineShapes.[](index);
Begin
inline_shape_obj := new TSDocxInlineShape(self.Application, self.Creator, self.Parent);
inline_shape_obj.Init(docx_, inline_shapes_[index - 1]);
return inline_shape_obj;
End;
Function TSDocxInlineShapes.SerializeInlineShapes();
Begin
paragraphs := document_.Paragraphs;
num := paragraphs.Count;
for i:=1 to num do
begin
paragraph := paragraphs.Item(i);
node := paragraph.Object();
if ifObj(node) then
begin
run := node.FirstChildElement("w:r");
while ifObj(run) do
begin
drawing := run.FirstChildElement("w:drawing");
if ifObj(drawing) then
begin
trun := TOfficeObj("TRun");
trun.Init(run);
inline_shapes_[length(inline_shapes_)] := trun;
end
run := run.NextElement("w:r");
end
end
end
End;
// Methods
Function TSDocxInlineShapes.AddPicture(FileName, LinkToFile, SaveWithDocument, Range);
Begin
if not ifBinary(FileName) then raise "TSVBA::Parameter LinkToFile must be in binary.";
picture := TOfficeObj("TPicture");
picture.Image := FileName;
if ifObj(Range) then
begin
paragraph := Range.InsertNewParagraph();
pic := docx_.AddPicture(picture, paragraph.Object());
document_.Paragraphs.AddParagraph(paragraph.Index(), pic.Root());
paragraph.Range.Clear(true);
self.SerializeInlineShapes();
run := pic.Root().FirstChildElement("w:r");
trun := TOfficeObj("TRun");
trun.Init(run);
end
else begin
last := document_.Paragraphs.Item(document_.Paragraphs.Count);
pic := docx_.AddPicture(picture, last.Object());
document_.Paragraphs.AddParagraph(nil, pic.Root());
run := pic.Root().FirstChildElement("w:r");
trun := TOfficeObj("TRun");
trun.Init(run);
inline_shapes_[length(inline_shapes_)] := trun;
end
inline_shape_obj := new TSDocxInlineShape(self.Application, self.Creator, self.Parent);
inline_shape_obj.Init(docx_, trun);
return inline_shape_obj;
End;
Function TSDocxInlineShapes.Item(Index);
Begin
return self[Index];
End;
// Properties
Function TSDocxInlineShapes.ReadCount();
Begin
return length(inline_shapes_);
End;

View File

@ -3,6 +3,7 @@ Type TSDocxParagraph = Class(TSDocxParagraphFormat)
public
Function Init(docx, paragraph, index);
Function Object();
Function Index();
private
paragraph_; // TOfficeObj("TParagraph")
@ -63,6 +64,11 @@ Begin
return paragraph_;
End;
Function TSDocxParagraph.Index();
Begin
return index_;
End;
// Methods
Function TSDocxParagraph.Next(Count);
Begin

View File

@ -7,7 +7,8 @@ public
Function ParseTable(root, arr, lastNode);
Function FindNewParagraphPosition(findNode, direction);
Function AddParagraph(findNode, newNode, direction);
Function AddParagraph(pos, newNode);overload;
Function AddParagraph(findNode, newNode, direction);overload;
Function AddTableToParagraphs(findNode, tTable);
Function DeleteParagraph(findNode);
@ -23,8 +24,8 @@ public
Function DecreaseSpacing();
Function IncreaseSpacing();
Function Indent();
Function IndentCharWidth(Count);
Function IndentFirstLineCharWidth(Count);
Function IndentCharWidth(_Count);
Function IndentFirstLineCharWidth(_Count);
Function Item(Index);
Function OpenOrCloseUp();
Function OpenUp();
@ -36,8 +37,8 @@ public
Function Space1();
Function Space15();
Function Space2();
Function TabHangingIndent(Count);
Function TabIndent(Count);
Function TabHangingIndent(_Count);
Function TabIndent(_Count);
// Properties
property AddSpaceBetweenFarEastAndAlpha read ReadAddSpaceBetweenFarEastAndAlpha write WriteAddSpaceBetweenFarEastAndAlpha;
@ -220,15 +221,21 @@ Begin
return pos;
End;
Function TSDocxParagraphs.AddParagraph(findNode, newNode, direction);
Function TSDocxParagraphs.AddParagraph(pos, newNode);overload;
Begin
pos := self.FindNewParagraphPosition(findNode, direction);
if ifnil(pos) then pos := length(paragraphs_);
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;
End;
Function TSDocxParagraphs.AddParagraph(findNode, newNode, direction);overload;
Begin
pos := self.FindNewParagraphPosition(findNode, direction);
self.AddParagraph(pos, newNode);
return pos;
End;

View File

@ -6,8 +6,8 @@ public
Function AddParagraph();overload;
Function AddParagraph(direction);overload;
Function AddTable(numRows, numColumns);
Function InsertNewParagraph();
Function Clear(deleteAll);
Function InsertPara();
private
Function CopyRunFormat(sourceRun, destRun);
@ -361,7 +361,7 @@ Begin
return document_.Paragraphs.Item(pos + 1);
End;
Function TSDocxRange.InsertPara();
Function TSDocxRange.InsertNewParagraph();
Begin
case collapse_ of
TSDocxEnumerations.wdCollapseStart():
@ -555,7 +555,7 @@ Begin
case Type of
TSDocxEnumerations.wdColumnBreak():
begin
paragraph_obj := self.InsertPara();
paragraph_obj := self.InsertNewParagraph();
if collapse_ = TSDocxEnumerations.wdCollapseStart() then paragraph_obj := paragraph_obj.Next(1);
obj := paragraph_obj.Object();
run := obj.PrependRun();
@ -574,8 +574,8 @@ Begin
TSDocxEnumerations.wdPageBreak():
begin
paragraph_obj := self.InsertPara();
paragraph_obj := self.InsertPara();
paragraph_obj := self.InsertNewParagraph();
paragraph_obj := self.InsertNewParagraph();
paragraph_obj.Format.Alignment := TSDocxEnumerations.wdAlignParagraphLeft();
paragraph_obj.Format.WidowControl := true;
paragraph_obj.Format.Apply();
@ -609,7 +609,7 @@ End;
Function TSDocxRange.InsertParagraph();
Begin
self.InsertPara();
self.InsertNewParagraph();
End;
Function TSDocxRange.InsertParagraphAfter();

View File

@ -31,7 +31,7 @@ End;
// Methods
Function TSDocxTablesOfContents.Add(Range, UseHeadingStyles, UpperHeadingLevel, LowerHeadingLevel, UseFields, TableID, RightAlignPageNumbers, IncludePageNumbers, AddedStyles, UseHyperlinks, HidePageNumbersInWeb, UseOutlineLevels);
Begin
paragraph := Range.InsertPara();
paragraph := Range.InsertNewParagraph();
if ifnil(UpperHeadingLevel) then UpperHeadingLevel := 1;
if ifnil(LowerHeadingLevel) then LowerHeadingLevel := 9;
docx_.AddTableContent(paragraph.Object(), UpperHeadingLevel, LowerHeadingLevel);