MS Word,OpenXML,PageSetup,方向和4向边距 [英] MS Word, OpenXML, PageSetup, Orientation and 4_directional Margins

查看:229
本文介绍了MS Word,OpenXML,PageSetup,方向和4向边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OpenXML制作了此文档..我正在学习OpenXML.哦..这太难了.

I made this document with OpenXML. . I'm learning OpenXML. Oh.. it is so difficult.

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

我想从纵向->横向更改其方向,并将其边距设置得较小.

I'd like to change its orientation from portrait -> landscape and to set its margin smaller.

处理之前;

处理后;

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

I can achieve this goal with VBA codes like this;

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

但是,当我进入OpenXML领域时,情况就完全不同了.

But, when I go to OpenXML area, it is quite different.

我能给我一些提示吗?

致谢

推荐答案

您需要使用 PageSize

You need to use the SectionProperties, PageSize and PageMargin classes like so:

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.27厘米大约是36点,相当于一点的720二十分.

Note that the page margin values are defined in twentieths of a point. 1.27cm is roughly 36 points which is 720 twentieths of a point.

这篇关于MS Word,OpenXML,PageSetup,方向和4向边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆