2013-03-27 4 views
5

Ich erzeuge ein Word-Dokument mit C# und Open XML SDK. Ich möchte die Ausrichtung einiger Absätze und ihrer Seiten auf Querformat ändern, während die anderen im Hochformat bleiben.
Ich habe versucht, einige Lösungen, aber sie haben nicht erreicht, was ich suche, und stattdessen die Ausrichtung aller Seiten wurden in Landschaft geändert, mit Ausnahme der ersten. Zu diesen Lösungen gehören:
- Hinzufügen von SectionProperties auf den Absatz, die ich ändern will seine Ausrichtung auf Landschaft:Wie kann die Ausrichtung einzelner Absätze oder Seiten mit Open XML und C# geändert werden?

WordprocessingDocument WordDocument = WordprocessingDocument.Open(ReportFile, true) 
Paragraph paragraph = new Paragraph(new ParagraphProperties(
       new SectionProperties(new PageSize() 
       { Width = (UInt32Value)15840U, Height = (UInt32Value)12240U, Orient = PageOrientationValues.Landscape }))); 
WordDocument.MainDocumentPart.Document.Body.Append(paragraph); 


- Hinzufügen einen neuen Körper zum Hauptdokument und Anhänge des Absatzes es:

WordprocessingDocument WordDocument = WordprocessingDocument.Open(ReportFile, true)  
Body body = new Body();   
Paragraph paragraph = new Paragraph(new ParagraphProperties(
       new SectionProperties(new PageSize() 
       { Width = (UInt32Value)15840U, Height = (UInt32Value)12240U, Orient = PageOrientationValues.Landscape }))); 
body.Append(paragraph); 
WordDocument.MainDocumentPart.Document.Append(body); 


- einen neuen Körper zum Hauptdokument Hinzufügen und Anhängen von SectionProprties es direkt:

WordprocessingDocument WordDocument = WordprocessingDocument.Open(ReportFile, true) 
Body body = new Body(); 
Paragraph paragraph = new Paragraph(); 
SectionProperties sectionProp = new SectionProperties(new PageSize() { Width = (UInt32Value)15840U, Height = (UInt32Value)12240U, Orient = PageOrientationValues.Landscape }); 
body.Append(paragraph); 
body.Append(sectionProp); 
WordDocument.MainDocumentPart.Document.Append(body); 

Gibt es also eine Möglichkeit, die Ausrichtung eines einzelnen Absatzes/einer Seite zu ändern?

+0

Ich bezweifle, wenn Sie die Ausrichtung eines einzelnen Absatzes ändern können MS mit Office Word (Anwendung) selbst? –

+0

Sie können es in MS Office Word mit benutzerdefinierten Rändern tun. Überprüfen Sie diesen Link http://www.officetooltips.com/word/tips/how_to_use_different_page_orientations_inside_one_document.html –

+0

OK ich überprüft. Es fügt aber auch vor und nach dem ausgewählten Text die Abschnittsumbrüche ein. So werden zum Beispiel 3 Absätze mit einer Landschaft und andere im Hochformat auf 3 Seiten des Dokuments gemacht. Ist das was du willst? –

Antwort

7

Sie haben einen nächsten Seite Abschnittsumbruch setzen einen neuen Abschnitt zu beginnen, und stellen Sie die Ausrichtung für die Sektion. Sobald Sie fertig sind, starten Sie einen neuen Abschnitt mit der Standardseitenausrichtung.

Nächste Seite Abschnittswechsel starten Querformat:

doc.MainDocumentPart.Document.Body.Append(
    new Paragraph(
     new ParagraphProperties(
      new SectionProperties(
       new PageSize() { Width = (UInt32Value)12240U, Height = (UInt32Value)15840U, Orient = PageOrientationValues.Landscape }, 
       new PageMargin() { Top = 720, Right = Convert.ToUInt32(rightmargin * 1440.0), Bottom = 360, Left = Convert.ToUInt32(leftmargin * 1440.0), Header = (UInt32Value)450U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U })))); 

Nächste Seite Abschnittswechsel zu Hochformat zu starten:

doc.MainDocumentPart.Document.Body.Append(
    new Paragraph(
     new ParagraphProperties(
      new SectionProperties(
       new PageSize() { Width = (UInt32Value)12240U, Height = (UInt32Value)15840U }, 
       new PageMargin() { Top = 720, Right = Convert.ToUInt32(rightmargin * 1440.0), Bottom = 360, Left = Convert.ToUInt32(leftmargin * 1440.0), Header = (UInt32Value)450U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U })))); 
3

Basierend auf der obigen Konversation und meinem Verständnis, könnte der Code etwa wie folgt aussehen.

In Main-Methode Aufruf Unter Methoden:

  using (WordprocessingDocument package = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document)) 
      { 
       package.AddMainDocumentPart(); 
       package.MainDocumentPart.Document = new Document(); 
       Body body = package.MainDocumentPart.Document.Body = new Body(); 

       Paragraph para1 = CreatePara1(); // Paragraph 1 Portrait 

       Paragraph breakBefore = CreateBreakBefore(); // Break before next paragraph that would be landscape 

       Paragraph para2 = CreatePara2(); // Paragraph 2 Landscape 

       Paragraph breakAfter = CreateBreakAfter(); // Break after previous paragraph that was landscape 

       Paragraph para3 = CreatePara3(); // Paragraph 3 Portrait again 

       body.Append(para1); 
       body.Append(breakBefore); 
       body.Append(para2); 
       body.Append(breakAfter); 
       body.Append(para3); 
      } 

Folgt Unter Methoden:

private static Paragraph CreatePara1() 
    { 
     Paragraph para1 = new Paragraph(); 
     Run run1 = new Run(new Text("This is Paragraph one.")); 
     para1.Append(run1); 
     return para1; 
    } 

    private static Paragraph CreateBreakBefore() 
    { 
     Paragraph paragraph2 = new Paragraph() { RsidParagraphAddition = "00BA2F0F", RsidParagraphProperties = "00BA2F0F", RsidRunAdditionDefault = "00BA2F0F" }; 

     ParagraphProperties paragraphProperties1 = new ParagraphProperties(); 

     SectionProperties sectionProperties1 = new SectionProperties() { RsidR = "00BA2F0F" }; 
     PageSize pageSize1 = new PageSize() { Width = (UInt32Value)12240U, Height = (UInt32Value)15840U }; 
     PageMargin pageMargin1 = new PageMargin() { Top = 1440, Right = (UInt32Value)1440U, Bottom = 1440, Left = (UInt32Value)1440U, Header = (UInt32Value)720U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U }; 
     Columns columns1 = new Columns() { Space = "720" }; 
     DocGrid docGrid1 = new DocGrid() { LinePitch = 360 }; 

     sectionProperties1.Append(pageSize1); 
     sectionProperties1.Append(pageMargin1); 
     sectionProperties1.Append(columns1); 
     sectionProperties1.Append(docGrid1); 

     paragraphProperties1.Append(sectionProperties1); 

     paragraph2.Append(paragraphProperties1); 
     return paragraph2; 
    } 

    private static Paragraph CreatePara2() 
    { 
     Paragraph para2 = new Paragraph() { RsidParagraphAddition = "00BA2F0F", RsidParagraphProperties = "00BA2F0F", RsidRunAdditionDefault = "00BA2F0F" }; 
     BookmarkStart bookmarkStart1 = new BookmarkStart() { Name = "_GoBack", Id = "0" }; 

     Run run2 = new Run(); 
     LastRenderedPageBreak lastRenderedPageBreak1 = new LastRenderedPageBreak(); 
     Text text2 = new Text(); 
     text2.Text = "This is the paragraph two"; 

     run2.Append(lastRenderedPageBreak1); 
     run2.Append(text2); 

     para2.Append(bookmarkStart1); 
     para2.Append(run2); 
     BookmarkEnd bookmarkEnd1 = new BookmarkEnd() { Id = "0" }; 
     return para2; 
    } 

    private static Paragraph CreateBreakAfter() 
    { 
     Paragraph paragraph4 = new Paragraph() { RsidParagraphAddition = "00BA2F0F", RsidParagraphProperties = "00BA2F0F", RsidRunAdditionDefault = "00BA2F0F" }; 

     ParagraphProperties paragraphProperties2 = new ParagraphProperties(); 

     SectionProperties sectionProperties2 = new SectionProperties() { RsidR = "00BA2F0F", RsidSect = "00BA2F0F" }; 
     PageSize pageSize2 = new PageSize() { Width = (UInt32Value)15840U, Height = (UInt32Value)12240U, Orient = PageOrientationValues.Landscape }; 
     PageMargin pageMargin2 = new PageMargin() { Top = 1440, Right = (UInt32Value)1440U, Bottom = 1440, Left = (UInt32Value)1440U, Header = (UInt32Value)720U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U }; 
     Columns columns2 = new Columns() { Space = "720" }; 
     DocGrid docGrid2 = new DocGrid() { LinePitch = 360 }; 

     sectionProperties2.Append(pageSize2); 
     sectionProperties2.Append(pageMargin2); 
     sectionProperties2.Append(columns2); 
     sectionProperties2.Append(docGrid2); 

     paragraphProperties2.Append(sectionProperties2); 

     paragraph4.Append(paragraphProperties2); 
     return paragraph4; 
    } 

    private static Paragraph CreatePara3() 
    { 
     Paragraph para3 = new Paragraph(); 
     Run run3 = new Run(new Text("This is Paragraph three.")); 
     para3.Append(run3); 
     return para3; 
    } 
+1

Der obige Beispielcode weist die Querformatausrichtung in der CreateBreakAfter() -Methode zu. Zuerst wird der Absatz hinzugefügt und dann wird die Ausrichtung in einem anderen Absatz festgelegt. Nur um klar zu sein, um anderen zu helfen. –

Verwandte Themen