MigraDoc - only Last Page Footer

安稳与你 提交于 2019-12-25 03:26:35

问题


I have this code:

        Document document = new Document();

        Section sec = document.AddSection();
        Paragraph par;

        for (int i = 0; i < 50; i++)
        {
            par = sec.AddParagraph();
            par.AddText("Wiki je označení webů (nebo obecněji hypertextových dokumentů), které umožňují uživatelům přidávat obsah podobně jako v internetových diskusích, ale navíc jim také umožňují měnit stávající obsah; v přeneseném smyslu se jako wiki označuje software, který takovéto weby vytváří.Původně se termín wiki používal zcela opačně. Wiki bylo označení typu softwaru a weby postavené.");
        }

        par = sec.Headers.Primary.AddParagraph();
        par.AddText("hlavicka");
        Borders hranice = new Borders();
        hranice.Width = 1;
        sec.Headers.Primary.Format.Borders = hranice;

        sec.AddImage("images.jpg");

        par = sec.AddParagraph();
        par.AddText("paticka");

        PdfDocumentRenderer print = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);
        print.Document = document;
        print.RenderDocument();
        print.PdfDocument.Save("ahoj.pdf");

I need to make a Footer only on the last page. Is it possible?


回答1:


There is no way to create a real footer on the last page.

You can create a TextFrame with your last paragraph. TextFrames are Shapes and can be placed at an absolute position on the page, so you can also place them where the Footer would be.




回答2:


For example:
You need a 30mm place over the footer on the last page. (That could be a signature field.)

  1. Define the PageSetup without this place (only footer hight + your distance from the bottom of the page)
  2. Define your 30mm paragraph or table as TextFrame on a fix position: TextFrame.RelativeVertical = RelativeVertical.Page; frame.Top = ShapePosition.Bottom;
  3. Define an empty paragraph as the last paragraph of the document with paragraph.Format.SpaceBefore = 30mm

Now the 30mm empty paragraph is on the end of all Documents and the signature overlapped it or the place for signature is not enough. Then the 30mm empty place forces a page break.




回答3:


MemoryStream PdfDocumnet()
{
    Document pdfdoc = new MigraDoc.DocumentObjectModel.Document();
    pdfdoc.Info.Title = "Doc Title";
    pdfdoc.Info.Subject = "Doc Subject";

    DefineProformaStyles(pdfdoc);

    CreatePdf(pdfdoc);

    MemoryStream stream = new MemoryStream();
    PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
    renderer.Document = pdfdoc;
    renderer.RenderDocument();
    renderer.Save(stream, false);

    return stream;
}


private void CreatePdf(Document pdfDoc)
{
    pdfDoc.DefaultPageSetup.HeaderDistance = "0.8cm";
    pdfDoc.DefaultPageSetup.FooterDistance = "0.6cm"; 

    MigraDoc.DocumentObjectModel.Section section = pdfDoc.AddSection();
    section.PageSetup.TopMargin = "6.8cm"; //Unit.FromCentimeter(3.0);
    section.PageSetup.BottomMargin = "3.4cm"; //Unit.FromCentimeter(3.0);


    // HEADER  ///////////////////////////////////////////////////////////////////////////////////
    MigraDoc.DocumentObjectModel.Tables.Table tableh1 = section.Headers.Primary.AddTable();
    Column colh1 = tableh1.AddColumn("10.8cm");
    Column colh2 = tableh1.AddColumn("8cm");
    colh2.Format.Alignment = ParagraphAlignment.Right; 

    // ...
    // ... add content to header
    // ...


    //FOOTER for every page //////////////////////////////////////////////////////////////////////
    MigraDoc.DocumentObjectModel.Tables.Table tablef2 = section.Footers.Primary.AddTable();
    tablef2.Style = "Table";

    //add content to footer



    // BODY    ///////////////////////////////////////////////////////////////////////////////////
    MigraDoc.DocumentObjectModel.Tables.Table tableC = section.AddTable();

    TextFrame frm0 = section.AddTextFrame();

    Paragraph prg1 = section.AddParagraph();

    //....

    //.....



    //FOOTER FOR ONLY LAST PAGE //////////////////////////////////////////////////////////////////
    //Add an empty paragraph. If there is not enough space to fit in current page then ... page break
    Paragraph emptyPar = section.AddParagraph();
    emptyPar.Format.SpaceBefore = "2.6cm";

    //add the special footer
    string lastPageFooterImgFile = HttpContext.Current.Server.MapPath("/company/CompanyFooter.png");
    TextFrame lastframe = section.AddTextFrame();
    lastframe.Height = "2.6cm";
    lastframe.RelativeVertical = RelativeVertical.Page;
    lastframe.Top = "24cm"; // 24cm + 2.6cm + footer_for_every_page.Height = Page.Height
    lastframe.AddImage(lastPageFooterImgFile);
}


private void DefineProformaStyles(Document Doc)
{
    Doc.DefaultPageSetup.LeftMargin = "1.3cm";
    MigraDoc.DocumentObjectModel.Style style = Doc.Styles["Normal"];

    style = Doc.Styles[StyleNames.Header];
    style.ParagraphFormat.AddTabStop("1cm", TabAlignment.Right);

    style = Doc.Styles[StyleNames.Footer];
    style.ParagraphFormat.AddTabStop("1cm", TabAlignment.Center);

    style = Doc.Styles.AddStyle("Table", "Normal");
    style.Font.Name = "Times New Roman";
    style.Font.Size = 9;

    style = Doc.Styles.AddStyle("Reference", "Normal");
    style.ParagraphFormat.SpaceBefore = "1mm";
    style.ParagraphFormat.SpaceAfter = "1mm";
    style.ParagraphFormat.TabStops.AddTabStop("1cm", TabAlignment.Right);
}


来源:https://stackoverflow.com/questions/25244593/migradoc-only-last-page-footer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!