113 lines
3.5 KiB
C#
113 lines
3.5 KiB
C#
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 |