Kaynağa Gözat

Applied patch 7398 submitted by lckuiper on November 16th 2010 @

2:23 PM.

This patch adds the property VerticalAlignment to the Cell class.

Thank you for contributing this patch lckuiper.
master
coffeycathal_cp 15 yıl önce
ebeveyn
işleme
36ddc691bd
4 değiştirilmiş dosya ile 175 ekleme ve 48 silme
  1. 56
    48
      DocX/DocX.cs
  2. 3
    0
      DocX/Properties/AssemblyInfo.cs
  3. 113
    0
      DocX/Table.cs
  4. 3
    0
      DocX/_Enumerations.cs

+ 56
- 48
DocX/DocX.cs Dosyayı Görüntüle

@@ -1436,7 +1436,22 @@ namespace Novacode
/// <seealso cref="Paragraph.InsertPicture"/>
public Image AddImage(string filename)
{
return AddImage(filename as object);
string contentType = "";
// The extension this file has will be taken to be its format.
switch (Path.GetExtension(filename))
{
case ".tiff": contentType = "image/tif"; break;
case ".tif": contentType = "image/tif"; break;
case ".png": contentType = "image/png"; break;
case ".bmp": contentType = "image/png"; break;
case ".gif": contentType = "image/gif"; break;
case ".jpg": contentType = "image/jpg"; break;
case ".jpeg": contentType = "image/jpeg"; break;
default: contentType = "image/jpg"; break;
}
return AddImage(filename as object, contentType);
}
/// <summary>
@@ -1783,7 +1798,7 @@ namespace Novacode
}
}
internal Image AddImage(object o)
internal Image AddImage(object o, string contentType = "image/jpeg")
{
// Open a Stream to the new image being added.
Stream newImageStream;
@@ -1795,33 +1810,34 @@ namespace Novacode
// 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();
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;
using (TextReader tr = new StreamReader(relsPart.GetStream(FileMode.Open, FileAccess.Read)))
{
relsPartContent = XDocument.Load(tr);
}
IEnumerable<XElement> imageRelationships =
relsPartContent.Root.Elements().Where(
imageRel =>
imageRel.Attribute(XName.Get("Type")).Value.Equals(
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"));
foreach (XElement imageRelationship in imageRelationships)
{
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("\\", "/");
if (!imagePartUri.StartsWith("/"))
XDocument relsPartContent;
using (TextReader tr = new StreamReader(relsPart.GetStream(FileMode.Open, FileAccess.Read)))
relsPartContent = XDocument.Load(tr);
IEnumerable<XElement> imageRelationships =
relsPartContent.Root.Elements().Where
(
imageRel =>
imageRel.Attribute(XName.Get("Type")).Value.Equals("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image")
);
foreach (XElement imageRelationship in imageRelationships)
{
imagePartUri = "/" + imagePartUri;
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("\\", "/");
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);
}
}
}
// Loop through each image part in this document.
foreach (PackagePart pp in imageParts)
@@ -1843,30 +1859,22 @@ namespace Novacode
}
}
/*
* This Image being added is infact a new Image,
* we need to generate a unique name for this image of the format imageN.ext,
* where n is an integer that has not been used before.
* This could probabily be replace by a Guid.
*/
int max = 0;
var values =
(
from ip in imageParts
let Name = Path.GetFileNameWithoutExtension(ip.Uri.ToString())
let Number = Regex.Match(Name, @"\d+$").Value
select Number != string.Empty ? int.Parse(Number) : 0
);
if (values.Count() > 0)
max = Math.Max(max, values.Max());
string imgPartUriPath = string.Empty;
string extension = contentType.Substring(contentType.LastIndexOf("/") + 1);
do
{
// Create a new image part.
imgPartUriPath = string.Format
(
"/word/media/{0}.{1}",
Guid.NewGuid().ToString(), // The unique part.
extension
);
// Create a new image part.
string imgPartUriPath = string.Format("/word/media/image{0}.jpeg", max + 1);
if (package.PartExists(new Uri(imgPartUriPath, UriKind.Relative)))
{
package.DeletePart(new Uri(imgPartUriPath, UriKind.Relative));
}
PackagePart img = package.CreatePart(new Uri(imgPartUriPath, UriKind.Relative), System.Net.Mime.MediaTypeNames.Image.Jpeg);
} while (package.PartExists(new Uri(imgPartUriPath, UriKind.Relative)));
// We are now guareenteed that imgPartUriPath is unique.
PackagePart img = package.CreatePart(new Uri(imgPartUriPath, UriKind.Relative), contentType);
// Create a new image relationship
PackageRelationship rel = mainPart.CreateRelationship(img.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");

+ 3
- 0
DocX/Properties/AssemblyInfo.cs Dosyayı Görüntüle

@@ -14,6 +14,9 @@ using System.Runtime.InteropServices;
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Allow the UnitTests to get at internal stuff.
[assembly: InternalsVisibleTo("UnitTests")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.

+ 113
- 0
DocX/Table.cs Dosyayı Görüntüle

@@ -1504,6 +1504,119 @@ namespace Novacode
}
}
/// <summary>
/// Gets or Sets this Cells vertical alignment.
/// </summary>
/// <!--Patch 7398 added by lckuiper on Nov 16th 2010 @ 2:23 PM-->
/// <example>
/// Creates a table with 3 cells and sets the vertical alignment of each to 1 of the 3 available options.
/// <code>
///// Create a new document.
///using(DocX document = DocX.Create("Test.docx"))
///{
/// // Insert a Table into this document.
/// Table t = document.InsertTable(3, 1);
///
/// // Set the design of the Table such that we can easily identify cell boundaries.
/// t.Design = TableDesign.TableGrid;
///
/// // Set the height of the row bigger than default.
/// // We need to be able to see the difference in vertical cell alignment options.
/// t.Rows[0].Height = 100;
///
/// // Set the vertical alignment of cell0 to top.
/// Cell c0 = t.Rows[0].Cells[0];
/// c0.InsertParagraph("VerticalAlignment.Top");
/// c0.VerticalAlignment = VerticalAlignment.Top;
///
/// // Set the vertical alignment of cell1 to center.
/// Cell c1 = t.Rows[0].Cells[1];
/// c1.InsertParagraph("VerticalAlignment.Center");
/// c1.VerticalAlignment = VerticalAlignment.Center;
///
/// // Set the vertical alignment of cell2 to bottom.
/// Cell c2 = t.Rows[0].Cells[2];
/// c2.InsertParagraph("VerticalAlignment.Bottom");
/// c2.VerticalAlignment = VerticalAlignment.Bottom;
///
/// // Save the document.
/// document.Save();
///}
/// </code>
/// </example>
public VerticalAlignment VerticalAlignment
{
get
{
/*
* Get the tcPr (table cell properties) element for this Cell,
* null will be return if no such element exists.
*/
XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
// If tcPr is null, this cell contains no width information.
if (tcPr == null)
return VerticalAlignment.Center;
/*
* Get the vAlign (table cell vertical alignment) element for this Cell,
* null will be return if no such element exists.
*/
XElement vAlign = tcPr.Element(XName.Get("vAlign", DocX.w.NamespaceName));
// If vAlign is null, this cell contains no vertical alignment information.
if (vAlign == null)
return VerticalAlignment.Center;
// Get the val attribute of the vAlign element.
XAttribute val = vAlign.Attribute(XName.Get("val", DocX.w.NamespaceName));
// If val is null, this cell contains no vAlign information.
if (val == null)
return VerticalAlignment.Center;
// If val is not a VerticalAlign enum, something is wrong with this attributes value, so remove it and return VerticalAlignment.Center;
try
{
return (VerticalAlignment)Enum.Parse(typeof(VerticalAlignment), val.Value, true);
}
catch
{
val.Remove();
return VerticalAlignment.Center;
}
}
set
{
/*
* Get the tcPr (table cell properties) element for this Cell,
* null will be return if no such element exists.
*/
XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
if (tcPr == null)
{
Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
}
/*
* Get the vAlign (table cell vertical alignment) element for this Cell,
* null will be return if no such element exists.
*/
XElement vAlign = tcPr.Element(XName.Get("vAlign", DocX.w.NamespaceName));
if (vAlign == null)
{
tcPr.SetElementValue(XName.Get("vAlign", DocX.w.NamespaceName), string.Empty);
vAlign = tcPr.Element(XName.Get("vAlign", DocX.w.NamespaceName));
}
// Set the VerticalAlignment in 'val'
vAlign.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), value.ToString().ToLower());
}
}
public Color Shading
{
get

+ 3
- 0
DocX/_Enumerations.cs Dosyayı Görüntüle

@@ -5,6 +5,9 @@ using System.Text;
namespace Novacode
{
// Patch 7398 added by lckuiper on Nov 16th 2010 @ 2:23 PM
public enum VerticalAlignment { Top, Center, Bottom };
public enum Orientation { Portrait, Landscape };
public enum XmlDocument { Main, HeaderOdd, HeaderEven, HeaderFirst, FooterOdd, FooterEven, FooterFirst };
public enum MatchFormattingOptions { ExactMatch, SubsetMatch};

Loading…
İptal
Kaydet