feat(pdf): Add page numbering to generated PDF documents
Some checks failed
continuous-integration/drone/push Build is failing

- Add page counter display at bottom center of each PDF page
- Track total page count and current page index during PDF generation
- Use Arial font (10pt) with gray color (#999999) for page numbers
- Format page numbers as "current / total" (e.g., "1 / 5")
- Position page numbers 20pt from bottom of page
- Refactor loop from foreach to indexed for loop to access page index
- Improve PDF document readability with page navigation information
This commit is contained in:
zpc 2026-03-30 18:00:30 +08:00
parent 8ae6dcfa88
commit 04b9fa8220

View File

@ -409,17 +409,27 @@ public class PdfGenerationService : IPdfGenerationService
private static void BuildAndSavePdf(List<byte[]> images, string filePath)
{
using var document = new PdfDocument();
var totalPages = images.Count;
var font = new XFont("Arial", 10, XFontStyleEx.Regular);
var brush = new XSolidBrush(XColor.FromArgb(153, 153, 153)); // #999999
foreach (var imageBytes in images)
for (var i = 0; i < totalPages; i++)
{
var imageBytes = images[i];
var page = document.AddPage();
page.Width = XUnit.FromPoint(PageWidthPt);
page.Height = XUnit.FromPoint(PageHeightPt);
using var stream = new MemoryStream(imageBytes);
using var xImage = XImage.FromStream(() => new MemoryStream(imageBytes));
using var gfx = XGraphics.FromPdfPage(page);
gfx.DrawImage(xImage, 0, 0, page.Width, page.Height);
// 绘制页码(底部居中)
var pageNumber = $"{i + 1} / {totalPages}";
var size = gfx.MeasureString(pageNumber, font);
var x = (page.Width - size.Width) / 2;
var y = page.Height - 20; // 距底部 20pt
gfx.DrawString(pageNumber, font, brush, x, y);
}
document.Save(filePath);