Skip to content

Commit

Permalink
Merge pull request #104 from SyncfusionExamples/914451
Browse files Browse the repository at this point in the history
914451 Optimize Memory Usage by Adding 'Using' Statement When Saving PDF Documents
  • Loading branch information
chinnumuniyappan authored Oct 14, 2024
2 parents 892c206 + 87f31ed commit 274d8e8
Show file tree
Hide file tree
Showing 31 changed files with 198 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
PdfDocument document = htmlConverter.Convert(urlToConvert);

//Create file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);

//Save and close the PDF document
document.Save(fileStream);
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
PdfDocument document = htmlConverter.Convert("https://www.example.com");

//Create file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);

//Save and close the PDF document
document.Save(fileStream);
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
htmlConverter.ConverterSettings = settings;
//Convert URL to PDF.
PdfDocument document = htmlConverter.Convert("file:///D:/PDF-Examples/HTML%20to%20PDF/Blink/Accessible_PDF_In_HTML_to_PDF/.NET/Accessible%20PDF/Data/Input.html");
//Save and close the PDF document.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);
//Save and close the PDF document
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
PdfDocument document = htmlConverter.Convert("https://www.google.com");

//Create file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);

//Save and close the PDF document.
document.Save(fileStream);
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
//Convert URL to PDF document.
PdfDocument document = htmlConverter.Convert(Path.GetFullPath(@"Data/Input.html"));

//Create file stream to save the PDF document.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);

//Save and close the PDF document
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
PdfDocument document = htmlConverter.Convert("https://www.google.com");

//Create file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);

//Save and close the PDF document.
document.Save(fileStream);
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
PdfDocument document = htmlConverter.Convert("https://www.example.com");

//Create file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);

//Save and close the PDF document
document.Save(fileStream);
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
PdfDocument document = htmlConverter.ConvertPartialHtml(Path.GetFullPath(@"Data/Input.html"), "picture");

//Create file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite);

//Save and close the PDF document
document.Save(fileStream);
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
//Convert HTML string to PDF document.
PdfDocument document = htmlConverter.Convert(htmlText, baseUrl);

//Create the file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);

//Save and close the PDF document
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
PdfDocument document = htmlConverter.Convert("https://httpbin.org/headers");

//Create file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);

//Save and close the PDF document.
document.Save(fileStream);
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
//Convert URL to PDF document.
PdfDocument document = htmlConverter.Convert("https://www.google.com");

//Create file stream for PDF document.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite);

//Save and close the PDF document
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
//Convert URL to PDF document.
PdfDocument document = htmlConverter.Convert("https://www.example.com");

//Create file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);

//Save and close the PDF document
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
//Convert HTML to PDF document.
PdfDocument document = htmlConverter.Convert(Path.GetFullPath(@"Data/Input.html"));

//Create file stream to save the PDF document.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);

//Save and close the PDF document.
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
//Convert HTML to PDF document.
PdfDocument document = htmlConverter.Convert(Path.GetFullPath(@"Data/Input.html"));

//Create file stream to save the PDF document.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);

//Save and close the PDF document
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
PdfDocument document = htmlConverter.Convert(Path.GetFullPath(@"Data/Input.html"));

//Create file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);

//Save and close the PDF document.
document.Save(fileStream);
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
//Convert the URL to a PDF document.
PdfDocument document = htmlConverter.Convert("<div>Hello World</div>",string.Empty);

//Create a filestream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite);
//Save and close a PDF document.
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ static void Main(string[] args) {
//Convert the URL to a PDF document.
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");

//Create a filestream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);
//Save and close a PDF document.
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ static void Main(string[] args) {
//Convert URL to PDF.
PdfDocument document = htmlConverter.Convert("https://www.google.com/");

//Create a file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);
//Save a PDF document to the file stream.
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ static void Main(string[] args) {
//Convert URL to PDF document.
PdfDocument document = htmlConverter.Convert("https://www.google.com");

//Create a file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);
//Save the PDF document to the file stream.
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ static void Main(string[] args) {
//Convert URL to PDF document.
PdfDocument document = htmlConverter.Convert("https://www.google.com");

//Create a file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);
//Save a PDF document to a file stream.
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ static void Main(string[] args) {
//Convert URL to PDF document.
PdfDocument document = htmlConverter.Convert("https://www.google.com");

//Create a file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite);
//Save the PDF document to the file stream.
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ static void Main(string[] args) {
//Convert URL to PDF document.
PdfDocument document = htmlConverter.Convert("https://www.google.com");

//Create a file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite);
//Save a PDF document to a file stream.
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
htmlConverter.ConverterSettings = blinkConverterSettings;
//Convert URL to PDF document.
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
//Create a file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite);
//Save a PDF document to a file stream.
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
//Convert the URL to PDF document.
PdfDocument document = htmlConverter.Convert("https://www.google.com");

//Create file stream.
FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
//Create file stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the PDF document
document.Save(fileStream);
}
//Close the document.
document.Close(true);
Loading

0 comments on commit 274d8e8

Please sign in to comment.