Skip to content

Commit b501ae7

Browse files
committed
v.1.2.2 changes
1 parent 6e084ce commit b501ae7

File tree

15 files changed

+120
-38
lines changed

15 files changed

+120
-38
lines changed

Ghostscript.NET.Samples/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ static void Main(string[] args)
4444
ISample sample;
4545

4646
//sample = new GetInkCoverageSample();
47-
sample = new ProcessorSample1();
47+
//sample = new ProcessorSample1();
4848
//sample = new ProcessorSample2();
4949
//sample = new FindInstalledGhostscriptVersionsSample();
5050
//sample = new RunMultipleInstancesSample();
5151
//sample = new ViewerSample();
52-
//sample = new RasterizerSample1();
52+
sample = new RasterizerSample1();
5353
//sample = new RasterizerSample2();
5454
//sample = new AddWatermarkSample();
5555
//sample = new DeviceUsageSample();

Ghostscript.NET.Samples/Samples/RasterizerCropSample.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// This file is part of Ghostscript.NET.Samples project
44
//
55
// Author: Josip Habjan (habjan@gmail.com, http://www.linkedin.com/in/habjan)
6-
// Copyright (c) 2013-2016 by Josip Habjan. All rights reserved.
6+
// Copyright (c) 2013-2021 by Josip Habjan. All rights reserved.
77
//
88
// Permission is hereby granted, free of charge, to any person obtaining
99
// a copy of this software and associated documentation files (the
@@ -45,8 +45,7 @@ public class RasterizerCropSample : ISample
4545
{
4646
public void Start()
4747
{
48-
int desired_x_dpi = 300;
49-
int desired_y_dpi = 300;
48+
int desired_dpi = 300;
5049

5150
string inputPdfPath = @"E:\__test_data\test2.pdf";
5251
string outputPath = @"E:\__test_data\output\";
@@ -64,7 +63,7 @@ public void Start()
6463
{
6564
string pageFilePath = Path.Combine(outputPath, "Page-" + pageNumber.ToString() + ".png");
6665

67-
Image img = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
66+
Image img = rasterizer.GetPage(desired_dpi, pageNumber);
6867
img.Save(pageFilePath, ImageFormat.Png);
6968

7069
Console.WriteLine(pageFilePath);

Ghostscript.NET.Samples/Samples/RasterizerSample1.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ public void Start()
5050

5151
public void Sample1()
5252
{
53-
int desired_x_dpi = 96;
54-
int desired_y_dpi = 96;
53+
int desired_dpi = 96;
5554

56-
string inputPdfPath = @"E:\gss_test\test.pdf";
55+
string inputPdfPath = @"E:\gss_test\Vin�cius\a.pdf";
5756
string outputPath = @"E:\gss_test\output\";
5857

5958
using (var rasterizer = new GhostscriptRasterizer())
@@ -64,7 +63,7 @@ public void Sample1()
6463
{
6564
var pageFilePath = Path.Combine(outputPath, string.Format("Page-{0}.png", pageNumber));
6665

67-
var img = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
66+
var img = rasterizer.GetPage(desired_dpi, pageNumber);
6867
img.Save(pageFilePath, ImageFormat.Png);
6968

7069
Console.WriteLine(pageFilePath);
@@ -74,8 +73,7 @@ public void Sample1()
7473

7574
public void Sample2()
7675
{
77-
int desired_x_dpi = 96;
78-
int desired_y_dpi = 96;
76+
int desired_dpi = 96;
7977

8078
string inputPdfPath = @"E:\gss_test\test.pdf";
8179
string outputPath = @"E:\gss_test\output\";
@@ -93,7 +91,7 @@ public void Sample2()
9391
{
9492
var pageFilePath = Path.Combine(outputPath, string.Format("Page-{0}.png", pageNumber));
9593

96-
var img = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
94+
var img = rasterizer.GetPage(desired_dpi, pageNumber);
9795
img.Save(pageFilePath, ImageFormat.Png);
9896

9997
Console.WriteLine(pageFilePath);

Ghostscript.NET.Samples/Samples/RasterizerSample2.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public class RasterizerSample2 : ISample
4545
{
4646
public void Start()
4747
{
48-
int desired_x_dpi = 96;
49-
int desired_y_dpi = 96;
48+
int desired_dpi = 96;
5049

5150
string inputPdfPath = @"E:\gss_test\test.pdf";
5251
string outputPath = @"E:\gss_test\output\";
@@ -68,7 +67,7 @@ public void Start()
6867
{
6968
string pageFilePath = Path.Combine(outputPath, "Page-" + pageNumber.ToString() + ".png");
7069

71-
Image img = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
70+
Image img = rasterizer.GetPage(desired_dpi, pageNumber);
7271
img.Save(pageFilePath, ImageFormat.Png);
7372

7473
Console.WriteLine(pageFilePath);

Ghostscript.NET/GhostscriptLibrary.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ protected virtual void Dispose(bool disposing)
206206
public gsapi_run_string_end @gsapi_run_string_end = null;
207207
public gsapi_run_string_with_length @gsapi_run_string_with_length = null;
208208
public gsapi_run_string @gsapi_run_string = null;
209+
public gsapi_run_ptr_string @gsapi_run_ptr_string = null;
209210
public gsapi_run_file @gsapi_run_file = null;
210211
public gsapi_exit @gsapi_exit = null;
211212

@@ -294,6 +295,11 @@ private void Initialize()
294295
if (this.gsapi_run_string == null)
295296
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_run_string"));
296297

298+
this.gsapi_run_ptr_string = _library.GetDelegateForFunction<gsapi_run_ptr_string>("gsapi_run_string");
299+
300+
if (this.gsapi_run_ptr_string == null)
301+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_run_string"));
302+
297303
this.gsapi_run_file = _library.GetDelegateForFunction<gsapi_run_file>("gsapi_run_file");
298304

299305
if (this.gsapi_run_file == null)

Ghostscript.NET/Helpers/StringHelper.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// This file is part of Ghostscript.NET library
44
//
55
// Author: Josip Habjan (habjan@gmail.com, http://www.linkedin.com/in/habjan)
6-
// Copyright (c) 2013-2016 by Josip Habjan. All rights reserved.
6+
// Copyright (c) 2013-2021 by Josip Habjan. All rights reserved.
77
//
88
// Permission is hereby granted, free of charge, to any person obtaining
99
// a copy of this software and associated documentation files (the
@@ -24,6 +24,8 @@
2424
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2525
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626

27+
using System;
28+
using System.Runtime.InteropServices;
2729
using System.Text;
2830

2931
namespace Ghostscript.NET
@@ -38,5 +40,28 @@ public static string ToUtf8String(string value)
3840
}
3941

4042
#endregion
43+
44+
#region NativeUtf8FromString
45+
46+
public static IntPtr NativeUtf8FromString(string managedString)
47+
{
48+
int len = Encoding.UTF8.GetByteCount(managedString);
49+
byte[] buffer = new byte[len + 1]; // null-terminator allocated
50+
Encoding.UTF8.GetBytes(managedString, 0, managedString.Length, buffer, 0);
51+
IntPtr nativeUtf8 = Marshal.AllocHGlobal(buffer.Length);
52+
Marshal.Copy(buffer, 0, nativeUtf8, buffer.Length);
53+
return nativeUtf8;
54+
}
55+
56+
#endregion
57+
58+
#region HasNonASCIIChars
59+
60+
public static bool HasNonASCIIChars(string str)
61+
{
62+
return (Encoding.UTF8.GetByteCount(str) != str.Length);
63+
}
64+
65+
#endregion
4166
}
4267
}

Ghostscript.NET/Interpreter/GhostscriptInterpreter.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,30 @@ public int Run(string str)
370370

371371
#endregion
372372

373+
#region Run
374+
375+
/// <summary>
376+
/// Runs a string.
377+
/// </summary>
378+
internal int Run(IntPtr str)
379+
{
380+
lock (this)
381+
{
382+
int exit_code;
383+
384+
int rc_run = _gs.gsapi_run_ptr_string(_gs_instance, str, 0, out exit_code);
385+
386+
if (ierrors.IsFatalIgnoreNeedInput(rc_run))
387+
{
388+
throw new GhostscriptAPICallException("gsapi_run_string", rc_run);
389+
}
390+
391+
return rc_run;
392+
}
393+
}
394+
395+
#endregion
396+
373397
#region RunFile
374398

375399
/// <summary>

Ghostscript.NET/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[assembly: AssemblyConfiguration("")]
88
[assembly: AssemblyCompany("Habjan")]
99
[assembly: AssemblyProduct("Ghostscript.NET")]
10-
[assembly: AssemblyCopyright("Copyright © 2013-2016 by Josip Habjan (habjan@gmail.com). All Rights Reserved.")]
10+
[assembly: AssemblyCopyright("Copyright © 2013-2021 by Josip Habjan (habjan@gmail.com). All Rights Reserved.")]
1111
[assembly: AssemblyTrademark("")]
1212
[assembly: AssemblyCulture("")]
1313

@@ -16,5 +16,5 @@
1616
[assembly: Guid("5b1fe89e-e040-4b7a-9c05-478c1756b6e8")]
1717

1818

19-
[assembly: AssemblyVersion("1.2.1")]
20-
[assembly: AssemblyFileVersion("1.2.1")]
19+
[assembly: AssemblyVersion("1.2.2")]
20+
[assembly: AssemblyFileVersion("1.2.2")]

Ghostscript.NET/Rasterizer/GhostscriptRasterizer.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,12 @@ public int PageCount
282282
/// <summary>
283283
/// Gets PDF page as System.Drawing.Image.
284284
/// </summary>
285-
/// <param name="xDpi">Desired x DPI.</param>
286-
/// <param name="yDpi">Desired y DPI.</param>
285+
/// <param name="dpi">Desired dpi.</param>
287286
/// <param name="pageNumber">The page number.</param>
288287
/// <returns>PDF page represented as System.Drawing.Image.</returns>
289-
public Image GetPage(int Dpi, int pageNumber)
288+
public Image GetPage(int dpi, int pageNumber)
290289
{
291-
_viewer.Dpi = Dpi;
290+
_viewer.Dpi = dpi;
292291
_viewer.ShowPage(pageNumber, true);
293292
return _lastRasterizedImage;
294293
}

Ghostscript.NET/Viewer/FormatHandlers/GhostscriptViewerPdfFormatHandler.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// This file is part of Ghostscript.NET library
44
//
55
// Author: Josip Habjan (habjan@gmail.com, http://www.linkedin.com/in/habjan)
6-
// Copyright (c) 2013-2016 by Josip Habjan. All rights reserved.
6+
// Copyright (c) 2013-2021 by Josip Habjan. All rights reserved.
77
//
88
// Author ported some parts of this code from GSView.
99
//
@@ -82,10 +82,15 @@ public override void Initialize()
8282
/DSCPageCount 0 def
8383
");
8484

85-
// open PDF support dictionaries
86-
this.Execute(@"
85+
if (this.Viewer.Interpreter.LibraryRevision < 927)
86+
{
87+
// this should be executed only for gs versions below 9.27
88+
89+
// open PDF support dictionaries
90+
this.Execute(@"
8791
GS_PDF_ProcSet begin
8892
pdfdict begin");
93+
}
8994
}
9095

9196
#endregion
@@ -94,12 +99,24 @@ GS_PDF_ProcSet begin
9499

95100
public override void Open(string filePath)
96101
{
97-
filePath = StringHelper.ToUtf8String(filePath);
102+
int res = 0;
103+
104+
if (StringHelper.HasNonASCIIChars(filePath))
105+
{
106+
IntPtr ptrStr = StringHelper.NativeUtf8FromString(string.Format("({0}) (r) file runpdfbegin", filePath.Replace("\\", "/")));
98107

99-
// open PDF file
100-
int res = this.Execute(string.Format("({0}) (r) file runpdfbegin", filePath.Replace("\\", "/")));
108+
// open PDF file
109+
res = this.Execute(ptrStr);
110+
111+
System.Runtime.InteropServices.Marshal.FreeHGlobal(ptrStr);
112+
}
113+
else
114+
{
115+
// open PDF file
116+
res = this.Execute(string.Format("({0}) (r) file runpdfbegin", filePath.Replace("\\", "/")));
117+
}
101118

102-
if(res == ierrors.e_ioerror)
119+
if (res == ierrors.e_ioerror)
103120
{
104121
throw new GhostscriptException("IO error for file: '" + filePath + "'", ierrors.e_ioerror);
105122
}

0 commit comments

Comments
 (0)