Quellcode durchsuchen

Fixed:

- AddImage does not work when document has remote images.

patch provided by sdpfigueiredo
master
MadBoy_cp vor 11 Jahren
Ursprung
Commit
dcc091a6a7
1 geänderte Dateien mit 23 neuen und 9 gelöschten Zeilen
  1. 23
    9
      DocX/DocX.cs

+ 23
- 9
DocX/DocX.cs Datei anzeigen

// Open a Stream to the new image being added. // Open a Stream to the new image being added.
Stream newImageStream; Stream newImageStream;
if (o is string) 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 else
newImageStream = o as Stream; newImageStream = o as Stream;
// Get all image parts in word\document.xml // 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"))) 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; XDocument relsPartContent;
{ {
if (imageRelationship.Attribute(XName.Get("Target")) != null) 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…
Abbrechen
Speichern