Converter and direct BASE64 files
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
using Docnet.Core.Models;
|
||||
using Docnet.Core.Readers;
|
||||
using Docnet.Core;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Runtime.InteropServices;
|
||||
using Docnet.Core.Exceptions;
|
||||
|
||||
#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]);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Functions
|
||||
|
||||
int ReadPDF(string path)
|
||||
{
|
||||
string outPath = "";
|
||||
try
|
||||
{
|
||||
outPath = Path.GetFileNameWithoutExtension(path);
|
||||
Directory.CreateDirectory(outPath);
|
||||
} catch (Exception e)
|
||||
{
|
||||
Console.Error.WriteLine("Error creating file directory");
|
||||
Console.Error.WriteLine(e.Message);
|
||||
Console.ReadKey();
|
||||
return 2;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using (IDocReader docReader = DocLib.Instance.GetDocReader(path, new PageDimensions(794, 1122)))
|
||||
{
|
||||
int pages = docReader.GetPageCount();
|
||||
Console.WriteLine($"Converting {pages} pages");
|
||||
|
||||
for (int i = 0; i < pages; i++)
|
||||
{
|
||||
using (IPageReader pageReader = docReader.GetPageReader(i))
|
||||
{
|
||||
byte[] rawBytes = pageReader.GetImage();
|
||||
int width = pageReader.GetPageWidth();
|
||||
int height = pageReader.GetPageHeight();
|
||||
|
||||
using (Bitmap doc = new Bitmap(width, height, PixelFormat.Format32bppArgb))
|
||||
using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb))
|
||||
{
|
||||
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())
|
||||
{
|
||||
//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)
|
||||
{
|
||||
Console.Error.WriteLine("File is not a PDF");
|
||||
Console.ReadKey();
|
||||
return 3;
|
||||
}
|
||||
|
||||
Console.ReadKey();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void AddBytes(Bitmap bmp, byte[] rawBytes)
|
||||
{
|
||||
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
|
||||
|
||||
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.WriteOnly, bmp.PixelFormat);
|
||||
nint pNative = bmpData.Scan0;
|
||||
|
||||
Marshal.Copy(rawBytes, 0, pNative, rawBytes.Length);
|
||||
bmp.UnlockBits(bmpData);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPublishable>False</IsPublishable>
|
||||
<Trimming>full</Trimming>
|
||||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<WarningLevel>9999</WarningLevel>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<WarningLevel>9999</WarningLevel>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Docnet.Core" Version="2.6.0" />
|
||||
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
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}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
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
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {D95A0B4E-6516-4CBE-84E0-5FF1068CEF60}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
Reference in New Issue
Block a user