hande hyplerlink references in header or footers

This commit is contained in:
amitm02
2018-10-02 16:17:26 +03:00
parent a1e20f4c9a
commit ffdcc7baca
3 changed files with 31 additions and 7 deletions

View File

@ -55,6 +55,15 @@ export class FooterWrapper {
return mediaData;
}
public addHyperlinkRelationship(target: string, refId: number, targetMode?: "External" | undefined): void {
this.relationships.createRelationship(
refId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
target,
targetMode,
);
}
public createImage(image: Buffer, width?: number, height?: number): void {
const mediaData = this.addImageRelationship(image, this.relationships.RelationshipCount, width, height);
this.addImage(new Image(new ImageParagraph(mediaData)));

View File

@ -55,6 +55,15 @@ export class HeaderWrapper {
return mediaData;
}
public addHyperlinkRelationship(target: string, refId: number, targetMode?: "External" | undefined): void {
this.relationships.createRelationship(
refId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
target,
targetMode,
);
}
public createImage(image: Buffer, width?: number, height?: number): void {
const mediaData = this.addImageRelationship(image, this.relationships.RelationshipCount, width, height);
this.addImage(new Image(new ImageParagraph(mediaData)));

View File

@ -20,6 +20,7 @@ const schemeToType = {
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header": "header",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer": "footer",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image": "image",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink": "hyperlink",
};
interface IDocumentRefs {
@ -29,8 +30,8 @@ interface IDocumentRefs {
interface IRelationshipFileInfo {
id: number;
targetFile: string;
type: "header" | "footer" | "image";
target: string;
type: "header" | "footer" | "image" | "hyperlink";
}
// Document Template
@ -70,7 +71,7 @@ export class ImportDotx {
throw new Error(`Can not find target file for id ${headerRef.id}`);
}
const xmlData = await zipContent.files[`word/${relationFileInfo.targetFile}`].async("text");
const xmlData = await zipContent.files[`word/${relationFileInfo.target}`].async("text");
const xmlObj = fastXmlParser.parse(xmlData, importParseOptions);
const importedComp = convertToXmlComponent(headerKey, xmlObj[headerKey]) as ImportedXmlComponent;
@ -87,7 +88,7 @@ export class ImportDotx {
if (relationFileInfo === null || !relationFileInfo) {
throw new Error(`Can not find target file for id ${footerRef.id}`);
}
const xmlData = await zipContent.files[`word/${relationFileInfo.targetFile}`].async("text");
const xmlData = await zipContent.files[`word/${relationFileInfo.target}`].async("text");
const xmlObj = fastXmlParser.parse(xmlData, importParseOptions);
const importedComp = convertToXmlComponent(footerKey, xmlObj[footerKey]) as ImportedXmlComponent;
@ -106,15 +107,20 @@ export class ImportDotx {
wrapper: HeaderWrapper | FooterWrapper,
): Promise<void> {
let wrapperImagesReferences: IRelationshipFileInfo[] = [];
const refFile = zipContent.files[`word/_rels/${relationFile.targetFile}.rels`];
let hyperLinkReferences: IRelationshipFileInfo[] = [];
const refFile = zipContent.files[`word/_rels/${relationFile.target}.rels`];
if (refFile) {
const xmlRef = await refFile.async("text");
wrapperImagesReferences = this.findReferenceFiles(xmlRef).filter((r) => r.type === "image");
hyperLinkReferences = this.findReferenceFiles(xmlRef).filter((r) => r.type === "hyperlink");
}
for (const r of wrapperImagesReferences) {
const buffer = await zipContent.files[`word/${r.targetFile}`].async("nodebuffer");
const buffer = await zipContent.files[`word/${r.target}`].async("nodebuffer");
wrapper.addImageRelationship(buffer, r.id);
}
for (const r of hyperLinkReferences) {
wrapper.addHyperlinkRelationship(r.target, r.id, "External");
}
}
public findReferenceFiles(xmlData: string): IRelationshipFileInfo[] {
@ -127,7 +133,7 @@ export class ImportDotx {
return {
id: this.parseRefId(item._attr.Id),
type: schemeToType[item._attr.Type],
targetFile: item._attr.Target,
target: item._attr.Target,
};
})
.filter((item) => item.type !== null);