我将HTML导入到word文档中,我将body标签中的宽度设置为max-width:10px,但文本不受影响。
using (WordprocessingDocument doc = WordprocessingDocument.Open(@"mydoc.docx", true))
{
var mainPart = document.MainDocumentPart;
string altChunkId = "AltChunkID" + "R" + Guid.NewGuid().ToString();
MainDocumentPart mainDocPart = doc.MainDocumentPart;
string html = "<html><head></head> <body style='max-width: 10px;'>" + actualhtml + "</body></html>";
AlternativeFormatImportPart formatImportPart =
document.MainDocumentPart.AddAlternativeFormatImportPart
(AlternativeFormatImportPartType.Html, altChunkId);
MemoryStream ms = new MemoryStream(newUTF8Encoding(true).GetPreamble().Concat(Encoding.UTF8.GetBytes(html)).ToArray());
formatImportPart.FeedData(ms);
mainDocPart.Document.Body.Append(new Paragraph(
new Run(new RunProperties(new AltChunk()
{ Id = altChunkId }),
(new AltChunkProperties(new MatchSource() {Val = true })))));
}
注意:如果我使用上述HTML并在浏览器中打开,则宽度是正确的。