首页 > 解决方案 > MigraDoc 文本溢出到下一页页眉

问题描述

我正在使用 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++;
            }
}

我没有分享这里调用的一些私有方法的代码。FillDetails(business) 是一个可以有一行或多行的表。这个表格信息和它下面的内容被推送到具有标题的下一页并且它重叠在上面。

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

标签: c#pdfsharpmigradoc

解决方案


使用 MigraDoc 时,您必须设置一个足够大的顶部边框以容纳标题。如果标头大于保留区域,则标头和正文可能会重叠,而 MigraDoc 无法阻止这种情况。

仅在第一页上有标题是另一种选择。


推荐阅读