Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove OpenXmlPackage.Close and all references #1373

Merged
merged 4 commits into from
Apr 3, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Removed obsolete validation logic from v1 of the SDK
- Removed obsoleted methods from 2.x
- Removed mutable properties on OpenXmlAttribute and marked as `readonly`
- Removed `OpenXmlPackage.Close` in favor of Dispose (#1373)

### Fixed
- Fixed incorrect file extensions for vbaProject files (#1292)
Expand Down
4 changes: 1 addition & 3 deletions samples/PowerPointModernCommentSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void Main(string[] args)
}

// Create a presentation
PresentationDocument presentationDocument = PowerPointUtils.CreatePresentation(fileInfo.FullName);
using PresentationDocument presentationDocument = PowerPointUtils.CreatePresentation(fileInfo.FullName);

// create missing PowerPointAuthorsPart if it is null
if (presentationDocument?.PresentationPart?.authorsPart is null)
Expand Down Expand Up @@ -94,8 +94,6 @@ public static void Main(string[] args)
new CommentRelationship()
{ Id = slidePart.GetIdOfPart(powerPointCommentPart) })
{ Uri = "{6950BFC3-D8DA-4A85-94F7-54DA5524770B}" }));

presentationDocument.Close();
}
catch (Exception ex)
{
Expand Down
5 changes: 1 addition & 4 deletions samples/SunburstChartExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@ private static void Main(string[] args)
public static void CreatePresentation(string filepath)
{
// Create a presentation at a specified file path. The presentation document type is pptx, by default.
var presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation);
using PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation);
var presentationPart = presentationDoc.AddPresentationPart();
presentationPart.Presentation = new Presentation();

CreatePresentationParts(presentationPart);

// Close the presentation handle
presentationDoc.Close();
}

private static void CreatePresentationParts(PresentationPart presentationPart)
Expand Down
5 changes: 1 addition & 4 deletions samples/ThreadedCommentExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private static void Main(string[] args)
}

// WORKBOOK
SpreadsheetDocument? sd = ExampleUtilities.CreateSpreadsheetWorkbook(filePath, sheetName);
using SpreadsheetDocument? sd = ExampleUtilities.CreateSpreadsheetWorkbook(filePath, sheetName);

if (sd != null)
{
Expand Down Expand Up @@ -105,9 +105,6 @@ private static void Main(string[] args)
// ThreadedComment attributes
{ Ref = reference, PersonId = idUser, Id = tcId, DT = System.DateTime.Now });
}

// Close the document.
sd.Close();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,6 @@ public void DeletePartsRecursivelyOfType<T>()
DeletePartsRecursivelyOfTypeBase<T>();
}

/// <summary>
/// Saves and closes the OpenXml package and all underlying part streams.
/// </summary>
public void Close()
{
ThrowIfObjectDisposed();
Dispose();
}

#region methods to operate DataPart

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions test/DocumentFormat.OpenXml.Tests/CreateFromTemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void CanCreatePresentationFromTemplate()
var part = packageDocument.PresentationPart;
var root = part.Presentation;

packageDocument.SaveAs(Path.GetTempFileName()).Close();
_ = packageDocument.SaveAs(Path.GetTempFileName());

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

packageDocument.SaveAs(Path.GetTempFileName()).Close();
_ = packageDocument.SaveAs(Path.GetTempFileName());

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

packageDocument.SaveAs(Path.GetTempFileName()).Close();
_ = packageDocument.SaveAs(Path.GetTempFileName());

// We are fine if we have not run into an exception.
Assert.True(true);
Expand Down
37 changes: 22 additions & 15 deletions test/DocumentFormat.OpenXml.Tests/SaveAndCloneTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public void CanCloneDocument()
{
var body = clone.MainDocumentPart.Document.Body;
body.InsertBefore(new Paragraph(new Run(new Text("Hello World"))), body.FirstChild);
clone.SaveAs(file.Path).Close();
using (var temp = clone.SaveAs(file.Path))
{
// clean up the temp package.
}

using (var dest = WordprocessingDocument.Open(file.Path, false))
{
Expand Down Expand Up @@ -105,18 +108,20 @@ public void CanDoMultithreadedMultipleCloning()
}

// Clone clone1 again.
var clone3 = (WordprocessingDocument)clone1.Clone();
var body3 = clone3.MainDocumentPart.Document.Body;
body3.GetFirstChild<Paragraph>()
.InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 3"))));
clone3.Close();
using (WordprocessingDocument clone3 = (WordprocessingDocument)clone1.Clone())
{
var body3 = clone3.MainDocumentPart.Document.Body;
body3.GetFirstChild<Paragraph>()
.InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 3"))));
}

// Clone source again.
var clone4 = (WordprocessingDocument)source.Clone();
var body4 = clone4.MainDocumentPart.Document.Body;
body4.GetFirstChild<Paragraph>()
.InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 4"))));
clone4.Close();
using (WordprocessingDocument clone4 = (WordprocessingDocument)source.Clone())
{
var body4 = clone4.MainDocumentPart.Document.Body;
body4.GetFirstChild<Paragraph>()
.InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 4"))));
}
}
});
}
Expand Down Expand Up @@ -181,7 +186,9 @@ public void CanWildlyCloneAndFlush()

using (var tempFile = TemporaryFile.Create())
{
clone.SaveAs(tempFile.Path).Close();
using (clone.SaveAs(tempFile.Path))
{
}
}
}
});
Expand All @@ -197,7 +204,7 @@ public void CanDoPackageBasedCloningWord()
{
using (var package = Package.Open(ms, FileMode.Create))
{
source.Clone(package).Close();
_ = source.Clone(package);
}

ms.Position = 0;
Expand All @@ -218,7 +225,7 @@ public void CanDoPackageBasedCloningSpreadsheet()
{
using (var package = Package.Open(ms, FileMode.Create))
{
source.Clone(package).Close();
_ = source.Clone(package);
}

ms.Position = 0;
Expand All @@ -239,7 +246,7 @@ public void CanDoPackageBasedCloningPowerpoint()
{
using (var package = Package.Open(ms, FileMode.Create))
{
source.Clone(package).Close();
_ = source.Clone(package);
}

ms.Position = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3079,8 +3079,6 @@ public void PackageStuctureValidatingTest()
commentsPart.Comments.SaveToPart(commentsPart2);

mainPart.Relationships.Create(mainPart.Uri, System.IO.Packaging.TargetMode.Internal, mainPart.RelationshipType);

wordDoc.Close();
}

stream.Flush();
Expand Down