private static HWPFDocument Reload(HWPFDocument hwpfDocument) { MemoryStream baos = new MemoryStream(); hwpfDocument.Write(baos); return(new HWPFDocument(new MemoryStream(baos.ToArray()))); }
public void TestReadWrite() { // This document is widely available on the internet as "blair.doc". // I tried stripping the content and saving the document but my version // of Word (from Office XP) strips this table out. HWPFDocument doc = HWPFTestDataSamples.OpenSampleFile("saved-by-table.doc"); // Check what we just Read. for (int i = 0; i < expected.Count; i++) { Assert.AreEqual(expected[i], doc.GetSavedByTable().GetEntries()[i], "List of saved-by entries was not as expected"); } // Now write the entire document out, and read it back in... MemoryStream byteStream = new MemoryStream(); doc.Write(byteStream); Stream copyStream = new MemoryStream(byteStream.ToArray()); HWPFDocument copy = new HWPFDocument(copyStream); // And check again. for (int i = 0; i < expected.Count; i++) { Assert.AreEqual( expected[i], copy.GetSavedByTable().GetEntries()[i], "List of saved-by entries was incorrect after writing"); } }
/** * Writes a spreadsheet to a <tt>MemoryStream</tt> and Reads it back * from a <tt>MemoryStream</tt>.<p/> * Useful for verifying that the serialisation round trip */ public static HWPFDocument WriteOutAndReadBack(HWPFDocument original) { MemoryStream baos = new MemoryStream(4096); original.Write(baos); MemoryStream bais = new MemoryStream(baos.ToArray()); return(new HWPFDocument(bais)); }
protected HWPFDocument SaveAndReload(HWPFDocument doc) { MemoryStream baos = new MemoryStream(); doc.Write(baos); return(new HWPFDocument( new MemoryStream(baos.ToArray()) )); }
public HWPFDocument WriteOutAndRead(HWPFDocument doc) { MemoryStream baos = new MemoryStream(); HWPFDocument newDoc; doc.Write(baos); MemoryStream bais = new MemoryStream(baos.ToArray()); newDoc = new HWPFDocument(bais); return(newDoc); }
static void Main(string[] args) { // POI apparently can't create a document from scratch, // so we need an existing empty dummy document POIFSFileSystem fs = new POIFSFileSystem(File.OpenRead("empty.doc")); HWPFDocument doc = new HWPFDocument(fs); // centered paragraph with large font size Range range = doc.GetRange(); CharacterRun run1 = range.InsertAfter("one"); //par1.SetSpacingAfter(200); //par1.SetJustification((byte)1); // justification: 0=left, 1=center, 2=right, 3=left and right //CharacterRun run1 = par1.InsertAfter("one"); run1.SetFontSize(2 * 18); // font size: twice the point size // paragraph with bold typeface Paragraph par2 = run1.InsertAfter(new ParagraphProperties(), 0); par2.SetSpacingAfter(200); CharacterRun run2 = par2.InsertAfter("two two two two two two two two two two two two two"); run2.SetBold(true); // paragraph with italic typeface and a line indent in the first line Paragraph par3 = run2.InsertAfter(new ParagraphProperties(), 0); par3.SetFirstLineIndent(200); par3.SetSpacingAfter(200); CharacterRun run3 = par3.InsertAfter("three three three three three three three three three " + "three three three three three three three three three three three three three three " + "three three three three three three three three three three three three three three"); run3.SetItalic(true); // add a custom document property (needs POI 3.5; POI 3.2 doesn't save custom properties) DocumentSummaryInformation dsi = doc.DocumentSummaryInformation; CustomProperties cp = dsi.CustomProperties; if (cp == null) { cp = new CustomProperties(); } cp.Put("myProperty", "foo bar baz"); doc.Write(File.OpenWrite("new-hwpf-file.doc")); }
public void TestShapes1() { HWPFDocument doc = HWPFTestDataSamples.OpenSampleFile("WithArtShapes.doc"); IList shapes = doc.GetShapesTable().GetAllShapes(); IList vshapes = doc.GetShapesTable().GetVisibleShapes(); Assert.AreEqual(2, shapes.Count); Assert.AreEqual(2, vshapes.Count); Shape s1 = (Shape)shapes[0]; Shape s2 = (Shape)shapes[1]; Assert.AreEqual(3616, s1.Width); Assert.AreEqual(1738, s1.Height); Assert.AreEqual(true, s1.IsWithinDocument); Assert.AreEqual(4817, s2.Width); Assert.AreEqual(2164, s2.Height); Assert.AreEqual(true, s2.IsWithinDocument); // Re-serialisze, check still there MemoryStream baos = new MemoryStream(); doc.Write(baos); MemoryStream bais = new MemoryStream(baos.ToArray()); doc = new HWPFDocument(bais); shapes = doc.GetShapesTable().GetAllShapes(); vshapes = doc.GetShapesTable().GetVisibleShapes(); Assert.AreEqual(2, shapes.Count); Assert.AreEqual(2, vshapes.Count); s1 = (Shape)shapes[0]; s2 = (Shape)shapes[1]; Assert.AreEqual(3616, s1.Width); Assert.AreEqual(1738, s1.Height); Assert.AreEqual(true, s1.IsWithinDocument); Assert.AreEqual(4817, s2.Width); Assert.AreEqual(2164, s2.Height); Assert.AreEqual(true, s2.IsWithinDocument); }
public async Task <IActionResult> Word() { var path = @"I:\yim\Documents\工作交接-2021-05\OA中专路由.doc"; await using var fileStream = System.IO.File.OpenRead(path); var doc = new HWPFDocument(fileStream); doc.Text.AppendLine("新增内容").AppendLine(DateTimeOffset.Now.ToString("yyyy-MM-dd hh:mm:ss")); await using var memoryStream = new MemoryStream(); doc.Write(memoryStream); var fileData = memoryStream.ToArray(); return(Json(new ResponseResult <FileDownloadDto>() { Success = true, Data = new FileDownloadDto() { FileData = Convert.ToBase64String(fileData), FileName = fileStream.Name } })); }
private static HWPFDocument Reload(HWPFDocument hwpfDocument) { MemoryStream baos = new MemoryStream(); hwpfDocument.Write(baos); return new HWPFDocument(new MemoryStream(baos.ToArray())); }
protected HWPFDocument SaveAndReload(HWPFDocument doc) { MemoryStream baos = new MemoryStream(); doc.Write(baos); return new HWPFDocument( new MemoryStream(baos.ToArray()) ); }