zoukankan      html  css  js  c++  java
  • c# novacode-docx – Novacode DocX同一文档中的不同页面方向(转)

    转自:http://www.voidcn.com/article/p-mlrdwdni-bwm.html

    使用以下代码,我正在尝试创建一个文档,其中第2页和第3页是横向,而其他是纵向.全部应为8.5“x 11”.

    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    {
       using (DocX document = DocX.Create(ms))
       {
          document.PageLayout.Orientation = Novacode.Orientation.Portrait;
          document.PageWidth = 816F;
          document.PageHeight = 1056F;
    
          document.MarginTop = 50F;
          document.MarginRight = 50F;
          document.MarginBottom = 75F;
          document.MarginLeft = 50F;
    
          document.AddHeaders();
          document.AddFooters();
          document.DifferentFirstPage = true;
          document.DifferentOddAndEvenPages = false;
    
          Header header_first = document.Headers.first;
          Header header_main = document.Headers.odd;
          Footer footer_main = document.Footers.odd;
    
          Novacode.Table tHeaderFirst = header_first.InsertTable(2, 1);
          tHeaderFirst.Design = TableDesign.None;
          tHeaderFirst.AutoFit = AutoFit.Window;
    
          Paragraph pHeaderFirst = header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0];
          Novacode.Image imgHeaderFirst = document.AddImage(ctx.Server.MapPath("~/proposal-assets/header-front.jpg"));
          pHeaderFirst.InsertPicture(imgHeaderFirst.CreatePicture());
    
          Novacode.Table tHeaderMain = header_main.InsertTable(2, 1);
          tHeaderMain.Design = TableDesign.None;
          tHeaderMain.AutoFit = AutoFit.Window;
    
          Paragraph pHeader = header_main.Tables[0].Rows[0].Cells[0].Paragraphs[0];
          Novacode.Image imgHeader = document.AddImage(ctx.Server.MapPath("~/proposal-assets/header-internal-portrait.jpg"));
          pHeader.InsertPicture(imgHeader.CreatePicture());
    
          Paragraph pFooter = footer_main.Paragraphs.First();
          pFooter.Alignment = Alignment.center;
          pFooter.Append("Page ");
          pFooter.AppendPageNumber(PageNumberFormat.normal);
          pFooter.Append("/");
          pFooter.AppendPageCount(PageNumberFormat.normal);
    
          Paragraph p1 = document.InsertParagraph("test");
          p1.InsertPageBreakAfterSelf();
          document.InsertSection(true);
          document.PageLayout.Orientation = Novacode.Orientation.Landscape;
          //document.PageWidth = 1056F;
          //document.PageHeight = 816F;
    
          Paragraph p2 = document.InsertParagraph("test");
          p2.InsertPageBreakAfterSelf();
    
          Paragraph p3 = document.InsertParagraph("test");
          p3.InsertPageBreakAfterSelf();
    
          document.InsertSection(true);
          document.PageLayout.Orientation = Novacode.Orientation.Portrait;
          //document.PageWidth = 816F;
          //document.PageHeight = 1056F;
    
          Paragraph p4 = document.InsertParagraph("test");
          p4.InsertPageBreakAfterSelf();
    
          Paragraph p5 = document.InsertParagraph("test");
          p5.InsertPageBreakAfterSelf();
    
          Paragraph p6 = document.InsertParagraph("test");
          p6.InsertPageBreakAfterSelf();
    
          document.Save();
       }
    }

    我有几个问题.

    首先,如果我在开头设置方向一次,所有页面的大小都是正确的,但是一旦我将第2和第3个更改添加到PageLayout.Orientation,突然我的所有页面都是错误的大小.

    其次,插入这些部分会对我的页眉和页脚产生奇怪的影响.第3部分的第一页就像它是文档的第一页,并采用第一页页眉和页脚.

    最后,将第2和第3个更改添加到PageLayout.Orientation实际上并不会更改页面方向.正如您在注释掉的代码中看到的那样,我也尝试在更改布局后设置新的页面高度和宽度.这样做会使我的页面恢复到正确的大小,但绝不会影响方向.

    我错过了什么?任何帮助将不胜感激.

    最后!我已经找到了一个可用的解决方案,我将放在这里,希望它能帮助别人.

    document.PageLayout.Orientation = Novacode.Orientation.Portrait;
    document.PageWidth = 816F;
    document.PageHeight = 1056F;
    document.MarginTop = 50F;
    document.MarginRight = 50F;
    document.MarginBottom = 75F;
    document.MarginLeft = 50F;                     
    
    document.AddHeaders();
    document.AddFooters();
    document.DifferentFirstPage = true;
    document.DifferentOddAndEvenPages = false;
    
    Header header_first = document.Headers.first;
    Header header_main = document.Headers.odd;
    Footer footer_main = document.Footers.odd;
    
    Paragraph pHeaderFirst = header_first.Paragraphs.First();
    Novacode.Image imgHeaderFirst = document.AddImage(ctx.Server.MapPath("~/proposal-assets/header-front.jpg"));
    pHeaderFirst.Alignment = Alignment.center;
    pHeaderFirst.SpacingAfter(25);
    pHeaderFirst.AppendPicture(imgHeaderFirst.CreatePicture());
    
    Paragraph pHeader = header_main.Paragraphs.First();
    Novacode.Image imgHeader = document.AddImage(ctx.Server.MapPath("~/proposal-assets/header-internal-portrait.jpg"));
    pHeader.Alignment = Alignment.center;
    pHeader.SpacingAfter(25);
    pHeader.AppendPicture(imgHeader.CreatePicture());
    
    Paragraph pFooter = footer_main.Paragraphs.First();
    pFooter.Alignment = Alignment.center;
    pFooter.Append("Page ");
    pFooter.AppendPageNumber(PageNumberFormat.normal);
    pFooter.Append("/");
    pFooter.AppendPageCount(PageNumberFormat.normal);
    
    Paragraph p1 = document.InsertParagraph("test");
    
    System.IO.MemoryStream ms2 = new System.IO.MemoryStream();
    DocX document2 = DocX.Create(ms2);
    document2.PageLayout.Orientation = Novacode.Orientation.Landscape;
    document2.PageWidth = 1056F;
    document2.PageHeight = 816F;
    document2.MarginTop = 50F;
    document2.MarginRight = 50F;
    document2.MarginBottom = 75F;
    document2.MarginLeft = 50F; 
    Paragraph p2 = document2.InsertParagraph("test --- doc 2");
    p2.InsertPageBreakAfterSelf();
    Paragraph p3 = document2.InsertParagraph("test --- doc 2");
    document2.Save();
    
    document.InsertSection();
    document.InsertDocument(document2);
    
    
    System.IO.MemoryStream ms3 = new System.IO.MemoryStream();
    DocX document3 = DocX.Create(ms3);
    document3.PageLayout.Orientation = Novacode.Orientation.Portrait;
    document3.PageWidth = 816F;
    document3.PageHeight = 1056F;
    document3.MarginTop = 50F;
    document3.MarginRight = 50F;
    document3.MarginBottom = 75F;
    document3.MarginLeft = 50F; 
    Paragraph p4 = document3.InsertParagraph("test");
    p4.InsertPageBreakAfterSelf();
    Paragraph p5 = document3.InsertParagraph("test");
    p5.InsertPageBreakAfterSelf();
    Paragraph p6 = document3.InsertParagraph("test");
    document3.Save();
    
    document.InsertSection();
    document.InsertDocument(document3);
    
    document.Save();

    将不同的部分创建为单独的文档并将其插入主文档中运行良好并解决了我的所有问题.

  • 相关阅读:
    分享一个本人打造的公众号吸粉、推广方案。
    iOS开发简记(8):数据持久化
    iOS开发简记(7):网络请求模块
    iOS开发简记(6):storyboard的使用
    iOS开发简记(5):设备唯一标识与全局变量
    iOS开发简记(4):录音AVAudioRecorder
    iOS开发简记(3):tips提示
    iOS开发简记(2):自定义tabbar
    spring+mybatis+springMVC框架配置多数据源
    Docker常用操作
  • 原文地址:https://www.cnblogs.com/qijunzifeng/p/14523618.html
Copyright © 2011-2022 走看看