Novacode DocX Различная ориентация страниц в одном документе

Используя следующий код, я пытаюсь создать документ, в котором страницы 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 фактически не меняет ориентацию страницы. Как вы можете видеть в закомментированном коде, я также попытался установить новую высоту и ширину страницы после изменения макета. В результате мои страницы вернутся к правильному размеру, но это никак не повлияет на ориентацию.

Что мне не хватает? Любая помощь будет принята с благодарностью.


person Delford Chaffin    schedule 16.10.2015    source источник
comment
PageLayout.Orientation в NovaCode, кажется, имеет много нерешенных проблем. NovaCode отлично подходит для работы с шаблонами документов. Создание и управление новыми документами приведет вас к множеству мелких проблем, которые невозможно решить с помощью текущей версии NovaCode.   -  person Phillip    schedule 16.10.2015


Ответы (1)


Наконец-то! Я разработал удобное решение, которое я размещу здесь в надежде, что оно поможет кому-то другому.

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();

Создание различных разделов как отдельных документов и вставка их в основной документ сработали и решили все мои проблемы.

person Delford Chaffin    schedule 16.10.2015