我使用 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
和 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.27 厘米大约为 36 点,即 720 二十分之一点。