MS Word、OpenXML、PageSetup、方向和 4_directional 页边距

MS Word, OpenXML, PageSetup, Orientation and 4_directional Margins

我用 OpenXML 制作了这个文档。。我正在学习 OpenXML。唉。。好难啊

MainDocumentPart m = wd.AddMainDocumentPart();
m.Document = new Document();
Body b1 = new Body();
int myCount = 5;
for (int z = 1; z <= myCount; z++)
{
    Paragraph p1 = new Paragraph();
    Run r1 = new Run();
    Text t1 = new Text(
        "The Quick Brown Fox Jumps Over The Lazy Dog  " + z );
    r1.Append(t1);                      
    p1.Append(r1);
    b1.Append(p1);
}
m.Document.Append(b1);

我想将其方向从纵向更改为横向并将其边距设置得更小。

处理前;

处理后;

我可以用 VBA 这样的代码实现这个目标;

With ActiveDocument.PageSetup
    .Orientation = wdOrientLandscape  
    .TopMargin = CentimetersToPoints(1.27)
    .BottomMargin = CentimetersToPoints(1.27)
    .LeftMargin = CentimetersToPoints(1.27)
    .RightMargin = CentimetersToPoints(1.27)
End With

但是,当我进入 OpenXML 区域时,情况就大不相同了。

能给我一些建议吗?

此致

您需要像这样使用 SectionProperties, PageSize and PageMargin 类:

using (WordprocessingDocument wd = WordprocessingDocument.Create(filename, WordprocessingDocumentType.Document))
{
    MainDocumentPart m = wd.AddMainDocumentPart();
    m.Document = new Document();
    Body b1 = new Body();

    //new code to support orientation and margins
    SectionProperties sectProp = new SectionProperties();
    PageSize pageSize = new PageSize() { Width = 16838U, Height = 11906U, Orient = PageOrientationValues.Landscape };
    PageMargin pageMargin = new PageMargin() { Top = 720, Right = 720U, Bottom = 720, Left = 720U };

    sectProp.Append(pageSize);
    sectProp.Append(pageMargin);
    b1.Append(sectProp);
    //end new code

    int myCount = 5;
    for (int z = 1; z <= myCount; z++)
    {
        Paragraph p1 = new Paragraph();
        Run r1 = new Run();
        Text t1 = new Text(
            "The Quick Brown Fox Jumps Over The Lazy Dog  " + z);
        r1.Append(t1);
        p1.Append(r1);
        b1.Append(p1);
    }
    m.Document.Append(b1);
}

请注意,页边距值以二十分之一点定义。 1.27cm大约是36分,也就是720分之二。