Skip to content

Commit c8bc19e

Browse files
committed
Adding the print command to .NET bindings
1 parent a2d9b94 commit c8bc19e

7 files changed

+516
-34
lines changed

dotnet/src/webdriver/EncodedFile.cs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// <copyright file="EncodedFile.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System;
20+
21+
namespace OpenQA.Selenium
22+
{
23+
/// <summary>
24+
/// Represents a file transmitted over the wire as a base64-encoded string.
25+
/// </summary>
26+
public abstract class EncodedFile
27+
{
28+
private string base64Encoded = string.Empty;
29+
private byte[] byteArray;
30+
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="EncodedFile"/> class.
33+
/// </summary>
34+
/// <param name="base64EncodedFile">The file as a Base64-encoded string.</param>
35+
protected EncodedFile(string base64EncodedFile)
36+
{
37+
this.base64Encoded = base64EncodedFile;
38+
this.byteArray = Convert.FromBase64String(this.base64Encoded);
39+
}
40+
41+
/// <summary>
42+
/// Gets the value of the encoded file as a Base64-encoded string.
43+
/// </summary>
44+
public string AsBase64EncodedString
45+
{
46+
get { return this.base64Encoded; }
47+
}
48+
49+
/// <summary>
50+
/// Gets the value of the encoded file as an array of bytes.
51+
/// </summary>
52+
public byte[] AsByteArray
53+
{
54+
get { return this.byteArray; }
55+
}
56+
57+
/// <summary>
58+
/// Saves the file, overwriting it if it already exists.
59+
/// </summary>
60+
/// <param name="fileName">The full path and file name to save the file to.</param>
61+
public abstract void SaveAsFile(string fileName);
62+
63+
/// <summary>
64+
/// Returns a <see cref="string">String</see> that represents the current <see cref="object">Object</see>.
65+
/// </summary>
66+
/// <returns>A <see cref="string">String</see> that represents the current <see cref="object">Object</see>.</returns>
67+
public override string ToString()
68+
{
69+
return this.base64Encoded;
70+
}
71+
}
72+
}

dotnet/src/webdriver/PrintDocument.cs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// <copyright file="PrintDocument.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System;
20+
using System.IO;
21+
22+
namespace OpenQA.Selenium
23+
{
24+
/// <summary>
25+
/// Represents a printed document in the form of a PDF document.
26+
/// </summary>
27+
public class PrintDocument : EncodedFile
28+
{
29+
/// <summary>
30+
/// Initializes a new instance of the <see cref="PrintDocument"/> class.
31+
/// </summary>
32+
/// <param name="base64EncodedDocument">The printed document as a Base64-encoded string.</param>
33+
public PrintDocument(string base64EncodedDocument) : base(base64EncodedDocument)
34+
{
35+
}
36+
37+
/// <summary>
38+
/// Saves this <see cref="PrintDocument"/> as a PDF formatted file, overwriting the file if it already exists.
39+
/// </summary>
40+
/// <param name="fileName">The full path and file name to save the printed document to.</param>
41+
public override void SaveAsFile(string fileName)
42+
{
43+
if (string.IsNullOrEmpty(fileName))
44+
{
45+
throw new ArgumentException("The file name to be saved cannot be null or the empty string", "fileName");
46+
}
47+
48+
using (MemoryStream imageStream = new MemoryStream(this.AsByteArray))
49+
{
50+
using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
51+
{
52+
imageStream.WriteTo(fileStream);
53+
}
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)