php分界符,c# – 如何使用openxml添加分节符下一页?

我想在文档的末尾添加一个分节符并添加一些文本.

我的代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using DocumentFormat.OpenXml.Packaging;

using DocumentFormat.OpenXml.Wordprocessing;

namespace WordDocManipulation

{

class Program

{

static void Main(string[] args)

{

string path = @"C:\sample.docx";

string strtxt = "Hello This is done by programmatically";

OpenAndAddTextToWordDocument(path,strtxt);

}

public static void OpenAndAddTextToWordDocument(string filepath,string txt)

{

/* I want to the below text to be added in the new section */

// Open a WordprocessingDocument for editing using the filepath.

WordprocessingDocument wordprocessingDocument =

WordprocessingDocument.Open(filepath,true);

// Assign a reference to the existing document body.

Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

// Add new text.

Paragraph para = body.AppendChild(new Paragraph());

Run run = para.AppendChild(new Run());

run.AppendChild(new Text(txt));

// Close the handle explicitly.

wordprocessingDocument.Close();

}

}

}

我该怎么办?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C# Open XML添加图片并居中的示例代码: ```csharp using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; using A = DocumentFormat.OpenXml.Drawing; using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing; using PIC = DocumentFormat.OpenXml.Drawing.Pictures; public void AddCenteredImage(string imagePath, string documentPath) { using (WordprocessingDocument document = WordprocessingDocument.Open(documentPath, true)) { MainDocumentPart mainPart = document.MainDocumentPart; // 创建一个段落 Paragraph paragraph = new Paragraph(); // 创建一个段落属性 ParagraphProperties paragraphProperties = new ParagraphProperties(); Justification justification = new Justification() { Val = JustificationValues.Center }; // 将段落属性添加到段落 paragraphProperties.Append(justification); paragraph.Append(paragraphProperties); // 创建一个Run Run run = new Run(); // 创建一个Drawing Drawing drawing = new Drawing(); // 创建一个Inline Inline inline = new Inline(); // 创建一个Graphic Graphic graphic = new Graphic(); // 创建一个GraphicData GraphicData graphicData = new GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }; // 创建一个Picture PIC.Picture picture = new PIC.Picture(); // 创建一个BlipFill PIC.BlipFill blipFill = new PIC.BlipFill(); // 创建一个Blip A.Blip blip = new A.Blip() { Embed = mainPart.GetIdOfPart(mainPart.AddImagePart(ImagePartType.Jpeg)) }; // 创建一个Stretch A.Stretch stretch = new A.Stretch(); A.FillRectangle fillRectangle = new A.FillRectangle(); // 将Blip添加到BlipFill blipFill.Append(blip); blipFill.Append(stretch); // 将BlipFill添加到Picture picture.Append(blipFill); picture.Append(fillRectangle); // 创建一个ShapeProperties PIC.ShapeProperties shapeProperties = new PIC.ShapeProperties(); // 创建一个Transform2D A.Transform2D transform2D = new A.Transform2D(); // 创建一个Offset A.Offset offset = new A.Offset() { X = 0, Y = 0 }; // 创建一个Extent A.Extents extents = new A.Extents() { Cx = 914400, Cy = 914400 }; // 将Offset和Extent添加到Transform2D transform2D.Append(offset); transform2D.Append(extents); // 将Transform2D添加到ShapeProperties shapeProperties.Append(transform2D); // 将ShapeProperties添加到Picture picture.Append(shapeProperties); // 将Picture添加到GraphicData graphicData.Append(picture); // 将GraphicData添加到Graphic graphic.Append(graphicData); // 将Graphic添加到Inline inline.Append(graphic); // 将Inline添加到Drawing drawing.Append(inline); // 将Drawing添加到Run run.Append(drawing); // 将Run添加到段落 paragraph.Append(run); // 将段落添加到文档主体 mainPart.Document.Body.Append(paragraph); // 保存文档 mainPart.Document.Save(); } } ``` 请注意,您需要将`imagePath`替换为您要添加的图片的路径,将`documentPath`替换为您要保存的Word文档的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值