1. 修复表格遇到边界报错问题

2. 支持表格的奇数行、偶数行样式
This commit is contained in:
csh 2024-09-26 11:40:23 +08:00
parent 5dc201c1a2
commit 68ce759668
5 changed files with 58 additions and 11 deletions

View File

@ -12,9 +12,14 @@ public
function IsReComputeByCantSplit(): boolean;
function IfRemoveEmptyRectangle(): boolean;
private
function GetCellPrType(): string;
public
VMerge;
RemoveFlag;
Row;
Col;
private
[weakref]parent_: TSPdfTableRange;
@ -78,7 +83,7 @@ begin
if element.LocalName = "p" then
begin
range := new TSPdfParagraphRange(docx_to_pdf_, page_, docx_components_ware_, element);
range.SetExtraStyleId(tbl_pr_.TblStyle.Val);
range.SetTblStyleIdAndType(tbl_pr_.TblStyle.Val, {self.}GetCellPrType());
end
else if element.LocalName = "tbl" then
begin
@ -99,8 +104,16 @@ begin
page_ := range.GetLastPage();
end
end
{self.}EndY := cell_y;
{self.}EndY := cell_y - tbl_pr_.TblCellMar.Bottom.W;
{self.}DynamicHeight += tbl_pr_.TblCellMar.Top.W + tbl_pr_.TblCellMar.Bottom.W;
if {self.}EndY < {self.}LowerBound then
begin
page_ := docx_to_pdf_.GetNextPage(page_);
if ifnil(page_) then page_ := docx_to_pdf_.AddTSPage();
point := docx_to_pdf_.GetCurrentTextPoint();
{self.}StartY := point.Y;
{self.}Calc();
end
end;
function TSPdfCellRange.Do();override;
@ -226,3 +239,10 @@ begin
if length(region_array_) < 2 then return false;
return length(region_array_[0].RangeArr) ? false : true;
end;
function TSPdfCellRange.GetCellPrType(): string;
begin
if {self.}Row = 0 then return "firstRow";
else if ({self.}Row + 1) % 2 = 0 then return "band1Horz";
else return "band2Horz";
end;

View File

@ -3,7 +3,7 @@ public
function Create(docx_to_pdf: TSDocxToPdf; pg: TSPage; components: TSDocxComponentsWare; paragraph: P);
function Calc();
function Do();override;
function SetExtraStyleId(style_id: string);
function SetTblStyleIdAndType(style_id: string; type: string);
function SetNumPages(num: integer);
function RangesToLines();
function FirstValidTSPage(): TSPage;
@ -17,6 +17,7 @@ private
function SetPPrByStyleId(var ppr: PPr; style_id: string);
function SetRPr(var rpr; ppr: PPrUnitDecorator);
function SetRPrByStyleId(var rpr: RPr; style_id: string);
function SetRPrByTblStyleId(var rpr: RPr; style_id: string);
function SetLvlText();
function GetImageFileType(data: binary): string;
function GetImageData(id: string): PdfImage;
@ -43,13 +44,14 @@ private
[weakref]docx_components_ware_: TSDocxComponentsWare;
[weakref]paragraph_: P;
[weakref]page_: TSPage;
extra_style_id_: string;
range_array_: array of TSPdfBasicRange;
line_range_array_: array of TSPdfLineRange;
hyperlink_array_: tableArray;
bookmark_array_: tableArray;
ppr_unit_decorator_: PPrUnitDecorator;
placeholder_array_: tableArray;
table_style_id_: string;
table_style_type_: string;
end;
function TSPdfParagraphRange.Create(docx_to_pdf: TSDocxToPdf; pg: TSPage; components: TSDocxComponentsWare; paragraph: P);
@ -58,7 +60,8 @@ begin
page_ := pg;
docx_components_ware_ := components;
paragraph_ := paragraph;
extra_style_id_ := "";
table_style_id_ := "";
table_style_type_ := "";
range_array_ := array();
line_range_array_ := array();
hyperlink_array_ := array();
@ -286,9 +289,10 @@ begin
line_range.Do();
end;
function TSPdfParagraphRange.SetExtraStyleId(style_id: string);
function TSPdfParagraphRange.SetTblStyleIdAndType(style_id: string; type: string);
begin
extra_style_id_ := style_id;
table_style_id_ := style_id;
table_style_type_ := type;
end;
function TSPdfParagraphRange.NewLineRange(): TSPdfLineRange;
@ -631,7 +635,7 @@ begin
styles := docx_components_ware_.GetStyles();
new_ppr.Copy(styles.DocDefaults.PPrDefault.PPr);
new_ppr.RPr.Copy(styles.DocDefaults.RPrDefault.RPr);
{self.}SetPPrByStyleId(new_ppr, extra_style_id_);
{self.}SetPPrByStyleId(new_ppr, table_style_id_);
{self.}SetPPrByStyleId(new_ppr, ppr.PStyle.Val);
if ifObj(ppr) then
begin
@ -658,6 +662,7 @@ begin
new_rpr := new RPr();
styles := docx_components_ware_.GetStyles();
new_rpr.Copy(styles.DocDefaults.RPrDefault.RPr);
{self.}SetRPrByTblStyleId(new_rpr, table_style_id_);
{self.}SetRPrByStyleId(new_rpr, ppr.PStyle.Val);
{self.}SetRPrByStyleId(new_rpr, rpr.RStyle.Val);
if ifObj(rpr) then
@ -667,6 +672,23 @@ begin
end
end;
function TSPdfParagraphRange.SetRPrByTblStyleId(var rpr: RPr; style_id: string);
begin
styles := docx_components_ware_.GetStylesAdapter();
style := styles.GetStyleByStyleId(style_id);
if ifObj(style) then
begin
based_on := style.BasedOn.Val;
{self.}SetRPrByTblStyleId(rpr, based_on);
rpr.Copy(style.RPr);
end;
if table_style_type_ then
begin
tbl_style_pr := docx_components_ware_.GetTblStylePrByType(table_style_id_, table_style_type_);
if tbl_style_pr then rpr.Copy(tbl_style_pr.RPr);
end
end;
function TSPdfParagraphRange.SetRPrByStyleId(var rpr: RPr; style_id: string);
begin
styles := docx_components_ware_.GetStylesAdapter();

View File

@ -58,7 +58,7 @@ begin
for _,row in cell_range_array_ do
begin
flag := nil;
for _,range in row do
for __,range in row do
begin
if ifnil(flag) and ifObj(range) then flag := range.IfRemoveEmptyRectangle();
if not ifObj(range) or flag <> range.IfRemoveEmptyRectangle() then
@ -67,7 +67,7 @@ begin
break;
end
end
for _,range in row do
for __,range in row do
begin
if ifObj(range) then
begin
@ -102,6 +102,8 @@ begin
begin
{self.}SetTcPr(tc.TcPr);
if i = 0 then {self.}SetTblStylePr(tc.TcPr, "firstRow");
else if (i + 1) % 2 = 0 then {self.}SetTblStylePr(tc.TcPr, "band1Horz");
else {self.}SetTblStylePr(tc.TcPr, "band2Horz");
vmerge := tc.TcPr.XmlChildVMerge.Val ? tc.TcPr.XmlChildVMerge.Val : tc.TcPr.VMerge;
if vmerge = "restart" then
begin
@ -129,6 +131,8 @@ begin
cell_range.Width := grid_cols[pos].W;
cell_range.LowerBound := {self.}LowerBound;
cell_range.FixedHeight := tc_h;
cell_range.Row := i;
cell_range.Col := j;
cell_range_array_[i][pos] := cell_range;
grid_span := new GridSpanUnitDecorator(tc.TcPr.GridSpan);
pos++;

View File

@ -21,6 +21,7 @@ end;
function TSPdfTextRange.Do();
begin
// println("text = {}, endx = {}, endy = {}, width = {}, page = {}", ansiToUtf8(text), endx, endy, width, page);
// println("Text = {}, sz = {}, szcs = {}, rpr.I = {}", ansiToUtf8({self.}Text), {self.}RPr.Sz.Val, {self.}RPr.SzCs.Val, {self.}RPr.I);
[r, g, b] := array(0, 0, 0);
if {self.}RPr.Color.Val then [r, g, b] := TSColorToolKit.HexToRGB({self.}RPr.Color.Val);
{self.}TSPage.PdfPage.SetRGBFill(r / 255, g / 255, b / 255);

View File

@ -70,7 +70,7 @@ end;
function TSFontWare.GetBuiltInFont(name: string; bold: boolean; italic: boolean);
begin
font_name := substitution_rules_["name"];
font_name := substitution_rules_[name];
if ifnil(font_name) then font_name := "SimSun";
if bold and italic then
font_name += ",BoldItalic";