C# 将文档文本溢出到下一页页眉中

C# 将文档文本溢出到下一页页眉中,c#,pdfsharp,migradoc,C#,Pdfsharp,Migradoc,我正在使用MigraDoc以PDF格式创建信件,以便批量打印。这意味着我有多个企业需要得到上市批准通知。我已经创建了一个包含一般信息的模板,并在foreach循环中为每个业务填写信函的详细信息,如下所示: public TemplateClass(string title, string subject, string author, string memo_field) { try { //-----------------

我正在使用MigraDoc以PDF格式创建信件,以便批量打印。这意味着我有多个企业需要得到上市批准通知。我已经创建了一个包含一般信息的模板,并在foreach循环中为每个业务填写信函的详细信息,如下所示:

   public TemplateClass(string title, string subject, string author, string memo_field)
    {
        try
        {
            //---------------------------------------------------
            // Set up the document and section
            //--------------------------------------------------
            _document = new Document();
            _section = new Section();

            //--------------------------------------------------------------
            // Define the margins, style
            //--------------------------------------------------------------
            this.DefineBorder();
            this.DefineStyle(_document);
            this.FillHeaderAndFooter(memo_field);              
        }
        catch { }
    }

private void FillHeaderAndFooter(string memo_field)
    {
        //------------------------------------------------
        // Add the main section of the document
        //-----------------------------------------------------
        this._section = this._document.AddSection();
        {
            this._section.PageSetup.Orientation = Orientation.Portrait;
            this._section.PageSetup.PageFormat = PageFormat.Letter;
            this._section.PageSetup.OddAndEvenPagesHeaderFooter = true;

            //-----------------------------------------------
            // Add the header
            //-----------------------------------------------
            Image primaryImage = this._section.Headers.Primary.AddImage(Image.png");
            primaryImage.Height = "4.0cm";
            primaryImage.LockAspectRatio = true;
            primaryImage.RelativeVertical = RelativeVertical.Line;
            primaryImage.RelativeHorizontal = RelativeHorizontal.Margin;
            primaryImage.Top = ShapePosition.Top;
            primaryImage.Left = ShapePosition.Center;
            primaryImage.WrapFormat.Style = WrapStyle.Through;

            //------------------------------------------------
            // Add the header to the section
            //------------------------------------------------
            this._section.Headers.Primary.Add(this.Header());
            this._section.Headers.EvenPage.Add(this.Header());

            this._section.Footers.Primary.Add(this.Footer());
            this._section.Footers.EvenPage.Add(this.Footer());
        }
    }

public void GenerateLetters()
{
    template = new TemplateClass("", "", "", "Business Letter");
        {
            int i = 0;
            base.DocumentName = "Business Letter";
            var businesses = new List<Business>() { new Business() { ID=333, Name="XYZ", Address = "123 Main St" }, new Business() { ID=666, Name="PQR", Address="123 Main St"}}

            foreach (var business in businesses)
            {                    
                template.ReportSection.Add(AddBusinessInfo(business));
                template.ReportSection.Add(FillTopSection());
                template.ReportSection.Add(FillMessage());                   

                template.ReportSection.Add(FillDetails(business));
                template.ReportSection.Add(FillBottomSection());
                template.ReportSection.Add(FillSignature());

                if (i != businesses.Count)
                    template.ReportSection.AddPageBreak();

                i++;
            }
}
public TemplateClass(字符串标题、字符串主题、字符串作者、字符串备注字段)
{
尝试
{
//---------------------------------------------------
//设置文档和节
//--------------------------------------------------
_文件=新文件();
_节=新节();
//--------------------------------------------------------------
//定义页边距、样式
//--------------------------------------------------------------
这个.DefineBorder();
本.定义样式(_文件);
此.FillHeaderAndFooter(备注字段);
}
捕获{}
}
专用空白填充标头和文件夹(字符串备注\u字段)
{
//------------------------------------------------
//添加文档的主要部分
//-----------------------------------------------------
this._section=this._document.AddSection();
{
此._section.PageSetup.Orientation=方向.纵向;
这._section.PageSetup.PageFormat=PageFormat.Letter;
此._section.PageSetup.OddAndEvenPagesHeaderFooter=true;
//-----------------------------------------------
//添加标题
//-----------------------------------------------
Image primaryImage=this.\u section.Headers.Primary.AddImage(Image.png”);
主图像高度=“4.0cm”;
primaryImage.LockAspectRatio=true;
primaryImage.RelativeVertical=RelativeVertical.Line;
primaryImage.RelativeHorizontal=相对水平边缘;
primaryImage.Top=ShapePosition.Top;
primaryImage.Left=形状位置.Center;
primaryImage.WrapFormat.Style=WrapStyle.Through;
//------------------------------------------------
//将标题添加到节中
//------------------------------------------------
this.u节.Headers.Primary.Add(this.Header());
this.u section.Headers.EvenPage.Add(this.Header());
this.u section.Footers.Primary.Add(this.Footer());
this._section.Footers.EvenPage.Add(this.Footer());
}
}
公共无效生成器()
{
模板=新模板类(“商业信函”);
{
int i=0;
base.DocumentName=“商业信函”;
var Business=new List(){new Business(){ID=333,Name=“XYZ”,Address=“123 Main St”},new Business(){ID=666,Name=“PQR”,Address=“123 Main St”}
foreach(业务中的var业务)
{                    
template.ReportSection.Add(AddBusinessInfo(business));
template.ReportSection.Add(FillTopSection());
template.ReportSection.Add(FillMessage());
template.ReportSection.Add(FillDetails(business));
template.ReportSection.Add(FillBottomSection());
template.ReportSection.Add(FillSignature());
如果(i!=企业数)
template.ReportSection.AddPageBreak();
i++;
}
}
我不分享这里调用的一些私有方法的代码。 FillDetails(business)是一个可以有一行或多行的表。此表信息及其下面的内容被推送到下一个有标题的页面上,并在其上重叠


我可以从第二页开始跳过每项业务的页眉和页脚信息,也可以避免与页眉重叠,并以某种方式将正文保持为静态大小。

使用MigraDoc时,必须设置一个足够大的上边框来容纳页眉。如果页眉大于保留区域,则页眉身体可能会重叠,而MigraDoc无法阻止这一点


仅在第一页上设置页眉是另一种选择。

我希望每个企业的所有页面上都有页眉,但在第一页上都有页脚。如何实现这一点?指定您需要不同的第一页页眉/页脚(DifferentitFirstPageHeaderFooter)。为第一页设置页眉和页脚,仅为标准页面设置页脚(主页眉/页脚)。正如您在我的代码中看到的,我在模板类中定义的FillHeaderAndFooter()中添加了一个节。我是否需要将此代码移动到另一个类,并在GenerateLetters()中设置DifferentFirstPageHeaderFooter属性?我只看到代码片段,无法完全分析它们。您可能必须为每个新的逻辑文档创建一个新的部分,即使您将多个逻辑文档合并到一个PDF中。这样,每个逻辑文档都将从其自己的第一页开始。我尝试了您的建议。它现在只将标题放在第一页上。我想要每家企业的st页。根据内容,每家企业可以有多个页面。我有多家企业,信件是循环生成的。我需要在每家企业都有页眉。