using System; using System.IO.Packaging; using System.IO; namespace Novacode { /// /// Represents an Image embedded in a document. /// public class Image { /// /// A unique id which identifies this Image. /// private string id; private DocX document; internal PackageRelationship pr; public Stream GetStream(FileMode mode, FileAccess access) { string temp = pr.SourceUri.OriginalString; string start = temp.Remove(temp.LastIndexOf('/')); string end = pr.TargetUri.OriginalString; string full = start + "/" + end; return(document.package.GetPart(new Uri(full, UriKind.Relative)).GetStream(mode, access)); } /// /// Returns the id of this Image. /// public string Id { get {return id;} } internal Image(DocX document, PackageRelationship pr) { this.document = document; this.pr = pr; this.id = pr.Id; } /// /// Add an image to a document, create a custom view of that image (picture) and then insert it into a Paragraph using append. /// /// /// /// Add an image to a document, create a custom view of that image (picture) and then insert it into a Paragraph using append. /// /// using (DocX document = DocX.Create("Test.docx")) /// { /// // Add an image to the document. /// Image i = document.AddImage(@"Image.jpg"); /// /// // Create a picture i.e. (A custom view of an image) /// Picture p = i.CreatePicture(); /// p.FlipHorizontal = true; /// p.Rotation = 10; /// /// // Create a new Paragraph. /// Paragraph par = document.InsertParagraph(); /// /// // Append content to the Paragraph. /// par.Append("Here is a cool picture") /// .AppendPicture(p) /// .Append(" don't you think so?"); /// /// // Save all changes made to this document. /// document.Save(); /// } /// /// public Picture CreatePicture() { return Paragraph.CreatePicture(document, id, string.Empty, string.Empty); } public Picture CreatePicture(int height, int width) { Picture picture = Paragraph.CreatePicture(document, id, string.Empty, string.Empty); picture.Height = height; picture.Width = width; return picture; } /// /// Returns the name of the image file. /// public string FileName { get { return Path.GetFileName(this.pr.TargetUri.ToString()); } } } }