After upgrading from 18.5.0 to 21.4.0, the issue still exists.
My Asp.Net Core Web Service, .Net Core Version is 2.2.
How to reproduce the issue:
After starting your application or service, run the convert pages to png images two or more times.
Start with the second time, some form field values will be lost.
Here is my test code:
// PdfHelper.SplitPdfToPagesV1(fileData, 780);
// PdfHelper.SplitPdfToPagesV1(fileData, 780);
Result:
v1 input bytes: 4488515
v1 memory stream bytes: 4488515
v1 page: 1, Device Process stream length: 89256 // Different with second time
v1 width: 780, page: 1, to png stream length: 89256
v1 page: 2, Device Process stream length: 111503
v1 width: 780, page: 2, to png stream length: 111503
v1 input bytes: 4488515
v1 memory stream bytes: 4488515
v1 page: 1, Device Process stream length: 78128
v1 width: 780, page: 1, to png stream length: 78128
v1 page: 2, Device Process stream length: 87056
v1 width: 780, page: 2, to png stream length: 87056
// PdfHelper.SplitPdfToPagesV2(fileData, 780);
// PdfHelper.SplitPdfToPagesV2(fileData, 780);
Result:
v2 input bytes: 4488515
v2 memory stream bytes: 4488515
v2 page: 1, Page ConvertPageToPNGMemoryStream stream length: 644774 // Different with second time
v2 width: 780, page: 1, to png stream length: 118849
v2 page: 2, Page ConvertPageToPNGMemoryStream stream length: 694750
v2 width: 780, page: 2, to png stream length: 165614
v2 input bytes: 4488515
v2 memory stream bytes: 4488515
v2 page: 1, Page ConvertPageToPNGMemoryStream stream length: 573272
v2 width: 780, page: 1, to png stream length: 102409
v2 page: 2, Page ConvertPageToPNGMemoryStream stream length: 538528
v2 width: 780, page: 2, to png stream length: 133305
PdfHelper.SplitPdfToPagesV3(fileData, 780);
PdfHelper.SplitPdfToPagesV3(fileData, 780);
Result:
v3 input bytes: 4488515
v3 memory stream bytes: 4488515
v3 page: 1, Page AsByteArray stream length: 45319878
v3 width: 780, page: 1, to png stream length: 96054 // Different with second time
v3 page: 2, Page AsByteArray stream length: 45319878
v3 width: 780, page: 2, to png stream length: 132969
v3 input bytes: 4488515
v3 memory stream bytes: 4488515
v3 page: 1, Page AsByteArray stream length: 45319878
v3 width: 780, page: 1, to png stream length: 82841
v3 page: 2, Page AsByteArray stream length: 45319878
v3 width: 780, page: 2, to png stream length: 106231
Detailed codes:
public static void SplitPdfToPagesV3(byte[] pdfBytes, int width)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"\nv3 input bytes: {pdfBytes.Length}");
using (var memoryStream = new MemoryStream(pdfBytes))
{
Console.WriteLine($"v3 memory stream bytes: {memoryStream.Length}");
using (var pdfDocument = new Document(memoryStream, true))
{
for (var pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
{
int boxWidth = (int)pdfDocument.Pages[pageCount].MediaBox.Width;
int boxHeight = (int)pdfDocument.Pages[pageCount].MediaBox.Height;
var resolution = new Resolution(boxWidth, boxHeight);
using (var imageStream = new MemoryStream(pdfDocument.Pages[pageCount].AsByteArray(resolution)))
{
Console.WriteLine($"v3 page: {pageCount}, Page AsByteArray stream length: {imageStream.Length}");
using (var bmp = System.Drawing.Image.FromStream(imageStream))
{
Size thumbnailSize = GetThumbnailSize(bmp, width);
System.Drawing.Image thumbnailImage = bmp.GetThumbnailImage(thumbnailSize.Width, thumbnailSize.Height, null, IntPtr.Zero);
using (var toImageStream = new MemoryStream())
{
thumbnailImage.Save(toImageStream, System.Drawing.Imaging.ImageFormat.Png);
Console.WriteLine($"v3 width: {width}, page: {pageCount}, to png stream length: {toImageStream.Length}");
}
}
}
}
}
}
Console.ResetColor();
}
public static void SplitPdfToPagesV2(byte[] pdfBytes, int width)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"\nv2 input bytes: {pdfBytes.Length}");
using (var memoryStream = new MemoryStream(pdfBytes))
{
Console.WriteLine($"v2 memory stream bytes: {memoryStream.Length}");
using (var pdfDocument = new Document(memoryStream, true))
{
for (var pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
{
using (var imageStream = pdfDocument.ConvertPageToPNGMemoryStream(pdfDocument.Pages[pageCount]))
{
Console.WriteLine($"v2 page: {pageCount}, Page ConvertPageToPNGMemoryStream stream length: {imageStream.Length}");
using (var pngImage = System.Drawing.Image.FromStream(imageStream))
{
Size thumbnailSize = GetThumbnailSize(pngImage, width);
System.Drawing.Image thumbnailImage = pngImage.GetThumbnailImage(thumbnailSize.Width, thumbnailSize.Height, null, IntPtr.Zero);
using (var toImageStream = new MemoryStream())
{
thumbnailImage.Save(toImageStream, System.Drawing.Imaging.ImageFormat.Png);
Console.WriteLine($"v2 width: {width}, page: {pageCount}, to png stream length: {toImageStream.Length}");
}
}
}
}
}
}
Console.ResetColor();
}
public static void SplitPdfToPagesV1(byte[] pdfBytes, int width)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"\nv1 input bytes: {pdfBytes.Length}");
using (var memoryStream = new MemoryStream(pdfBytes))
{
Console.WriteLine($"v1 memory stream bytes: {memoryStream.Length}");
using (var pdfDocument = new Document(memoryStream, true))
{
for (var pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
{
int boxWidth = (int)pdfDocument.Pages[pageCount].MediaBox.Width;
int boxHeight = (int)pdfDocument.Pages[pageCount].MediaBox.Height;
var resolution = new Resolution(boxWidth, boxHeight);
using (var imageStream = new MemoryStream())
{
ImageDevice pngDevice = new PngDevice(boxWidth, boxHeight, resolution);
pngDevice.RenderingOptions.InterpolationHighQuality = true;
pngDevice.RenderingOptions.UseNewImagingEngine = true;
pngDevice.Process(pdfDocument.Pages[pageCount], imageStream);
Console.WriteLine($"v1 page: {pageCount}, Device Process stream length: {imageStream.Length}");
var bytes = imageStream.ToArray();
Console.WriteLine($"v1 width: {width}, page: {pageCount}, to png stream length: {bytes.Length}");
}
}
}
}
Console.ResetColor();
}
public static Size GetThumbnailSize(System.Drawing.Image original, int width)
{
// Maximum size of any dimension.
int maxPixels = width;
// Width and height.
int originalWidth = original.Width;
int originalHeight = original.Height;
// Compute best factor to scale entire image based on larger dimension.
double factor;
if (originalWidth > originalHeight)
{
factor = (double)maxPixels / originalWidth;
}
else
{
factor = (double)maxPixels / originalHeight;
}
// Return thumbnail size.
return new Size((int)(originalWidth * factor), (int)(originalHeight * factor));
}