简体   繁体   English

如何将 AltChunk 插入 docx 页面 header?

[英]How to insert AltChunk to docx page header?

I'm trying to build a docx document with HTML content in the page headers.我正在尝试在页眉中构建一个包含 HTML 内容的 docx 文档。 This is what my document.xml body looks like:这是我的 document.xml 正文的样子:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document ...xmlns declarations.. mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
    <w:body>      
        <w:p w14:paraId="43132D41" w14:textId="3A8DC205" w:rsidR="00940BB3" w:rsidRDefault="00772215">
            <w:r>
                <w:t>Hello</w:t>
            </w:r>
        </w:p>
        <w:sectPr w:rsidR="00940BB3">
            <w:headerReference w:type="even" r:id="rId7"/>
            <w:headerReference w:type="default" r:id="rId8"/>
            <w:footerReference w:type="even" r:id="rId9"/>
            <w:footerReference w:type="default" r:id="rId10"/>
            <w:headerReference w:type="first" r:id="rId11"/>
            <w:footerReference w:type="first" r:id="rId12"/>
            <w:pgSz w:w="12240" w:h="15840"/>
            <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>
            <w:cols w:space="708"/>
            <w:docGrid w:linePitch="360"/>
        </w:sectPr>
    </w:body>
</w:document>

rId8 header refers to header2.xml which looks like this: rId8 header 指的是 header2.xml,它看起来像这样:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:hdr ...lots of xmlns... mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
    <w:p w14:paraId="775C4615" w14:textId="387F538F" w:rsidR="00772215" w:rsidRDefault="00772215">
        <w:pPr>
            <w:pStyle w:val="Header"/>
        </w:pPr>
        <w:r>
            <w:t>The Header</w:t>
        </w:r>
    </w:p>
    <w:p w14:paraId="746D41E6" w14:textId="77777777" w:rsidR="00772215" w:rsidRDefault="00772215">
        <w:pPr>
            <w:pStyle w:val="Header"/>
        </w:pPr>
    </w:p>
    <w:altChunk r:id="htmlchunk"/>
</w:hdr>

And htmlchunk refers to a simple html file:htmlchunk指的是一个简单的 html 文件:

<!DOCTYPE html>
<html>
<body><b>Real HTML</b></body>
</html>

This is how these references look in the document.rels.xml:这是这些引用在 document.rels.xml 中的样子:

    <Relationship Id="htmlchunk" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/aFChunk" Target="htmlchunk.html"/>
    <Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" Target="header2.xml"/>

When trying to open the resulting document in MS Word, it tries to repair the document and then shows the header "The Header" portion without the html contents.当尝试在 MS Word 中打开生成的文档时,它会尝试修复文档,然后显示 header “标头”部分,而没有 html 内容。 When trying to validate the document with OOXMLValidator, it shows this error:尝试使用 OOXMLValidator 验证文档时,它显示此错误:

[{"Description":"The relationship 'htmlchunk' referenced by attribute 'http://schemas.openxmlformats.org/officeDocument/2006/relationships:id' does not exist.","Path":{"NamespacesDefinitions":["xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\""],"Namespaces":{},"XPath":"/w:hdr[1]/w:altChunk[1]","PartUri":"/word/header2.xml"},"Id":"Sem_InvalidRelationshipId","ErrorType":"Semantic"}]

How can I solve this problem?我怎么解决这个问题? How can the header xml file refer to other chunk? header xml 文件如何引用其他块?

To reference the part identified by "htmlchunk" , that part must be added to the header part ( header2.xml in your example) rather than the main document part ( document.xml ).要引用由"htmlchunk"标识的部分,该部分必须添加到header2.xml部分(在您的示例中为 header2.xml )而不是主文档部分( document.xml )。 This means that the Relationship element would not be found in document.xml.rels but in header2.xml.rels .这意味着Relationship元素不会在document.xml.rels中找到,而是在header2.xml.rels中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM