More pagination
converter settings json
This commit is contained in:
@@ -1,37 +1,87 @@
|
||||
using Docnet.Core.Models;
|
||||
using Docnet.Core;
|
||||
using Docnet.Core.Exceptions;
|
||||
using Docnet.Core.Models;
|
||||
using Docnet.Core.Readers;
|
||||
using Docnet.Core;
|
||||
using khofmann;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Runtime.InteropServices;
|
||||
using Docnet.Core.Exceptions;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
#region Defines
|
||||
|
||||
const double MM2IN = 25.4;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Main Code
|
||||
|
||||
/*
|
||||
if (args.Length == 0)
|
||||
{
|
||||
Console.WriteLine("No PDF specified");
|
||||
return 1;
|
||||
}
|
||||
|
||||
Console.WriteLine($"Conversion for {args[0]}");
|
||||
Console.WriteLine("Using configuration");
|
||||
Console.WriteLine("\tA4 at 96dpi");
|
||||
Console.WriteLine();
|
||||
return ReadPDF(args[0]);
|
||||
Console.WriteLine($"Conversion for {args[0]}\n");
|
||||
*/
|
||||
Tuple<int, int> size = ReadConfig();
|
||||
return ReadPDF("MD11_FCOM_vol1.pdf", size.Item1, size.Item2);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Functions
|
||||
|
||||
int ReadPDF(string path)
|
||||
Tuple<int, int> ReadConfig()
|
||||
{
|
||||
try
|
||||
{
|
||||
using FileStream stream = new("config.json", FileMode.Open, FileAccess.Read);
|
||||
Settings? settings = JsonSerializer.Deserialize<Settings>(stream);
|
||||
if (settings != null)
|
||||
{
|
||||
Paper paper = settings.PaperSizes.First(p => p.Name.Equals(settings.SelectedPaper));
|
||||
if (paper != null)
|
||||
{
|
||||
if (paper.Unit == Unit.Millimetre)
|
||||
{
|
||||
int width = Convert.ToInt32(Math.Round(paper.Width / MM2IN * settings.DPI));
|
||||
int height = Convert.ToInt32(Math.Round(paper.Height / MM2IN * settings.DPI));
|
||||
|
||||
Console.WriteLine($"Using {paper.Name}, {width}mm x {height}mm at {settings.DPI} DPI");
|
||||
|
||||
if (width < height) return new(width, height);
|
||||
else return new(height, width);
|
||||
}
|
||||
else
|
||||
{
|
||||
int width = Convert.ToInt32(Math.Round(paper.Width * settings.DPI));
|
||||
int height = Convert.ToInt32(Math.Round(paper.Height * settings.DPI));
|
||||
|
||||
Console.WriteLine($"Using {paper.Name}, ${width}in x {height}in at ${settings.DPI}");
|
||||
|
||||
if (width < height) return new(width, height);
|
||||
else return new(height, width);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
Console.WriteLine("Using default A4 at 96 dpi\n");
|
||||
|
||||
return new(794, 1122);
|
||||
}
|
||||
|
||||
int ReadPDF(string path, int width, int height)
|
||||
{
|
||||
string outPath = "";
|
||||
try
|
||||
{
|
||||
outPath = Path.GetFileNameWithoutExtension(path);
|
||||
Directory.CreateDirectory(outPath);
|
||||
} catch (Exception e)
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.Error.WriteLine("Error creating file directory");
|
||||
Console.Error.WriteLine(e.Message);
|
||||
@@ -41,54 +91,52 @@ int ReadPDF(string path)
|
||||
|
||||
try
|
||||
{
|
||||
using (IDocReader docReader = DocLib.Instance.GetDocReader(path, new PageDimensions(794, 1122)))
|
||||
using IDocReader docReader = DocLib.Instance.GetDocReader(path, new PageDimensions(width, height));
|
||||
int pages = docReader.GetPageCount();
|
||||
Console.WriteLine($"Converting {pages} pages");
|
||||
|
||||
for (int i = 0; i < pages; i++)
|
||||
{
|
||||
int pages = docReader.GetPageCount();
|
||||
Console.WriteLine($"Converting {pages} pages");
|
||||
|
||||
for (int i = 0; i < pages; i++)
|
||||
using (IPageReader pageReader = docReader.GetPageReader(i))
|
||||
{
|
||||
using (IPageReader pageReader = docReader.GetPageReader(i))
|
||||
byte[] rawBytes = pageReader.GetImage();
|
||||
int _width = pageReader.GetPageWidth();
|
||||
int _height = pageReader.GetPageHeight();
|
||||
|
||||
using (Bitmap doc = new(_width, _height, PixelFormat.Format32bppArgb))
|
||||
using (Bitmap bmp = new(_width, _height, PixelFormat.Format32bppArgb))
|
||||
{
|
||||
byte[] rawBytes = pageReader.GetImage();
|
||||
int width = pageReader.GetPageWidth();
|
||||
int height = pageReader.GetPageHeight();
|
||||
AddBytes(bmp, rawBytes);
|
||||
|
||||
using (Bitmap doc = new Bitmap(width, height, PixelFormat.Format32bppArgb))
|
||||
using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb))
|
||||
Graphics g = Graphics.FromImage(doc);
|
||||
g.FillRegion(Brushes.White, new(new Rectangle(0, 0, _width, _height)));
|
||||
g.DrawImage(bmp, new Point(0, 0));
|
||||
g.Save();
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
AddBytes(bmp, rawBytes);
|
||||
|
||||
Graphics g = Graphics.FromImage(doc);
|
||||
g.FillRegion(Brushes.White, new Region(new Rectangle(0, 0, width, height)));
|
||||
g.DrawImage(bmp, new Point(0, 0));
|
||||
g.Save();
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
Bitmap thumb = new Bitmap(doc, new Size(doc.Width / 10, doc.Height / 10));
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
//saving and exporting
|
||||
thumb.Save(stream, ImageFormat.Jpeg);
|
||||
Console.WriteLine($"Ouputing tumbnail");
|
||||
File.WriteAllText($"{outPath}\\thumb.bjpg", Convert.ToBase64String(stream.ToArray()));
|
||||
};
|
||||
}
|
||||
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
Bitmap thumb = new(doc, new(doc.Width / 10, doc.Height / 10));
|
||||
using (MemoryStream stream = new())
|
||||
{
|
||||
//saving and exporting
|
||||
doc.Save(stream, ImageFormat.Jpeg);
|
||||
Console.WriteLine($"Ouputing page {i + 1}");
|
||||
File.WriteAllText($"{outPath}\\{i + 1}.bjpg", Convert.ToBase64String(stream.ToArray()));
|
||||
thumb.Save(stream, ImageFormat.Jpeg);
|
||||
Console.WriteLine($"Ouputing tumbnail");
|
||||
File.WriteAllText($"{outPath}\\thumb.bjpg", Convert.ToBase64String(stream.ToArray()));
|
||||
};
|
||||
}
|
||||
|
||||
using (MemoryStream stream = new())
|
||||
{
|
||||
//saving and exporting
|
||||
doc.Save(stream, ImageFormat.Jpeg);
|
||||
Console.WriteLine($"Ouputing page {i + 1}");
|
||||
File.WriteAllText($"{outPath}\\{i + 1}.bjpg", Convert.ToBase64String(stream.ToArray()));
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
catch(DocnetLoadDocumentException)
|
||||
catch (DocnetLoadDocumentException)
|
||||
{
|
||||
Console.Error.WriteLine("File is not a PDF");
|
||||
Console.ReadKey();
|
||||
@@ -101,7 +149,7 @@ int ReadPDF(string path)
|
||||
|
||||
void AddBytes(Bitmap bmp, byte[] rawBytes)
|
||||
{
|
||||
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
|
||||
Rectangle rect = new(0, 0, bmp.Width, bmp.Height);
|
||||
|
||||
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.WriteOnly, bmp.PixelFormat);
|
||||
nint pNative = bmpData.Scan0;
|
||||
@@ -110,4 +158,33 @@ void AddBytes(Bitmap bmp, byte[] rawBytes)
|
||||
bmp.UnlockBits(bmpData);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region classes
|
||||
|
||||
namespace khofmann
|
||||
{
|
||||
public class Settings
|
||||
{
|
||||
public Paper[] PaperSizes { get; set; } = [];
|
||||
public string? SelectedPaper { get; set; }
|
||||
public int DPI { get; set; }
|
||||
}
|
||||
|
||||
public class Paper
|
||||
{
|
||||
public string Name { get; set; } = "";
|
||||
public double Width { get; set; }
|
||||
public double Height { get; set; }
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
public Unit Unit { get; set; }
|
||||
}
|
||||
|
||||
public enum Unit
|
||||
{
|
||||
Inch,
|
||||
Millimetre
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"PaperSizes": [
|
||||
{
|
||||
"Name": "A4",
|
||||
"Width": 210,
|
||||
"Height": 297,
|
||||
"Unit": "Millimetre"
|
||||
},
|
||||
{
|
||||
"Name": "A5",
|
||||
"Width": 148.5,
|
||||
"Height": 210,
|
||||
"Unit": "Millimetre"
|
||||
},
|
||||
{
|
||||
"Name": "Letter",
|
||||
"Width": 8.5,
|
||||
"Height": 11,
|
||||
"Unit": "Inch"
|
||||
}
|
||||
],
|
||||
"SelectedPaper": "A4",
|
||||
"DPI": 96
|
||||
}
|
||||
@@ -8,13 +8,18 @@
|
||||
<IsPublishable>False</IsPublishable>
|
||||
<Trimming>full</Trimming>
|
||||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<Platforms>x64</Platforms>
|
||||
<BaseIntermediateOutputPath>obj\</BaseIntermediateOutputPath>
|
||||
<BaseOutputPath>bin\</BaseOutputPath>
|
||||
<GenerateDocumentationFile>False</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<WarningLevel>9999</WarningLevel>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<WarningLevel>9999</WarningLevel>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -23,4 +28,10 @@
|
||||
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="config.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -3,18 +3,18 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.8.34309.116
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "converter", "converter.csproj", "{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "converter", "converter.csproj", "{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
Debug|x64 = Debug|x64
|
||||
Release|x64 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Debug|x64.Build.0 = Debug|x64
|
||||
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Release|x64.ActiveCfg = Release|x64
|
||||
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Release|x64.Build.0 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
Reference in New Issue
Block a user