소스 검색

Fixed issue with accessing "Pictures" property of "Paragraph" that has been removed from the document. Added method to replace an image inside a paragraph while keeping all image formatting.

master
Sven Erik Matzen 9 년 전
부모
커밋
24271df545
1개의 변경된 파일36개의 추가작업 그리고 0개의 파일을 삭제
  1. 36
    0
      DocX/Paragraph.cs

+ 36
- 0
DocX/Paragraph.cs 파일 보기

@@ -117,6 +117,11 @@ namespace Novacode
{
get
{
if (Xml == null)
{
return new List<Picture>();
}
List<Picture> pictures =
(
from p in Xml.Descendants()
@@ -1715,6 +1720,37 @@ namespace Novacode
return new Picture(document, xml, new Image(document, document.mainPart.GetRelationship(id)));
}
/// <summary>
/// Replaces a Picture with a new one.
/// </summary>
/// <remarks>
/// Only the content of the picture will be replaced - positioning inside the document and all other attributes will be preserved.
/// </remarks>
/// <param name="toBeReplaced">The picture object to be replaced.</param>
/// <param name="replaceWith">The picture object that should be inserted instead of <paramref name="toBeReplaced"/>.</param>
/// <returns>The new <see cref="Picture"/> object that replaces the old one.</returns>
public Picture ReplacePicture(Picture toBeReplaced, Picture replaceWith)
{
var document = this.Document;
var newDocPrId = document.GetNextFreeDocPrId();
var xml = XElement.Parse(toBeReplaced.Xml.ToString());
foreach (var element in xml.Descendants(XName.Get("docPr", DocX.wp.NamespaceName)))
{
element.SetAttributeValue(XName.Get("id"), newDocPrId);
}
foreach (var element in xml.Descendants(XName.Get("blip", DocX.a.NamespaceName)))
{
element.SetAttributeValue(XName.Get("embed", DocX.r.NamespaceName), replaceWith.Id);
}
var replacePicture = new Picture(document, xml, new Image(document, document.mainPart.GetRelationship(replaceWith.Id)));
this.AppendPicture(replacePicture);
toBeReplaced.Remove();
return replacePicture;
}
// Removed because it confusses the API.
//public Picture InsertPicture(int index, string imageID)
//{

Loading…
취소
저장