Bladeren bron

Fixed:

- AddImage does not work when document has remote images.

patch provided by sdpfigueiredo
master
MadBoy_cp 11 jaren geleden
bovenliggende
commit
dcc091a6a7
1 gewijzigde bestanden met toevoegingen van 23 en 9 verwijderingen
  1. 23
    9
      DocX/DocX.cs

+ 23
- 9
DocX/DocX.cs Bestand weergeven

@@ -3033,12 +3033,15 @@ namespace Novacode
// Open a Stream to the new image being added.
Stream newImageStream;
if (o is string)
newImageStream = new FileStream(o as string, FileMode.Open, FileAccess.Read, FileShare.Read);
newImageStream = new FileStream(o as string, FileMode.Open, FileAccess.Read);
else
newImageStream = o as Stream;
// Get all image parts in word\document.xml
List<PackagePart> imageParts = mainPart.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image").Select(ir => package.GetParts().Where(p => p.Uri.ToString().EndsWith(ir.TargetUri.ToString())).First()).ToList();
PackageRelationshipCollection relationshipsByImages = mainPart.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
List<PackagePart> imageParts = relationshipsByImages.Select(ir => package.GetParts().FirstOrDefault(p => p.Uri.ToString().EndsWith(ir.TargetUri.ToString()))).Where(e => e != null).ToList();
foreach (PackagePart relsPart in package.GetParts().Where(part => part.Uri.ToString().Contains("/word/")).Where(part => part.ContentType.Equals("application/vnd.openxmlformats-package.relationships+xml")))
{
XDocument relsPartContent;
@@ -3056,15 +3059,26 @@ namespace Novacode
{
if (imageRelationship.Attribute(XName.Get("Target")) != null)
{
string imagePartUri = Path.Combine(Path.GetDirectoryName(relsPart.Uri.ToString()), imageRelationship.Attribute(XName.Get("Target")).Value);
imagePartUri = Path.GetFullPath(imagePartUri.Replace("\\_rels", string.Empty));
imagePartUri = imagePartUri.Replace(Path.GetFullPath("\\"), string.Empty).Replace("\\", "/");
string targetMode = string.Empty;
XAttribute targetModeAttibute = imageRelationship.Attribute(XName.Get("TargetMode"));
if (targetModeAttibute != null)
{
targetMode = targetModeAttibute.Value;
}
if (!targetMode.Equals("External"))
{
string imagePartUri = Path.Combine(Path.GetDirectoryName(relsPart.Uri.ToString()), imageRelationship.Attribute(XName.Get("Target")).Value);
imagePartUri = Path.GetFullPath(imagePartUri.Replace("\\_rels", string.Empty));
imagePartUri = imagePartUri.Replace(Path.GetFullPath("\\"), string.Empty).Replace("\\", "/");
if (!imagePartUri.StartsWith("/"))
imagePartUri = "/" + imagePartUri;
if (!imagePartUri.StartsWith("/"))
imagePartUri = "/" + imagePartUri;
PackagePart imagePart = package.GetPart(new Uri(imagePartUri, UriKind.Relative));
imageParts.Add(imagePart);
PackagePart imagePart = package.GetPart(new Uri(imagePartUri, UriKind.Relative));
imageParts.Add(imagePart);
}
}
}
}

Laden…
Annuleren
Opslaan