Преглед изворни кода

Added support for document orientation (portrait or landscape), below is a code example of how to use this new feature.

// Create a new document.
using (DocX document = DocX.Create(@"C:\Users\Cathal\Desktop\Hello.docx"))
{
    // Set this documents orientation to landscape and save.
    document.PageLayout.Orientation = Orientation.Landscape;
    document.SaveAs(@"Landscape.docx");

    // Set this documents orientation to portrait and save.
    document.PageLayout.Orientation = Orientation.Portrait;
    document.SaveAs(@"Portrait.docx");
}
master
coffeycathal_cp пре 15 година
родитељ
комит
701de20428
5 измењених фајлова са 162 додато и 2 уклоњено
  1. 1
    1
      ConsoleApplication1/Properties/AssemblyInfo.cs
  2. 15
    0
      DocX/DocX.cs
  3. 1
    0
      DocX/DocX.csproj
  4. 76
    0
      DocX/PageLayout.cs
  5. 69
    1
      UnitTests/UnitTest1.cs

+ 1
- 1
ConsoleApplication1/Properties/AssemblyInfo.cs Прегледај датотеку

@@ -20,7 +20,7 @@ using System.Runtime.InteropServices;
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("4fbe30d8-8d81-4dc4-adc2-58297c152631")]
[assembly: Guid("a3ae1da3-afb6-49e6-bf5f-a1cb7c4b690a")]
// Version information for an assembly consists of the following four values:
//

+ 15
- 0
DocX/DocX.cs Прегледај датотеку

@@ -25,6 +25,21 @@ namespace Novacode
static internal XNamespace customVTypesSchema = "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes";
#endregion
public PageLayout PageLayout
{
get
{
XElement sectPr = Xml.Element(XName.Get("sectPr", DocX.w.NamespaceName));
if (sectPr == null)
{
Xml.SetElementValue(XName.Get("sectPr", DocX.w.NamespaceName), string.Empty);
sectPr = Xml.Element(XName.Get("sectPr", DocX.w.NamespaceName));
}
return new PageLayout(this, sectPr);
}
}
/// <summary>
/// Returns a collection of Headers in this Document.
/// A document typically contains three Headers.

+ 1
- 0
DocX/DocX.csproj Прегледај датотеку

@@ -91,6 +91,7 @@
<Compile Include="Headers.cs" />
<Compile Include="HelperFunctions.cs" />
<Compile Include="Hyperlink.cs" />
<Compile Include="PageLayout.cs" />
<Compile Include="_BaseClasses.cs" />
<Compile Include="Table.cs" />
<Compile Include="_Enumerations.cs" />

+ 76
- 0
DocX/PageLayout.cs Прегледај датотеку

@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace Novacode
{
public class PageLayout: DocXElement
{
internal PageLayout(DocX document, XElement xml):base(document, xml)
{
}
public Orientation Orientation
{
get
{
/*
* Get the pgSz (page size) element for this Section,
* null will be return if no such element exists.
*/
XElement pgSz = Xml.Element(XName.Get("pgSz", DocX.w.NamespaceName));
if (pgSz == null)
return Orientation.Portrait;
// Get the attribute of the pgSz element.
XAttribute val = pgSz.Attribute(XName.Get("orient", DocX.w.NamespaceName));
// If val is null, this cell contains no information.
if (val == null)
return Orientation.Portrait;
if (val.Value.Equals("Landscape", StringComparison.CurrentCultureIgnoreCase))
return Orientation.Landscape;
else
return Orientation.Portrait;
}
set
{
// Check if already correct value.
if (Orientation == value)
return;
/*
* Get the pgSz (page size) element for this Section,
* null will be return if no such element exists.
*/
XElement pgSz = Xml.Element(XName.Get("pgSz", DocX.w.NamespaceName));
if (pgSz == null)
{
Xml.SetElementValue(XName.Get("pgSz", DocX.w.NamespaceName), string.Empty);
pgSz = Xml.Element(XName.Get("pgSz", DocX.w.NamespaceName));
}
pgSz.SetAttributeValue(XName.Get("orient", DocX.w.NamespaceName), value.ToString().ToLower());
if(value == Novacode.Orientation.Landscape)
{
pgSz.SetAttributeValue(XName.Get("w", DocX.w.NamespaceName), "16838");
pgSz.SetAttributeValue(XName.Get("h", DocX.w.NamespaceName), "11906");
}
else if (value == Novacode.Orientation.Portrait)
{
pgSz.SetAttributeValue(XName.Get("w", DocX.w.NamespaceName), "11906");
pgSz.SetAttributeValue(XName.Get("h", DocX.w.NamespaceName), "16838");
}
}
}
}
}

+ 69
- 1
UnitTests/UnitTest1.cs Прегледај датотеку

@@ -36,13 +36,81 @@ namespace UnitTests
directory_documents = String.Join("\\", steps) + "\\documents\\";
}
[TestMethod]
public void Test_Document_AddImage_FromDisk()
{
using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
{
// Add a png to into this document
Novacode.Image png = document.AddImage(directory_documents + "purple.png");
Assert.IsTrue(document.Images.Count == 1);
Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".png");
// Add a tiff into to this document
Novacode.Image tif = document.AddImage(directory_documents + "yellow.tif");
Assert.IsTrue(document.Images.Count == 2);
Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".tif");
// Add a gif into to this document
Novacode.Image gif = document.AddImage(directory_documents + "orange.gif");
Assert.IsTrue(document.Images.Count == 3);
Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".gif");
// Add a jpg into to this document
Novacode.Image jpg = document.AddImage(directory_documents + "green.jpg");
Assert.IsTrue(document.Images.Count == 4);
Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpg");
// Add a bitmap to this document
Novacode.Image bitmap = document.AddImage(directory_documents + "red.bmp");
Assert.IsTrue(document.Images.Count == 5);
// Word does not allow bmp make sure it was inserted as a png.
Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".png");
}
}
[TestMethod]
public void Test_Document_AddImage_FromStream()
{
using (DocX document = DocX.Create(directory_documents + "test_add_images.docx"))
{
// DocX will always insert Images that come from Streams as jpeg.
// Add a png to into this document
Novacode.Image png = document.AddImage(new FileStream(directory_documents + "purple.png", FileMode.Open));
Assert.IsTrue(document.Images.Count == 1);
Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".jpeg");
// Add a tiff into to this document
Novacode.Image tif = document.AddImage(new FileStream(directory_documents + "yellow.tif", FileMode.Open));
Assert.IsTrue(document.Images.Count == 2);
Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".jpeg");
// Add a gif into to this document
Novacode.Image gif = document.AddImage(new FileStream(directory_documents + "orange.gif", FileMode.Open));
Assert.IsTrue(document.Images.Count == 3);
Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".jpeg");
// Add a jpg into to this document
Novacode.Image jpg = document.AddImage(new FileStream(directory_documents + "green.jpg", FileMode.Open));
Assert.IsTrue(document.Images.Count == 4);
Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpeg");
// Add a bitmap to this document
Novacode.Image bitmap = document.AddImage(new FileStream(directory_documents + "red.bmp", FileMode.Open));
Assert.IsTrue(document.Images.Count == 5);
// Word does not allow bmp make sure it was inserted as a png.
Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".jpeg");
}
}
[TestMethod]
public void Test_Tables()
{
using (DocX document = DocX.Load(directory_documents + "Tables.docx"))
{
// There is only one Paragraph at the document level.
Assert.IsTrue(document.Paragraphs.Count() == 1);
Assert.IsTrue(document.Paragraphs.Count() == 13);
// There is only one Table in this document.
Assert.IsTrue(document.Tables.Count() == 1);

Loading…
Откажи
Сачувај