我使用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);


c#-4.0 - MS Word,OpenXML,PageSetup,方向和4向边距-LMLPHP

我想将其方向从纵向->横向更改为较小的边距。

处理之前;

c#-4.0 - MS Word,OpenXML,PageSetup,方向和4向边距-LMLPHP

处理后;
c#-4.0 - MS Word,OpenXML,PageSetup,方向和4向边距-LMLPHP

我可以通过这样的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领域时,情况就大不相同了。

我可以给我一些提示吗?

问候

最佳答案

您需要像这样使用SectionPropertiesPageSizePageMargin类:

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二十分。

关于c#-4.0 - MS Word,OpenXML,PageSetup,方向和4向边距,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45003078/

10-14 16:23