From 2133f4a945f5df25add2ce29869a076e020442c5 Mon Sep 17 00:00:00 2001
From: Maksym Kei <111739434+maksymkeihz@users.noreply.github.com>
Date: Mon, 23 Mar 2026 15:07:50 +0200
Subject: [PATCH] added updates to fix issue #234
---
Demo/Program.cs | 17 +++++++++++++
MuPDF.NET.Test/UtilsTest.cs | 50 +++++++++++++++++++++++++++++++++++++
MuPDF.NET/Page.cs | 10 +++++++-
MuPDF.NET/Utils.cs | 12 ++++-----
4 files changed, 82 insertions(+), 7 deletions(-)
create mode 100644 MuPDF.NET.Test/UtilsTest.cs
diff --git a/Demo/Program.cs b/Demo/Program.cs
index 19fcedf..08fc6ef 100644
--- a/Demo/Program.cs
+++ b/Demo/Program.cs
@@ -85,10 +85,27 @@ static void Main(string[] args)
TestGetText();
TestMarkdownReader();
TestRecompressJBIG2();
+ TestIssue234();
return;
}
+ static void TestIssue234()
+ {
+ Console.WriteLine("\n=== TestIssue234 =======================");
+
+ var pix = new Pixmap("../../../TestDocuments/Image/boxedpage.jpg"); // 629x1000 image
+ var scaled = new Pixmap(pix, 943, 1500, null); // scale up
+ byte[] jpeg = scaled.ToBytes("jpg", 65);
+
+ using var doc = new Document();
+ Page page = doc.NewPage(0, 943, 1500);
+ page.InsertImage(page.Rect, stream: jpeg);
+ page.Dispose();
+ doc.Save("issue_234.pdf");
+ doc.Close();
+ }
+
static void TestRecompressJBIG2()
{
Console.WriteLine("\n=== TestJBIG2 =======================");
diff --git a/MuPDF.NET.Test/UtilsTest.cs b/MuPDF.NET.Test/UtilsTest.cs
new file mode 100644
index 0000000..2aeb0e7
--- /dev/null
+++ b/MuPDF.NET.Test/UtilsTest.cs
@@ -0,0 +1,50 @@
+using NUnit.Framework;
+using MuPDF.NET;
+
+namespace MuPDF.NET.Test
+{
+ public class UtilsTest
+ {
+ [Test]
+ public void FloatToString_NoScientificNotation()
+ {
+ Assert.That(Utils.FloatToString(1.5f), Is.EqualTo("1.5"));
+ Assert.That(Utils.FloatToString(0f), Is.EqualTo("0"));
+ Assert.That(Utils.FloatToString(-123.456f), Is.EqualTo("-123.456"));
+ Assert.That(Utils.FloatToString(1000000f), Is.EqualTo("1000000"));
+
+ // Values that would use scientific notation with default ToString
+ string small = Utils.FloatToString(0.0000123f);
+ Assert.That(small.Contains("E") || small.Contains("e"), Is.False, "Should not use scientific notation");
+ Assert.That(small.Contains("0.00001") || small.Contains("0.000012"), Is.True);
+ }
+
+ [Test]
+ public void FloatToString_InvariantCulture()
+ {
+ string result = Utils.FloatToString(1.5f);
+ Assert.That(result.Contains("."), Is.True);
+ Assert.That(result.Contains(","), Is.False);
+ }
+
+ [Test]
+ public void DoubleToString_NoScientificNotation()
+ {
+ Assert.That(Utils.DoubleToString(1.5), Is.EqualTo("1.5"));
+ Assert.That(Utils.DoubleToString(0), Is.EqualTo("0"));
+ Assert.That(Utils.DoubleToString(-123.456), Is.EqualTo("-123.456"));
+ Assert.That(Utils.DoubleToString(1000000), Is.EqualTo("1000000"));
+
+ string small = Utils.DoubleToString(1.23e-10);
+ Assert.That(small.Contains("E") || small.Contains("e"), Is.False, "Should not use scientific notation");
+ }
+
+ [Test]
+ public void DoubleToString_InvariantCulture()
+ {
+ string result = Utils.DoubleToString(1.5);
+ Assert.That(result.Contains("."), Is.True);
+ Assert.That(result.Contains(","), Is.False);
+ }
+ }
+}
diff --git a/MuPDF.NET/Page.cs b/MuPDF.NET/Page.cs
index b3e5484..4d47a8c 100644
--- a/MuPDF.NET/Page.cs
+++ b/MuPDF.NET/Page.cs
@@ -2242,7 +2242,15 @@ public int InsertImage(
xobject.pdf_dict_puts(imgName, ref_);
FzBuffer nres = mupdf.mupdf.fz_new_buffer(50);
nres.fz_append_string(
- string.Format(System.Globalization.CultureInfo.InvariantCulture, template, mat.a, mat.b, mat.c, mat.d, mat.e, mat.f, imgName)
+ string.Format(System.Globalization.CultureInfo.InvariantCulture,
+ template,
+ Utils.FloatToString(mat.a),
+ Utils.FloatToString(mat.b),
+ Utils.FloatToString(mat.c),
+ Utils.FloatToString(mat.d),
+ Utils.FloatToString(mat.e),
+ Utils.FloatToString(mat.f),
+ imgName)
);
Utils.InsertContents(pageDoc, page.obj(), nres, overlay);
}
diff --git a/MuPDF.NET/Utils.cs b/MuPDF.NET/Utils.cs
index bf551ce..a777e08 100644
--- a/MuPDF.NET/Utils.cs
+++ b/MuPDF.NET/Utils.cs
@@ -7913,19 +7913,19 @@ internal static void SetDotCultureForNumber()
}
///
- /// Converts a float to string with dot as decimal separator, regardless of culture
+ /// Converts a float to string with dot as decimal separator, without scientific notation.
///
- internal static string FloatToString(float value)
+ public static string FloatToString(float value)
{
- return value.ToString(CultureInfo.InvariantCulture);
+ return value.ToString("0.#######", CultureInfo.InvariantCulture);
}
///
- /// Converts a double to string with dot as decimal separator, regardless of culture
+ /// Converts a double to string with dot as decimal separator, without scientific notation.
///
- internal static string DoubleToString(double value)
+ public static string DoubleToString(double value)
{
- return value.ToString(CultureInfo.InvariantCulture);
+ return value.ToString("0.#################", CultureInfo.InvariantCulture);
}
///