Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ooxml/XWPF/Usermodel/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ public enum PictureType
* WordPerfect graphics (.wpg)
*/
WPG = 12,

/** Microsoft Windows Media Photo image (.wdp) */
WDP=13,
/**
* Scalable Vector Graphics (.svg)
*/
SVG = 13
SVG = 14
}
public interface Document
{
Expand Down
5 changes: 5 additions & 0 deletions ooxml/XWPF/Usermodel/XWPFParagraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,11 @@ public bool IsAlignmentSet()
var pr = GetCTPPr(false);
return pr != null && pr.IsSetJc();
}

public bool RunsIsEmpty()
{
return runs.Count==0;
}
}

}
3 changes: 2 additions & 1 deletion ooxml/XWPF/Usermodel/XWPFPictureData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class XWPFPictureData : POIXMLDocumentPart
internal static POIXMLRelation[] RELATIONS;
static XWPFPictureData()
{
RELATIONS = new POIXMLRelation[14];
RELATIONS = new POIXMLRelation[15];
RELATIONS[(int)PictureType.EMF] = XWPFRelation.IMAGE_EMF;
RELATIONS[(int)PictureType.WMF] = XWPFRelation.IMAGE_WMF;
RELATIONS[(int)PictureType.PICT] = XWPFRelation.IMAGE_PICT;
Expand All @@ -49,6 +49,7 @@ static XWPFPictureData()
RELATIONS[(int)PictureType.EPS] = XWPFRelation.IMAGE_EPS;
RELATIONS[(int)PictureType.BMP] = XWPFRelation.IMAGE_BMP;
RELATIONS[(int)PictureType.WPG] = XWPFRelation.IMAGE_WPG;
RELATIONS[(int) PictureType.WDP] = XWPFRelation.HDPHOTO_WDP;
RELATIONS[(int)PictureType.SVG] = XWPFRelation.IMAGE_SVG;
}

Expand Down
7 changes: 7 additions & 0 deletions ooxml/XWPF/Usermodel/XWPFRelation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ public class XWPFRelation : POIXMLRelation
"/word/media/image#.wpg",
typeof(XWPFPictureData)
);
public static XWPFRelation HDPHOTO_WDP = new XWPFRelation(
"image/vnd.ms-photo",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
"/ppt/media/hdphoto#.wdp",
typeof(XWPFPictureData)
);

public static XWPFRelation IMAGE_SVG = new XWPFRelation(
"image/svg",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
Expand Down
15 changes: 12 additions & 3 deletions ooxml/XWPF/Usermodel/XWPFTableCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,18 @@ private static CT_Border CreateBorder(XWPFTable.XWPFBorderType type, int size, i

public void SetText(String text)
{
CT_P ctP = (ctTc.SizeOfPArray() == 0) ? ctTc.AddNewP() : ctTc.GetPArray(0);
XWPFParagraph par = new XWPFParagraph(ctP, this);
par.CreateRun().AppendText(text);
XWPFParagraph par = paragraphs.Count==0 ? AddParagraph() : paragraphs[0];
while(!par.RunsIsEmpty())
{
par.RemoveRun(0);
}
par.CreateRun().SetText(text);
}

public void AppendText(String text)
{
XWPFParagraph par = paragraphs.Count==0 ? AddParagraph() : paragraphs[0];
par.CreateRun().SetText(text);
}

public XWPFTableRow GetTableRow()
Expand Down
85 changes: 49 additions & 36 deletions testcases/ooxml/XWPF/TestXWPFBugs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ namespace TestCases.XWPF
using NPOI.Util;
using NPOI.XWPF;
using NPOI.XWPF.UserModel;
using NUnit.Framework;using NUnit.Framework.Legacy;
using NUnit.Framework; using NUnit.Framework.Legacy;
using System;
using System.IO;
using System.Reflection.Metadata;
using System.Xml;
using System.Xml.Linq;
using TestCases;

[TestFixture]
Expand All @@ -46,7 +47,7 @@ public void Bug55802()
XWPFDocument doc = new XWPFDocument();
XWPFRun run = doc.CreateParagraph().CreateRun();

foreach (String str in blabla.Split("\n".ToCharArray()))
foreach(String str in blabla.Split("\n".ToCharArray()))
{
run.SetText(str);
run.AddBreak();
Expand Down Expand Up @@ -76,27 +77,27 @@ public void Test53475()
Biff8EncryptionKey.CurrentUserPassword = (/*setter*/"solrcell");
FileStream file = POIDataSamples.GetDocumentInstance().GetFile("bug53475-password-is-solrcell.docx");
NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file,null, true, true);
/*
// Check the encryption details
EncryptionInfo info = new EncryptionInfo(filesystem);
ClassicAssert.AreEqual(128, info.Header.KeySize);
ClassicAssert.AreEqual(EncryptionHeader.ALGORITHM_AES_128, info.Header.Algorithm);
ClassicAssert.AreEqual(EncryptionHeader.HASH_SHA1, info.Header.HashAlgorithm);

// Check it can be decoded
Decryptor d = Decryptor.GetInstance(info);
ClassicAssert.IsTrue("Unable to Process: document is encrypted", d.VerifyPassword("solrcell"));

// Check we can read the word document in that
InputStream dataStream = d.GetDataStream(filesystem);
OPCPackage opc = OPCPackage.Open(dataStream);
XWPFDocument doc = new XWPFDocument(opc);
XWPFWordExtractor ex = new XWPFWordExtractor(doc);
String text = ex.Text;
ClassicAssert.IsNotNull(text);
ClassicAssert.AreEqual("This is password protected Word document.", text.Trim());
ex.Close();
*/
/*
// Check the encryption details
EncryptionInfo info = new EncryptionInfo(filesystem);
ClassicAssert.AreEqual(128, info.Header.KeySize);
ClassicAssert.AreEqual(EncryptionHeader.ALGORITHM_AES_128, info.Header.Algorithm);
ClassicAssert.AreEqual(EncryptionHeader.HASH_SHA1, info.Header.HashAlgorithm);

// Check it can be decoded
Decryptor d = Decryptor.GetInstance(info);
ClassicAssert.IsTrue("Unable to Process: document is encrypted", d.VerifyPassword("solrcell"));

// Check we can read the word document in that
InputStream dataStream = d.GetDataStream(filesystem);
OPCPackage opc = OPCPackage.Open(dataStream);
XWPFDocument doc = new XWPFDocument(opc);
XWPFWordExtractor ex = new XWPFWordExtractor(doc);
String text = ex.Text;
ClassicAssert.IsNotNull(text);
ClassicAssert.AreEqual("This is password protected Word document.", text.Trim());
ex.Close();
*/
filesystem.Close();
}
finally
Expand All @@ -109,7 +110,7 @@ public void Bug57495_getTableArrayInDoc()
{
XWPFDocument doc = new XWPFDocument();
//let's create a few tables for the test
for (int i = 0; i < 3; i++)
for(int i = 0; i < 3; i++)
{
doc.CreateTable(2, 2);
}
Expand Down Expand Up @@ -147,18 +148,18 @@ public void Bug57312_NullPointException()
XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("57312.docx");
ClassicAssert.IsNotNull(doc);

foreach (IBodyElement bodyElement in doc.BodyElements)
foreach(IBodyElement bodyElement in doc.BodyElements)
{
BodyElementType elementType = bodyElement.ElementType;

if (elementType == BodyElementType.PARAGRAPH)
if(elementType == BodyElementType.PARAGRAPH)
{
XWPFParagraph paragraph = (XWPFParagraph)bodyElement;

foreach (IRunElement iRunElem in paragraph.IRuns)
foreach(IRunElement iRunElem in paragraph.IRuns)
{

if (iRunElem is XWPFRun)
if(iRunElem is XWPFRun)
{
XWPFRun RunElement = (XWPFRun)iRunElem;

Expand Down Expand Up @@ -189,7 +190,7 @@ public void Test57829()
ClassicAssert.IsNotNull(doc);
ClassicAssert.AreEqual(3, doc.Paragraphs.Count);

foreach (XWPFParagraph paragraph in doc.Paragraphs)
foreach(XWPFParagraph paragraph in doc.Paragraphs)
{
paragraph.RemoveRun(0);
ClassicAssert.IsNotNull(paragraph.Text);
Expand All @@ -215,7 +216,7 @@ public void Test58618()
public void Bug59058()
{
String[] files = { "bug57031.docx", "bug59058.docx" };
foreach (String f in files)
foreach(String f in files)
{
ZipFile zf = new ZipFile(POIDataSamples.GetDocumentInstance().GetFile(f));
ZipEntry entry = zf.GetEntry("word/document.xml");
Expand All @@ -240,27 +241,27 @@ public void Test59378()

[Test]
public void Test63788() {
using (XWPFDocument doc = new XWPFDocument())
using(XWPFDocument doc = new XWPFDocument())
{

XWPFNumbering numbering = doc.CreateNumbering();

for (int i = 10; i >= 0; i--) {
for(int i = 10; i >= 0; i--) {
addNumberingWithAbstractId(numbering, i); //add numbers in reverse order
}

for (int i = 0; i <= 10; i++) {
for(int i = 0; i <= 10; i++) {
ClassicAssert.AreEqual(i, int.Parse(numbering.GetAbstractNum(i.ToString()).GetAbstractNum().abstractNumId));
}

//attempt to remove item with numId 2
ClassicAssert.IsTrue(numbering.RemoveAbstractNum("2"));

for (int i = 0; i <= 10; i++) {
for(int i = 0; i <= 10; i++) {
XWPFAbstractNum abstractNum = numbering.GetAbstractNum(i.ToString());

// we removed id "2", so this one should be empty, all others not
if (i == 2) {
if(i == 2) {
ClassicAssert.IsNull(abstractNum, "Failed for " + i);
} else {
ClassicAssert.IsNotNull(abstractNum, "Failed for " + i);
Expand Down Expand Up @@ -292,7 +293,7 @@ private static void addNumberingWithAbstractId(XWPFNumbering documentNumbering,
[Test]
public void CorrectParagraphAlignment()
{
using (var document = XWPFTestDataSamples.OpenSampleDocument("bug-paragraph-alignment.docx")) {
using(var document = XWPFTestDataSamples.OpenSampleDocument("bug-paragraph-alignment.docx")) {
XWPFParagraph centeredParagraph = document.GetParagraphArray(0);
ClassicAssert.IsFalse(centeredParagraph.IsAlignmentSet());
ClassicAssert.AreEqual(ParagraphAlignment.LEFT, centeredParagraph.Alignment); // LEFT is a fallback value here.
Expand All @@ -301,7 +302,19 @@ public void CorrectParagraphAlignment()
ClassicAssert.IsTrue(leftParagraph.IsAlignmentSet());
ClassicAssert.AreEqual(ParagraphAlignment.LEFT, leftParagraph.Alignment); // LEFT is the real alignment value.
}
}
[Test]
public void Bug66988()
{
using(var document = XWPFTestDataSamples.OpenSampleDocument("Bug66988.docx"))
{
XWPFTableCell cell = document.GetTableArray(0).GetRow(0).GetCell(0);
cell.AppendText("World");
ClassicAssert.AreEqual("HelloWorld", cell.GetText());
cell.SetText("FooBar");
ClassicAssert.AreEqual("FooBar", cell.GetText());
}
}
}
}

5 changes: 3 additions & 2 deletions testcases/ooxml/XWPF/UserModel/TestXWPFDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,12 @@ public void TestAllPictureFormats()
doc.AddPictureData(new byte[18], (int) PictureType.EPS);
doc.AddPictureData(new byte[19], (int) PictureType.BMP);
doc.AddPictureData(new byte[20], (int) PictureType.WPG);
doc.AddPictureData(new byte[21], (int) PictureType.SVG);

ClassicAssert.AreEqual(11, doc.AllPictures.Count);
ClassicAssert.AreEqual(12, doc.AllPictures.Count);

doc = XWPFTestDataSamples.WriteOutAndReadBack(doc);
ClassicAssert.AreEqual(11, doc.AllPictures.Count);
ClassicAssert.AreEqual(12, doc.AllPictures.Count);

}
[Test]
Expand Down
Binary file added testcases/test-data/document/Bug66988.docx
Binary file not shown.
Loading