Skip to content

Commit e2d90ca

Browse files
authored
Remove OpenXmlPackage.Close and all references (#1373)
* Remove OpenXmlPackage.Close and all references
1 parent b6650ef commit e2d90ca

File tree

8 files changed

+29
-40
lines changed

8 files changed

+29
-40
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
3030
- Removed obsolete validation logic from v1 of the SDK
3131
- Removed obsoleted methods from 2.x
3232
- Removed mutable properties on OpenXmlAttribute and marked as `readonly`
33+
- Removed `OpenXmlPackage.Close` in favor of Dispose (#1373)
3334

3435
### Fixed
3536
- Fixed incorrect file extensions for vbaProject files (#1292)

samples/PowerPointModernCommentSample/Program.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void Main(string[] args)
3838
}
3939

4040
// Create a presentation
41-
PresentationDocument presentationDocument = PowerPointUtils.CreatePresentation(fileInfo.FullName);
41+
using PresentationDocument presentationDocument = PowerPointUtils.CreatePresentation(fileInfo.FullName);
4242

4343
// create missing PowerPointAuthorsPart if it is null
4444
if (presentationDocument?.PresentationPart?.authorsPart is null)
@@ -94,8 +94,6 @@ public static void Main(string[] args)
9494
new CommentRelationship()
9595
{ Id = slidePart.GetIdOfPart(powerPointCommentPart) })
9696
{ Uri = "{6950BFC3-D8DA-4A85-94F7-54DA5524770B}" }));
97-
98-
presentationDocument.Close();
9997
}
10098
catch (Exception ex)
10199
{

samples/SunburstChartExample/Program.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@ private static void Main(string[] args)
2626
public static void CreatePresentation(string filepath)
2727
{
2828
// Create a presentation at a specified file path. The presentation document type is pptx, by default.
29-
var presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation);
29+
using PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation);
3030
var presentationPart = presentationDoc.AddPresentationPart();
3131
presentationPart.Presentation = new Presentation();
3232

3333
CreatePresentationParts(presentationPart);
34-
35-
// Close the presentation handle
36-
presentationDoc.Close();
3734
}
3835

3936
private static void CreatePresentationParts(PresentationPart presentationPart)

samples/ThreadedCommentExample/Program.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private static void Main(string[] args)
5151
}
5252

5353
// WORKBOOK
54-
SpreadsheetDocument? sd = ExampleUtilities.CreateSpreadsheetWorkbook(filePath, sheetName);
54+
using SpreadsheetDocument? sd = ExampleUtilities.CreateSpreadsheetWorkbook(filePath, sheetName);
5555

5656
if (sd != null)
5757
{
@@ -105,9 +105,6 @@ private static void Main(string[] args)
105105
// ThreadedComment attributes
106106
{ Ref = reference, PersonId = idUser, Id = tcId, DT = System.DateTime.Now });
107107
}
108-
109-
// Close the document.
110-
sd.Close();
111108
}
112109
else
113110
{

src/DocumentFormat.OpenXml.Framework/Packaging/OpenXmlPackage.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,6 @@ public void DeletePartsRecursivelyOfType<T>()
143143
DeletePartsRecursivelyOfTypeBase<T>();
144144
}
145145

146-
/// <summary>
147-
/// Saves and closes the OpenXml package and all underlying part streams.
148-
/// </summary>
149-
public void Close()
150-
{
151-
ThrowIfObjectDisposed();
152-
Dispose();
153-
}
154-
155146
#region methods to operate DataPart
156147

157148
/// <summary>

test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void CanCreatePresentationFromTemplate()
2020
var part = packageDocument.PresentationPart;
2121
var root = part.Presentation;
2222

23-
packageDocument.SaveAs(Path.GetTempFileName()).Close();
23+
_ = packageDocument.SaveAs(Path.GetTempFileName());
2424

2525
// We are fine if we have not run into an exception.
2626
Assert.True(true);
@@ -36,7 +36,7 @@ public void CanCreateSpreadsheetFromTemplate()
3636
var part = packageDocument.WorkbookPart;
3737
var root = part.Workbook;
3838

39-
packageDocument.SaveAs(Path.GetTempFileName()).Close();
39+
_ = packageDocument.SaveAs(Path.GetTempFileName());
4040

4141
// We are fine if we have not run into an exception.
4242
Assert.True(true);
@@ -52,7 +52,7 @@ public void CanCreateWordprocessingDocumentFromTemplate()
5252
var part = packageDocument.MainDocumentPart;
5353
var root = part.Document;
5454

55-
packageDocument.SaveAs(Path.GetTempFileName()).Close();
55+
_ = packageDocument.SaveAs(Path.GetTempFileName());
5656

5757
// We are fine if we have not run into an exception.
5858
Assert.True(true);

test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ public void CanCloneDocument()
3333
{
3434
var body = clone.MainDocumentPart.Document.Body;
3535
body.InsertBefore(new Paragraph(new Run(new Text("Hello World"))), body.FirstChild);
36-
clone.SaveAs(file.Path).Close();
36+
using (var temp = clone.SaveAs(file.Path))
37+
{
38+
// clean up the temp package.
39+
}
3740

3841
using (var dest = WordprocessingDocument.Open(file.Path, false))
3942
{
@@ -105,18 +108,20 @@ public void CanDoMultithreadedMultipleCloning()
105108
}
106109

107110
// Clone clone1 again.
108-
var clone3 = (WordprocessingDocument)clone1.Clone();
109-
var body3 = clone3.MainDocumentPart.Document.Body;
110-
body3.GetFirstChild<Paragraph>()
111-
.InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 3"))));
112-
clone3.Close();
111+
using (WordprocessingDocument clone3 = (WordprocessingDocument)clone1.Clone())
112+
{
113+
var body3 = clone3.MainDocumentPart.Document.Body;
114+
body3.GetFirstChild<Paragraph>()
115+
.InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 3"))));
116+
}
113117

114118
// Clone source again.
115-
var clone4 = (WordprocessingDocument)source.Clone();
116-
var body4 = clone4.MainDocumentPart.Document.Body;
117-
body4.GetFirstChild<Paragraph>()
118-
.InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 4"))));
119-
clone4.Close();
119+
using (WordprocessingDocument clone4 = (WordprocessingDocument)source.Clone())
120+
{
121+
var body4 = clone4.MainDocumentPart.Document.Body;
122+
body4.GetFirstChild<Paragraph>()
123+
.InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 4"))));
124+
}
120125
}
121126
});
122127
}
@@ -181,7 +186,9 @@ public void CanWildlyCloneAndFlush()
181186

182187
using (var tempFile = TemporaryFile.Create())
183188
{
184-
clone.SaveAs(tempFile.Path).Close();
189+
using (clone.SaveAs(tempFile.Path))
190+
{
191+
}
185192
}
186193
}
187194
});
@@ -197,7 +204,7 @@ public void CanDoPackageBasedCloningWord()
197204
{
198205
using (var package = Package.Open(ms, FileMode.Create))
199206
{
200-
source.Clone(package).Close();
207+
_ = source.Clone(package);
201208
}
202209

203210
ms.Position = 0;
@@ -218,7 +225,7 @@ public void CanDoPackageBasedCloningSpreadsheet()
218225
{
219226
using (var package = Package.Open(ms, FileMode.Create))
220227
{
221-
source.Clone(package).Close();
228+
_ = source.Clone(package);
222229
}
223230

224231
ms.Position = 0;
@@ -239,7 +246,7 @@ public void CanDoPackageBasedCloningPowerpoint()
239246
{
240247
using (var package = Package.Open(ms, FileMode.Create))
241248
{
242-
source.Clone(package).Close();
249+
_ = source.Clone(package);
243250
}
244251

245252
ms.Position = 0;

test/DocumentFormat.OpenXml.Tests/ofapiTest/OpenXmlValidatorTest.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3079,8 +3079,6 @@ public void PackageStuctureValidatingTest()
30793079
commentsPart.Comments.SaveToPart(commentsPart2);
30803080

30813081
mainPart.Relationships.Create(mainPart.Uri, System.IO.Packaging.TargetMode.Internal, mainPart.RelationshipType);
3082-
3083-
wordDoc.Close();
30843082
}
30853083

30863084
stream.Flush();

0 commit comments

Comments
 (0)