From d687f45b3b4b4d1a86f7ab4a8fb4a59c7975bbd5 Mon Sep 17 00:00:00 2001 From: csh Date: Tue, 6 Aug 2024 16:19:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=9B=AE=E5=BD=95=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E8=B7=B3=E8=BD=AC=E4=BD=8D=E7=BD=AE=E4=B8=8D=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TSDocxToPdf.tsf | 13 +++---- range/Advanced/TSPdfParagraphRange.tsf | 53 +++++++++++++++----------- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/TSDocxToPdf.tsf b/TSDocxToPdf.tsf index 4b184a2..1cbfc5f 100644 --- a/TSDocxToPdf.tsf +++ b/TSDocxToPdf.tsf @@ -12,7 +12,7 @@ public function AddPage(): TSPage;overload; function AddPage(sect_ware: TSSectWare): TSPage;overload; function AdjustPageNumber(page: TSPage; num: integer); - function LinkToToc(anchor: string; page: TSPage); + function LinkToToc(anchor: string; page: TSPage; left: real; top: real); function AddToc(anchor: string; toc: TSToc); function SetHeaderAndFooter(); function ProcessNumpages(); @@ -100,7 +100,6 @@ begin elements := sect_ware.Elements; for _,element in elements do begin - // if _ = 150 then return; if element.LocalName = "p" then {self.}TransformP(sect_ware, element); else if element.LocalName = "tbl" then {self.}TransformTbl(sect_ware, element); else if element.LocalName = "sdt" then {self.}TransformSdt(sect_ware, element); @@ -227,7 +226,6 @@ begin elements := header.Elements(); for _,element in elements do begin - // if _ = 150 then return; if element.LocalName = "p" then {self.}TransformHeaderAndFooter(element, "header"); end end @@ -242,7 +240,6 @@ begin elements := header.Elements(); for _,element in elements do begin - // if _ = 150 then return; if element.LocalName = "p" then {self.}TransformHeaderAndFooter(element, "footer"); end end @@ -389,20 +386,22 @@ begin else toc_array_[anchor] := array(toc); if toc_unmacthed_array_[anchor] then begin - {self.}LinkToToc(anchor, toc_unmacthed_array_[anchor]); + toc := toc_unmacthed_array_[anchor]; + {self.}LinkToToc(anchor, toc[0], toc[1], toc[2]); toc_unmacthed_array_[anchor] := nil; end end; -function TSDocxToPdf.LinkToToc(anchor: string; page: TSPage); +function TSDocxToPdf.LinkToToc(anchor: string; page: TSPage; left: real; top: real); begin arr := toc_array_[anchor]; if ifnil(arr) then begin - toc_unmacthed_array_[anchor] := page; + toc_unmacthed_array_[anchor] := array(page, left, top); return; end dst := page.PdfPage.CreateDestination(); + dst.SetXYZ(left, top, 1); for _,toc in arr do toc.LinkAnnot(dst); toc.AddPageNumber(page); diff --git a/range/Advanced/TSPdfParagraphRange.tsf b/range/Advanced/TSPdfParagraphRange.tsf index ccf30bc..3e5a3a8 100644 --- a/range/Advanced/TSPdfParagraphRange.tsf +++ b/range/Advanced/TSPdfParagraphRange.tsf @@ -118,7 +118,7 @@ end; function TSPdfParagraphRange.BookMarkLinkToc(); begin for name,arr in bookmark_array_ do - if arr[0] then docx_to_pdf_.LinkToToc(name, arr[0].TSPage); + if arr[0] then docx_to_pdf_.LinkToToc(name, arr[0].TSPage, arr[0].EndX, arr[0].EndY + arr[0].RPr.Sz.Val); end; function TSPdfParagraphRange.HyperlinkToToc(ppr: PPrUnitDecorator); @@ -128,57 +128,66 @@ begin begin pg := arr[0].TSPage; left := {self.}StartX; + right := {self.}StartX + {self.}Width; + top := {self.}StartY; bottom := {self.}StartY; - right := left + {self.}Width; - top := bottom; - x := arr[0].EndX + arr[0].Width; + x := arr[0].EndX; y := arr[0].EndY; + if top - {self.}GetParagraphLineSpace(arr[0].RPr.Sz.Val, ppr.Spacing.Line) then + begin + top := docx_to_pdf_.GetCurrentPoint().Y; + bottom := top; + end for _,range in arr do begin if x + range.Width - {self.}StartX > {self.}Width + 1e-6 then // 换行 begin - top += max_size; + line_space := {self.}GetParagraphLineSpace(max_size, ppr.Spacing.Line); + bottom -= line_space; max_size := 0; + if range.TSPage <> pg then + begin + rect := array(left, bottom, right, top); + toc := new TSToc(ppr, rect, pg, x, y); + docx_to_pdf_.AddToc(anchor, toc); + pg := range.TSPage; + bottom := {self.}StartY; + end end if range.RPr.Sz.Val > max_size then max_size := range.RPr.Sz.Val; - if range.TSPage <> pg then - begin - rect := array(left, bottom, right, top + max_size); - toc := new TSToc(ppr, rect, pg, x, y); - docx_to_pdf_.AddToc(anchor, toc); - top := bottom; - pg := range.TSPage; - continue; - end x := range.EndX + range.Width; y := range.EndY; end font_name := ppr.RPr.RFonts.EastAsia ? ppr.RPr.RFonts.EastAsia : ppr.RPr.RFonts.Ascii; font_obj := docx_to_pdf_.Font.GetFont(font_name, ppr.RPr.B, ppr.RPr.I); - rect := array(left, bottom, right, top + max_size); - toc := new TSToc(ppr, rect, pg, x, y, font_obj); - docx_to_pdf_.AddToc(anchor, toc); - + line_space := {self.}GetParagraphLineSpace(max_size, ppr.Spacing.Line); pg.PdfPage.SetFontAndSize(font_obj, max_size); + bottom -= line_space; num_width := pg.PdfPage.TextWidth("." + tostring(pg.Number)); if x + num_width - {self.}StartX > {self.}Width + 1e-6 then // 换行 begin offset := (line_space - max_size) / 2 + max_size - max_size / 5; - line_space := {self.}GetParagraphLineSpace(max_size, ppr.Spacing.Line); diff := {self.}EndY - sect_ware_.SectPr.PgMar.Bottom; if {self.}CheckAndAddPage({self.}EndY, line_space) then begin {self.}DynamicHeight += diff; pg := page_; + rect := array(left, bottom, right, top); + toc := new TSToc(ppr, rect, pg, x, y); + docx_to_pdf_.AddToc(anchor, toc); + bottom := {self.}StartY; end else begin {self.}EndY -= line_space; {self.}DynamicHeight += line_space; + bottom -= line_space; + x := {self.}StartX; + y := {self.}EndY + offset; end - rect := array({self.}StartX, {self.}StartY - line_space, {self.}StartX + {self.}Width, {self.}StartY); - toc := new TSToc(ppr, rect, pg, {self.}StartX, {self.}EndY + offset, font_obj); - docx_to_pdf_.AddToc(anchor, toc); end + rect := array(left, bottom, right, top); + toc := new TSToc(ppr, rect, pg, x, y, font_obj); + docx_to_pdf_.AddToc(anchor, toc); end end;