Przeglądaj źródła

Changes to the merge_images method:

- Fixes "This image cannot currently be displayed" errors after using InsertDocument with documents containing images
* Looks for images inside /word/media folder as well as images in the /media folder
* Updates the relationship id in both document.xml and document.xml.rels (in the local document as well)
- Adds the images to the /word/media folder instead of the /media folder

+ Adds the "image/jpg" type to the imageContentTypes list

Patch provided by Annika89
master
MadBoy_cp 11 lat temu
rodzic
commit
3b00b4207d
1 zmienionych plików z 42 dodań i 3 usunięć
  1. 42
    3
      DocX/DocX.cs

+ 42
- 3
DocX/DocX.cs Wyświetl plik

@@ -983,6 +983,7 @@ namespace Novacode
List<String> imageContentTypes = new List<string>
{
"image/jpeg",
"image/jpg",
"image/png",
"image/bmp",
"image/gif",
@@ -1152,11 +1153,15 @@ namespace Novacode
private void merge_images(PackagePart remote_pp, DocX remote_document, XDocument remote_mainDoc, String contentType)
{
// Before doing any other work, check to see if this image is actually referenced in the document.
// In my testing I have found cases of Images inside documents that are not refer
// In my testing I have found cases of Images inside documents that are not referenced
var remote_rel = remote_document.mainPart.GetRelationships().Where(r => r.TargetUri.OriginalString.Equals(remote_pp.Uri.OriginalString.Replace("/word/", ""))).FirstOrDefault();
if (remote_rel == null)
return;
{
remote_rel = remote_document.mainPart.GetRelationships().Where(r => r.TargetUri.OriginalString.Equals(remote_pp.Uri.OriginalString)).FirstOrDefault();
if (remote_rel == null)
return;
}
String remote_Id = remote_rel.Id;
String remote_hash = ComputeMD5HashString(remote_pp.GetStream());
@@ -1172,6 +1177,10 @@ namespace Novacode
found = true;
var local_rel = mainPart.GetRelationships().Where(r => r.TargetUri.OriginalString.Equals(part.Uri.OriginalString.Replace("/word/", ""))).FirstOrDefault();
if (local_rel == null)
{
local_rel = mainPart.GetRelationships().Where(r => r.TargetUri.OriginalString.Equals(part.Uri.OriginalString)).FirstOrDefault();
}
if (local_rel != null)
{
String new_Id = local_rel.Id;
@@ -1208,7 +1217,7 @@ namespace Novacode
{
String new_uri = remote_pp.Uri.OriginalString;
new_uri = new_uri.Remove(new_uri.LastIndexOf("/"));
new_uri = new_uri.Replace("word/", "");
//new_uri = new_uri.Replace("word/", "");
new_uri += "/" + Guid.NewGuid().ToString() + contentType.Replace("image/", ".");
if (!new_uri.StartsWith("/"))
new_uri = "/" + new_uri;
@@ -1232,6 +1241,9 @@ namespace Novacode
String new_Id = pr.Id;
//Check if the remote relationship id is a default rId from Word
Match defRelId = Regex.Match(remote_Id, @"rId\d+", RegexOptions.IgnoreCase);
// Replace all instances of remote_Id in the local document with local_Id
var elems = remote_mainDoc.Descendants(XName.Get("blip", DocX.a.NamespaceName));
foreach (var elem in elems)
@@ -1243,6 +1255,33 @@ namespace Novacode
}
}
if (!defRelId.Success)
{
// Replace all instances of remote_Id in the local document with local_Id
var elems_local = mainDoc.Descendants(XName.Get("blip", DocX.a.NamespaceName));
foreach (var elem in elems_local)
{
XAttribute embed = elem.Attribute(XName.Get("embed", DocX.r.NamespaceName));
if (embed != null && embed.Value == remote_Id)
{
embed.SetValue(new_Id);
}
}
// Replace all instances of remote_Id in the local document with local_Id
var v_elems_local = mainDoc.Descendants(XName.Get("imagedata", DocX.v.NamespaceName));
foreach (var elem in v_elems_local)
{
XAttribute id = elem.Attribute(XName.Get("id", DocX.r.NamespaceName));
if (id != null && id.Value == remote_Id)
{
id.SetValue(new_Id);
}
}
}
// Replace all instances of remote_Id in the local document with local_Id (for shapes as well)
var v_elems = remote_mainDoc.Descendants(XName.Get("imagedata", DocX.v.NamespaceName));
foreach (var elem in v_elems)

Ładowanie…
Anuluj
Zapisz