gpt4 book ai didi

c# - 将 openXML 图像从一个文档复制到另一个文档

转载 作者:行者123 更新时间:2023-11-30 15:32:19 26 4
gpt4 key购买 nike

我们有条件页脚 INCLUDETEXT基于客户端:

IF $CLIENT = "CLIENT1" "{INCLUDETEXT "CLIENT1HEADER.DOCX"}" ""

根据我们的文档,可能会有不同数量的 IF/ELSE ,这些都可以在正确的位置合并正确的文件。

但是,其中一些文件可能有客户特定的图像/品牌,也需要从 INCLUDETEXT 复制过来文件。

下面是调用来替换任何 Picture 的方法IEnumerable<Run> 中存在的元素从源文档复制到目标文档。

图像复制得很好,但它似乎没有更新我的 Picture 中的 RID。或将记录添加到 .XML.Rels 文件中。 (我什至尝试添加 ForEach 以添加到所有页眉和页脚,看看这是否有任何不同。

    private void InsertImagesFromOldDocToNewDoc(WordprocessingDocument source, WordprocessingDocument target, IEnumerable<Picture> pics)
{
IEnumerable<Picture> imageElements = source.MainDocumentPart.Document.Descendants<Run>().Where(x => x.Descendants<Picture>().FirstOrDefault() != null).Select(x => x.Descendants<Picture>().FirstOrDefault());

foreach (Picture pic in pics) //the new pics
{
Picture oldPic = imageElements.Where(x => x.Equals(pic)).FirstOrDefault();

if (oldPic != null)
{
string imageId = "";

ImageData shape = oldPic.Descendants<ImageData>().FirstOrDefault();

ImagePart p = source.MainDocumentPart.GetPartById(shape.RelationshipId) as ImagePart;

ImagePart newPart = target.MainDocumentPart.AddPart<ImagePart>(p);

newPart.FeedData(p.GetStream());

shape.RelId = target.MainDocumentPart.GetIdOfPart(newPart);
string relPart = target.MainDocumentPart.CreateRelationshipToPart(newPart);
}
}
}

有没有人遇到过这个问题?

OpenXML SDK 文档似乎“有点”稀疏...

最佳答案

react 迟了,但这个帖子对我的工作帮助很大。这是我复制带有图像的文档的解决方案

private static void CopyDocumentWithImages(string path)
{
if (!Path.GetFileName(path).StartsWith("~$"))
{
using (var source = WordprocessingDocument.Open(path, false))
{
using (var newDoc = source.CreateNew(path.Replace(".docx", "-images.docx")))
{
foreach (var e in source.MainDocumentPart.Document.Body.Elements())
{
var clonedElement = e.CloneNode(true);
clonedElement.Descendants<DocumentFormat.OpenXml.Drawing.Blip>()
.ToList().ForEach(blip =>
{
var newRelation = newDoc.CopyImage(blip.Embed, source);
blip.Embed = newRelation;
});
clonedElement.Descendants<DocumentFormat.OpenXml.Vml.ImageData>().ToList().ForEach(imageData =>
{
var newRelation = newDoc.CopyImage(imageData.RelationshipId, source);
imageData.RelationshipId = newRelation;
});
newDoc.MainDocumentPart.Document.Body.AppendChild(clonedElement);
}
newDoc.Save();
}
}
}
}

复制图片:

public static string CopyImage(this WordprocessingDocument newDoc, string relId, WordprocessingDocument org)
{
var p = org.MainDocumentPart.GetPartById(relId) as ImagePart;
var newPart = newDoc.MainDocumentPart.AddPart(p);
newPart.FeedData(p.GetStream());
return newDoc.MainDocumentPart.GetIdOfPart(newPart);
}

新建:

public static WordprocessingDocument CreateNew(this WordprocessingDocument org, string name)
{
var doc = WordprocessingDocument.Create(name, WordprocessingDocumentType.Document);
doc.AddMainDocumentPart();
doc.MainDocumentPart.Document = new Document(new Body());
using (var streamReader = new StreamReader(org.MainDocumentPart.ThemePart.GetStream()))
using (var streamWriter = new StreamWriter(doc.MainDocumentPart.AddNewPart<ThemePart>().GetStream(FileMode.Create)))
{
streamWriter.Write(streamReader.ReadToEnd());
}
using (var streamReader = new StreamReader(org.MainDocumentPart.StyleDefinitionsPart.GetStream()))
using (var streamWriter = new StreamWriter(doc.MainDocumentPart.AddNewPart<StyleDefinitionsPart>().GetStream(FileMode.Create)))
{
streamWriter.Write(streamReader.ReadToEnd());
}
return doc;
}

关于c# - 将 openXML 图像从一个文档复制到另一个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19301256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com