gpt4 book ai didi

c# - 创建 ImagePart 不会在 OpenXML 中保存关系

转载 作者:行者123 更新时间:2023-11-30 16:15:35 27 4
gpt4 key购买 nike

--已更新以提供完整的工作类示例,以及 2 个示例文档--

www.sklinar.co.uk/wp-content/uploads/mydoc.docx - 带有 INCLUDETEXT 指令的原始文档合并到 Footer.Docx

www.sklinar.co.uk/wp-content/uploads/footer.docx

我已将 ImagePart 添加到我的文档中,并使用 FeedData() 为其提供流式内容。

但是我只能在我的文档中出现一个红色框。

我的初始代码创建了一个 Run 和一个 Drawing 作为它的 child :

--对于这个例子,我使用了一个硬编码的 FileStream 来清除不可靠的图像--

为了让它正常工作,我每次都使用相同的页脚,并且目前使用正确的页脚进行了硬编码:

runToAmend.InsertAfterSelf(CreateImageRun(sourceDocument,run,target.MainDocumentPart.FooterParts.ElementAt(2)));

runToAmend 取自当前文档中的运行 - 它包含一个字段,该字段随后被删除,图片(以及从另一个文档中获取的其他文本)放置在该位置。

    public Run CreateImageRun(WordprocessingDocument sourceDoc,  Run sourceRunFromOriginalDocument, FooterPart footerPart)
{
ImageData shape = sourceRun.Descendants<ImageData>().FirstOrDefault();

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

ImagePart newPart = footerPart.AddImagePart(ImagePartType.Jpeg);

using (Stream stream = new FileStream(@"C:\Users\SAS\Desktop\IMG_20130803_104521.jpg",FileMode.Open,FileAccess.Read))
{
stream.Position = 0;
newPart.FeedData(stream);
}

string partId = footerPart.GetIdOfPart(newPart);

Drawing newImage = CreateImage(partId);
return new Run(newImage);
}

创建绘图的代码

private Drawing CreateImage(string relationshipId)
{
// Define the reference of the image.
return new Drawing(
new DW.Inline(
new DW.Extent() { Cx = 990000L, Cy = 792000L },
new DW.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
},
new DW.DocProperties()
{
Id = (UInt32Value)1U,
Name = "Picture 1"
},
new DW.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks() { NoChangeAspect = true }),
new A.Graphic(
new A.GraphicData(
new PIC.Picture(
new PIC.NonVisualPictureProperties(
new PIC.NonVisualDrawingProperties()
{
Id = (UInt32Value)0U,
Name = "New Bitmap Image.jpg"
},
new PIC.NonVisualPictureDrawingProperties()),
new PIC.BlipFill(
new A.Blip(
new A.BlipExtensionList(
new A.BlipExtension()
{
Uri =
"{28A0092B-C50C-407E-A947-70E740481C1C}"
})
)
{
Embed = relationshipId,
CompressionState =
A.BlipCompressionValues.Print
},
new A.Stretch(
new A.FillRectangle())),
new PIC.ShapeProperties(
new A.Transform2D(
new A.Offset() { X = 0L, Y = 0L },
new A.Extents() { Cx = 990000L, Cy = 792000L }),
new A.PresetGeometry(
new A.AdjustValueList()
) { Preset = A.ShapeTypeValues.Rectangle }))
) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
)
{
DistanceFromTop = (UInt32Value)0U,
DistanceFromBottom = (UInt32Value)0U,
DistanceFromLeft = (UInt32Value)0U,
DistanceFromRight = (UInt32Value)0U,
EditId = "50D07946"
});
}

生成的 XML 看起来是正确的,图像被添加到 /Media/ 文件夹。

<w:r>
<w:drawing>
<wp:inline distT="0" distB="0" distL="0" distR="0" wp14:editId="50D07946">
<wp:extent cx="990000" cy="792000" />
<wp:effectExtent l="0" t="0" r="0" b="0" />
<wp:docPr id="1" name="Picture 1" />
<wp:cNvGraphicFramePr>
<a:graphicFrameLocks noChangeAspect="1" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" />
</wp:cNvGraphicFramePr>
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPr id="0" name="New Bitmap Image.jpg" />
<pic:cNvPicPr />
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="Raae77c5adb2e48f3" cstate="print">
<a:extLst>
<a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}" />
</a:extLst>
</a:blip>
<a:stretch>
<a:fillRect />
</a:stretch>
</pic:blipFill>
<pic:spPr>
<a:xfrm>
<a:off x="0" y="0" />
<a:ext cx="990000" cy="792000" />
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst />
</a:prstGeom>
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
</w:r>

我已经花了大约 2 天时间搜索 SO,Google MS 的糟糕文档。

public class WordTest
{
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Vml;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using A = DocumentFormat.OpenXml.Drawing;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
public void MethodName()
{
WordprocessingDocument mainDoc = WordprocessingDocument.Open("mydoc.docx", true);

foreach (var item in mainDoc.MainDocumentPart.FooterParts)
{
ProcessParaIncludeTextMerge(item, item.Footer.Descendants<Run>(), mainDoc);
item.Footer.Save();
}

mainDoc.MainDocumentPart.Document.Save();

}

private void ProcessParaIncludeTextMerge(OpenXmlPart part, IEnumerable<Run> runs, WordprocessingDocument originalDocument)
{
List<Run> paraRuns = runs.ToList();

int runCount = paraRuns.Count();

for (int i = 0; i < runCount; i++)
{
Run r = paraRuns.ElementAt(i);

// check if this is a simple Merge Field
if (r.HasChildren && r.Descendants<FieldCode>().Any())
{
FieldCode code = r.Descendants<FieldCode>().First();

// we check the first fieldcode is a merge field - but we can't *just* use that one, as for
// some stupid reason, the fieldcodes can be split across runs.... :/
if (code.Text.Trim().IndexOf("INCLUDETEXT", StringComparison.InvariantCultureIgnoreCase) > -1) //this is actually piss-poor, but as a merge field can go across n runs, we simply check for the M and let the other function figure it out..
{
MergeIncludeText(i, paraRuns, originalDocument);
}
}
}
}

public Run CreateImageRun(WordprocessingDocument sourceDoc, Run sourceRun, WordprocessingDocument target, FooterPart footerPart)
{
ImagePart newPart = footerPart.AddImagePart(ImagePartType.Png);

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

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

Bitmap image = new Bitmap(p.GetStream());

using (Stream s = p.GetStream())
{
s.Position = 0;
newPart.FeedData(s);
}

string partId = footerPart.GetIdOfPart(newPart);

Drawing newImage = CreateImage(partId);

return new Run(newImage);
}

private Drawing CreateImage(string relationshipId)
{
// Define the reference of the image.
return new Drawing(
new DW.Inline(
new DW.Extent() { Cx = 990000L, Cy = 792000L },
new DW.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
},
new DW.DocProperties()
{
Id = (UInt32Value)1U,
Name = "Picture 1"
},
new DW.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks() { NoChangeAspect = true }),
new A.Graphic(
new A.GraphicData(
new PIC.Picture(
new PIC.NonVisualPictureProperties(
new PIC.NonVisualDrawingProperties()
{
Id = (UInt32Value)0U,
Name = "New Bitmap Image.jpg"
},
new PIC.NonVisualPictureDrawingProperties()),
new PIC.BlipFill(
new A.Blip(
new A.BlipExtensionList(
new A.BlipExtension()
{
Uri =
"{28A0092B-C50C-407E-A947-70E740481C1C}"
})
)
{
Embed = relationshipId,
CompressionState =
A.BlipCompressionValues.Print
},
new A.Stretch(
new A.FillRectangle())),
new PIC.ShapeProperties(
new A.Transform2D(
new A.Offset() { X = 0L, Y = 0L },
new A.Extents() { Cx = 990000L, Cy = 792000L }),
new A.PresetGeometry(
new A.AdjustValueList()
) { Preset = A.ShapeTypeValues.Rectangle }))
) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
)
{
DistanceFromTop = (UInt32Value)0U,
DistanceFromBottom = (UInt32Value)0U,
DistanceFromLeft = (UInt32Value)0U,
DistanceFromRight = (UInt32Value)0U,
EditId = "50D07946"
});
}

private void ReplaceRunsWithRuns(IEnumerable<Run> fieldRuns, IEnumerable<Run> value, WordprocessingDocument source, WordprocessingDocument target)
{
FooterPart f = target.MainDocumentPart.FooterParts.ElementAt(1);

f.Footer.RemoveAllChildren();

Paragraph p = new Paragraph();
//the run has no value to merge into, remove the field.
if (value != null)
{
foreach (var item in value)
{
if (!item.Descendants<Picture>().Any()) //pictures are processed differently - they're an absolute s**t storm to code...
{
p.Append(item.CloneNode(true));
}
else
{
p.Append(CreateImageRun(source, item, target, f));
}
}
}
else
{
}
f.Footer.Append(p);
}

private void MergeIncludeText(int curRunIdx, List<Run> runs, WordprocessingDocument originalDoc)
{
int startRun = GetBeginRun(runs, curRunIdx);

if (startRun == -1)
{
return;
}

int endRun = GetEndRun(runs, startRun);

if (endRun == -1)
{
return;
}

IEnumerable<Run> fieldRuns = WordMLHelpers.GetRunsBetweenTwoPoints(runs, startRun, endRun);

IEnumerable<FieldCode> fieldCodes = fieldRuns.SelectMany(x => x.Descendants<FieldCode>());

string mergeField = string.Concat(fieldCodes.Select(x => x.Text));

string field = GetIncludeTextFilePath(mergeField);

MemoryStream ms = LoadDocumentStream(field);

WordprocessingDocument includeDoc = LoadDocumentFromStream(ms);

IEnumerable<Run> includedRuns = includeDoc.MainDocumentPart.Document.Descendants<Run>();

ReplaceRunsWithRuns(fieldRuns, includedRuns, includeDoc, originalDoc);
}

private string GetIncludeTextFilePath(string mergeFieldText)
{
int quoteStart = mergeFieldText.IndexOf('"') + 1;

int quoteEnd = mergeFieldText.IndexOf('"', quoteStart);

return mergeFieldText.Substring(quoteStart, quoteEnd - quoteStart);
}

private WordprocessingDocument LoadDocumentFromStream(Stream stream)
{
return WordprocessingDocument.Open(stream, true);
}

#region helpers
public static int GetBeginRun(IEnumerable<Run> runs, int curIdx)
{
for (int i = curIdx; i < runs.Count(); i--)
{
if (i == -1)
{
return -1;
}
Run run = runs.ElementAt(i);

if (run.HasChildren && run.ChildElements.OfType<FieldChar>().Count() > 0
&& (run.ChildElements.OfType<FieldChar>().First().FieldCharType == FieldCharValues.Begin))
{
return i;
}
}

throw new Exception("Begin not found");
}

/// <summary>
/// Get the first End Run in a <see cref="List"/>(<see cref="Run"/>)
/// </summary>
/// <param name="runs">The runs.</param>
/// <param name="curIdx">The cur idx.</param>
/// <returns></returns>
/// <exception cref="System.Exception">End not found</exception>
public static int GetEndRun(IEnumerable<Run> runs, int curIdx)
{
//runs.FirstOrDefault(x => x.HasChildren && x.Descendants<FieldChar>().Count > 0 && (x.Descendants<FieldChar>().First().FieldCharType & FieldCharValues.End) == FieldCharValues.End);

for (int i = curIdx; i < runs.Count(); i++)
{
if (i == -1)
{
return -1;
}
Run run = runs.ElementAt(i);

if (run.HasChildren && run.Descendants<FieldChar>().Any()
&& (run.Descendants<FieldChar>().First().FieldCharType == FieldCharValues.End))
{
return i;
}
}

return -1;
}

public static MemoryStream LoadDocumentStream(string template)
{
using (FileStream fs = File.Open(template, FileMode.Open, FileAccess.Read, FileShare.Read))
{
// first read document in as stream
MemoryStream ms = new MemoryStream();

fs.CopyTo(ms);
ms.Seek(0, SeekOrigin.Begin);
return ms;
}
}
#endregion


}

最佳答案

我已经分析了你的word文档。您的 word 文档存在一些问题:

  1. 如果我尝试在 MS 生产力工具包中打开您的 word 文档,我会收到错误消息 消息“缺少根元素”。

  2. 如果我以 zip 文件的形式打开您的文档(重命名为 mergedfooter.zip),那么 在 footer2.xml.rels 文件中,您的图像的关系丢失了。 我认为这就是您得到“红框”的原因。

为了进一步分析你的问题,我需要完整的代码(你如何获得FooterPart)?

下面是一个示例,说明如何将图像插入到 Word 文档的页脚中。请注意,下面的示例假设您的 word 文档已经包含一个页脚并且您的页脚包含一个段落元素。

using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open("mydoc.docx", true))
{
MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;

// Search for your footer part here.
// Just for the sake of simplicity I take the second footer part.
FooterPart fp = mainPart.FooterParts.ToList()[2];

// Create new image part.
ImagePart ip = fp.AddImagePart(ImagePartType.Jpeg);

using (FileStream fs = File.Open("mypicture.jpg", FileMode.Open))
{
ip.FeedData(fs);
}

string relationshipId = fp.GetIdOfPart(ip);

// Create the image element using your function.
Drawing img = CreateImage(relationshipId);

Run r = new Run(img);

Paragraph para = fp.RootElement.Descendants<Paragraph>().FirstOrDefault();

if(para != null)
{
para.Append(r);
}
else
{
Console.WriteLine("paragraph is null...");
}
}

编辑:

分析您新提供的文件后:

您的图像的关系未保存的原因是因为您没有 Dispose()Close() 您的 word 文档。

所以,只需添加一个 using 语句:

using (WordprocessingDocument mainDoc = WordprocessingDocument.Open("mydoc.docx", true))
{
foreach (var item in mainDoc.MainDocumentPart.FooterParts)
{
ProcessParaIncludeTextMerge(item, item.Footer.Descendants<Run>(), mainDoc);
item.Footer.Save();
}

mainDoc.MainDocumentPart.Document.Save();
}

此外,在您的 ReplaceRunsWithRuns() 方法中,您必须使用PIC.Picture 以引用正确的 Picture 类:

foreach (var item in value)
{
if (!item.Descendants<PIC.Picture>().Any())
{
p.Append(item.CloneNode(true));
}
else
{
p.Append(CreateImageRun(source, item, target, f));
}
}

出于同样的原因,在你的 CreateImageRun() 方法中我已经改变了前三行代码:

ImagePart newPart = footerPart.AddImagePart(ImagePartType.Jpeg);                   
A.Blip shape = sourceRun.Descendants<A.Blip>().FirstOrDefault();
ImagePart p = sourceDoc.MainDocumentPart.GetPartById(shape.Embed.Value) as ImagePart;

通过这些更改,图像出现在 mydoc.docx word 文档中。

关于c# - 创建 ImagePart 不会在 OpenXML 中保存关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19384826/

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