gpt4 book ai didi

c# - 在 Migradoc 中添加最后 3 行之前检查是否接近页尾

转载 作者:行者123 更新时间:2023-11-30 15:32:37 25 4
gpt4 key购买 nike

我是第一次使用 MigraDoc 库,需要帮助。我正在使用 MigraDoc 打印 pdf 表格。在我的表中,3 行组成一个组。所以每 3 行组必须在同一页上,但在我当前的代码中,这不会发生。

它将在同一页上打印第 1 行或第 2 行,如果页面结束,则第 3 行进入下一页。

public void CreateDocument()
{
this.document = new Document { Info = { Title = "Time Sheet Report By Job", } };
this.DefineStyles();
this.CreatePage();
this.FillContent();
}

public void FillContent()
{
int x = 1;
foreach (XElement r in this.xReport.Element(ElrepTable).Elements(ElrepRow))
{
Row row1 = this.table.AddRow();
int i = 0;
row1.Cells[0].Borders.Visible = false;
foreach (XElement c in r.Elements(ElrepCell))
{
row1.Cells[i++].AddParagraph(c.Value);
}

if (x++ % 3 != 0)
{
continue;
}

row1.Borders.Bottom.Width = 1;
row1.Cells[0].Borders.Bottom.Visible = true;
row1.Cells[0].Borders.Bottom.Width = 1;
}

int colCount = this.GetColCount();
int rowCount = this.GetRowCount();

this.table.SetEdge(1, 1, colCount - 1, rowCount - 1, Edge.Box, BorderStyle.Single, 0.05);
}


private void CreatePage()
{
Section section = this.document.AddSection();
section.PageSetup.LeftMargin = "1cm";
section.PageSetup.RightMargin = "1cm";
section.PageSetup.BottomMargin = "2cm";
section.PageSetup.TopMargin = "4.5cm";

// Put a logo in the header
string imgPath = this.SaveImg(Resource1.MMS_Logo, "logo");
Image image = section.Headers.Primary.AddImage(imgPath);
image.Height = "2cm";
image.LockAspectRatio = true;
image.RelativeVertical = RelativeVertical.Line;
image.RelativeHorizontal = RelativeHorizontal.Margin;
image.Top = ShapePosition.Top;
image.Left = ShapePosition.Center;
image.WrapFormat.Style = WrapStyle.Through;

imgPath = this.SaveImg(Resource1.shadow, "shadow");
Image image2 = section.Headers.Primary.AddImage(imgPath); // .Headers.Primary
image2.Height = "1.5cm";
image2.Width = "5cm";
image2.RelativeVertical = RelativeVertical.Line;
image2.RelativeHorizontal = RelativeHorizontal.Margin;
image2.Top = ShapePosition.Top;
image2.Left = ShapePosition.Right;
image2.WrapFormat.Style = WrapStyle.Through;

this.leftTextFrame = section.Headers.Primary.AddTextFrame();
this.leftTextFrame.Height = "10.0cm";
this.leftTextFrame.Width = "7.0cm";
this.leftTextFrame.RelativeHorizontal = RelativeHorizontal.Margin;
this.leftTextFrame.RelativeVertical = RelativeVertical.Page;
this.leftTextFrame.Left = ShapePosition.Left;
this.leftTextFrame.Top = "1.75cm";

this.centerTextFrame = section.Headers.Primary.AddTextFrame();
this.centerTextFrame.Height = "5.0cm";
this.centerTextFrame.Width = "6.0cm";
this.centerTextFrame.RelativeHorizontal = RelativeHorizontal.Margin;
this.centerTextFrame.RelativeVertical = RelativeVertical.Page;
this.centerTextFrame.Left = ShapePosition.Center;
this.centerTextFrame.Top = "3.5cm";

this.rightTextFrame = section.Headers.Primary.AddTextFrame();
this.rightTextFrame.Height = "3.0cm";
this.rightTextFrame.Width = "5.0cm";
this.rightTextFrame.RelativeHorizontal = RelativeHorizontal.Margin;
this.rightTextFrame.RelativeVertical = RelativeVertical.Page;
this.rightTextFrame.Left = ShapePosition.Right;
this.rightTextFrame.Top = "1.75cm";

Dictionary<string, string> jobdet = this.GetJobDetails();

// Put sender in address frame
Paragraph p1 = this.leftTextFrame.AddParagraph();
var f = new Font { Bold = true, Size = 8 };
p1.AddFormattedText(this.GetTitle(), f);
p1.AddLineBreak();
p1.AddLineBreak();
p1.AddFormattedText("Account Information", new Font { Bold = true, Italic = true, Size = 7 });
p1.AddLineBreak();

Paragraph p2 = this.leftTextFrame.AddParagraph();
p2.Format.LeftIndent = "2cm";

p2.AddFormattedText("Job #:", new Font { Bold = true, Size = 6 });
p2.AddTab();
p1.AddTab();
p2.AddFormattedText(this.GetJobNo(), new Font { Size = 6 });
p2.AddLineBreak();

p2.AddFormattedText("Job Name:", new Font { Bold = true, Size = 6 });
p2.AddTab();
p2.AddFormattedText(jobdet["Name"], new Font { Size = 6 });
p2.AddLineBreak();

p2.AddFormattedText("Address:", new Font { Bold = true, Size = 6 });
p2.AddTab();
p2.AddFormattedText(jobdet["Address"], new Font { Size = 6 });
p2.AddLineBreak();

Paragraph cp1 = this.centerTextFrame.AddParagraph();
cp1.AddFormattedText("Supervisor:", new Font { Bold = true, Size = 6 });
cp1.AddTab();
cp1.AddFormattedText(jobdet["Supervisor"], new Font { Size = 6 });

cp1.AddLineBreak();
cp1.AddLineBreak();

cp1.AddFormattedText("Location:", new Font { Bold = true, Size = 6 });
cp1.AddTab();
cp1.AddTab();
cp1.AddFormattedText(jobdet["Location"], new Font { Size = 6 });

Paragraph rp1 = this.rightTextFrame.AddParagraph();
rp1.Format.Alignment = ParagraphAlignment.Center;
rp1.AddFormattedText("Timesheet by Job", new Font { Bold = true, Size = 9 });
rp1.AddLineBreak();
rp1.AddFormattedText("Pay Period End " + this.GetEdate(), new Font { Bold = true, Size = 8 });
rp1.AddLineBreak();
rp1.AddLineBreak();

rp1.AddFormattedText(this.GetOnDate(), new Font { Size = 6 });
rp1.AddTab();
FormattedText ft = rp1.AddFormattedText("Page ", new Font { Bold = true, Size = 7 });
ft.AddPageField();
ft.Font.Size = 7;
ft.Font.Bold = true;
ft = rp1.AddFormattedText(" of ", new Font { Bold = true, Size = 7 });
ft.AddNumPagesField();
ft.Font.Size = 7;
ft.Font.Bold = true;

section.AddParagraph();

cp1.AddLineBreak();
cp1.AddLineBreak();
this.table = section.AddTable();

this.table.Style = "Table";
this.table.Borders.Color = Color.Parse("Black");
this.table.Rows.LeftIndent = 0;

int cols = this.GetColCount();
Column column = this.table.AddColumn("4cm");
column.Format.Alignment = ParagraphAlignment.Left;

column = this.table.AddColumn("0.6cm");
column.Format.Alignment = ParagraphAlignment.Left;

for (int i = 2; i < cols; i++)
{
column = this.table.AddColumn("0.85cm");
column.Format.Alignment = ParagraphAlignment.Left;
}

// Create the header of the table
Row row = this.table.AddRow();
row.HeadingFormat = true;
row.Format.Alignment = ParagraphAlignment.Center;
row.Format.Font.Bold = true;
row.Cells[0].Borders.Top.Visible = false;
row.Cells[0].Borders.Left.Visible = false;
row.Cells[0].Borders.Right.Visible = false;
row.Cells[1].Borders.Top.Visible = false;
row.Cells[1].Borders.Left.Visible = false;
string[] colNames = this.GetColNames();
for (int i = 0; i < cols; i++)
{
row.Cells[i].AddParagraph(colNames[i]);
row.Cells[i].Format.Font.Bold = false;
row.Cells[i].Format.Alignment = ParagraphAlignment.Left;
row.Cells[i].VerticalAlignment = VerticalAlignment.Center;
row.Cells[i].Borders.Width = 1.5;
}
}

我不知道 MigraDoc 是否有某些功能可以直接执行此类操作。否则我想知道页面是否会在 3 行之间中断,如果是,我将不得不自己进行分页。

任何伪代码片段都会很有帮助。

最佳答案

对于每组的第一行,设置 row1.KeepWith = 2 以将三行放在一个组中。

关于c# - 在 Migradoc 中添加最后 3 行之前检查是否接近页尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18642284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com