161 lines
6.1 KiB
C#
161 lines
6.1 KiB
C#
// /**
|
|
// * File: pdfstamp.cs
|
|
// * Author: haraldwolff
|
|
// *
|
|
// * This file and it's content is copyrighted by the Author and / or copyright holder.
|
|
// * Any use wihtout proper permission is illegal and may lead to legal actions.
|
|
// *
|
|
// *
|
|
// **/
|
|
using System;
|
|
using ln.types;
|
|
using iTextSharp.text.pdf;
|
|
using System.IO;
|
|
using ln.logging;
|
|
using iTextSharp.text;
|
|
|
|
namespace ln.pdfstamp
|
|
{
|
|
public class PDFStamp
|
|
{
|
|
public static void Main(String[] args)
|
|
{
|
|
ArgumentContainer argumentContainer = new ArgumentContainer();
|
|
argumentContainer.Add(new Argument('x', null, "0"));
|
|
argumentContainer.Add(new Argument('y', null, "0"));
|
|
argumentContainer.Add('s', "stamp", null);
|
|
argumentContainer.Add('o', "output", ".");
|
|
argumentContainer.Add('e', "extension", ".stamped");
|
|
argumentContainer.Add('d', "dpi", "72");
|
|
argumentContainer.Add('w', "width", null);
|
|
argumentContainer.Add('h', "height", null);
|
|
argumentContainer.Add((char)0, "daemon");
|
|
|
|
argumentContainer.Parse(args);
|
|
|
|
if (argumentContainer["daemon"].IsSet)
|
|
{
|
|
Daemon daemon = new Daemon();
|
|
daemon.Start();
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
Image stampImage = Image.GetInstance(Path.GetFullPath(argumentContainer['s'].Value));
|
|
if (stampImage == null)
|
|
{
|
|
throw new FileNotFoundException(argumentContainer['s'].Value);
|
|
}
|
|
|
|
float lx = (float)(argumentContainer['x'].DoubleValue / 25.4 * 72);
|
|
float ly = (float)(argumentContainer['y'].DoubleValue / 25.4 * 72);
|
|
|
|
double stampDpiX = stampImage.DpiX == 0 ? 72 : stampImage.DpiX; // / argumentContainer['d'].DoubleValue;
|
|
double stampDpiY = stampImage.DpiY == 0 ? 72 : stampImage.DpiY; // / argumentContainer['d'].DoubleValue;
|
|
|
|
double stampWidth = stampImage.Width / stampDpiX * 25.4;
|
|
double stampHeight = stampImage.Height / stampDpiY * 25.4;
|
|
|
|
Logging.Log(LogLevel.DEBUG, "Stamp: {0}/{1}", stampWidth, stampHeight);
|
|
|
|
if (argumentContainer['w'].HasValue && argumentContainer['h'].HasValue)
|
|
{
|
|
Logging.Log(LogLevel.DEBUG, "WH");
|
|
|
|
stampWidth = (float)argumentContainer['w'].DoubleValue;
|
|
stampHeight = (float)argumentContainer['h'].DoubleValue;
|
|
}
|
|
else if (argumentContainer['w'].HasValue && !argumentContainer['h'].HasValue)
|
|
{
|
|
Logging.Log(LogLevel.DEBUG, "W");
|
|
|
|
stampHeight *= argumentContainer['w'].DoubleValue / stampWidth;
|
|
stampWidth = (float)argumentContainer['w'].DoubleValue;
|
|
}
|
|
else if (!argumentContainer['w'].HasValue && argumentContainer['h'].HasValue)
|
|
{
|
|
Logging.Log(LogLevel.DEBUG, "H");
|
|
|
|
stampWidth *= argumentContainer['h'].DoubleValue / stampHeight;
|
|
stampHeight = (float)argumentContainer['h'].DoubleValue;
|
|
}
|
|
|
|
stampWidth = stampWidth / 25.4 * 72.0;
|
|
stampHeight = stampHeight / 25.4 * 72.0;
|
|
|
|
Logging.Log(LogLevel.DEBUG, "Stamp: {0}/{1}",stampWidth,stampHeight);
|
|
|
|
foreach (string filename in argumentContainer.AdditionalArguments)
|
|
{
|
|
string outputFilename = Path.Combine(
|
|
argumentContainer['o'].Value,
|
|
string.Format("{0}{1}.pdf",Path.GetFileNameWithoutExtension(filename), argumentContainer['e'].Value)
|
|
);
|
|
|
|
Logging.Log(LogLevel.INFO, "{0} -> {1}", filename, outputFilename);
|
|
|
|
using (FileStream outputStream = new FileStream(outputFilename, FileMode.Create))
|
|
{
|
|
PdfReader pdfReader = new PdfReader(filename);
|
|
PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream);
|
|
|
|
|
|
for (int p = 0; p < pdfReader.NumberOfPages; p++)
|
|
{
|
|
Rectangle pageSize = pdfReader.GetPageSize(p + 1);
|
|
PdfContentByte overContent = pdfStamper.GetOverContent(p+1);
|
|
|
|
overContent.AddImage(stampImage, (float)stampWidth, 0, 0, (float)stampHeight, lx, (float)(pageSize.Height - ly - stampHeight));
|
|
}
|
|
|
|
pdfStamper.Close();
|
|
pdfReader.Close();
|
|
|
|
outputStream.Close();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public static void Stamp(Stream pdfStream,Image stampImage,float left,float top,float width,Stream outputStream)
|
|
{
|
|
float lx = (float)(left / 25.4 * 72);
|
|
float ly = (float)(top / 25.4 * 72);
|
|
|
|
double stampDpiX = stampImage.DpiX == 0 ? 72 : stampImage.DpiX; // / argumentContainer['d'].DoubleValue;
|
|
double stampDpiY = stampImage.DpiY == 0 ? 72 : stampImage.DpiY; // / argumentContainer['d'].DoubleValue;
|
|
|
|
double stampWidth = stampImage.Width / stampDpiX * 25.4;
|
|
double stampHeight = stampImage.Height / stampDpiY * 25.4;
|
|
|
|
Logging.Log(LogLevel.DEBUG, "Stamp: (pre ) {0}/{1}", stampWidth, stampHeight);
|
|
|
|
stampHeight = stampHeight * width / stampWidth;
|
|
stampWidth = width;
|
|
|
|
stampWidth = stampWidth / 25.4 * 72.0;
|
|
stampHeight = stampHeight / 25.4 * 72.0;
|
|
|
|
Logging.Log(LogLevel.DEBUG, "Stamp: (final) {0}/{1}", stampWidth, stampHeight);
|
|
|
|
PdfReader pdfReader = new PdfReader(pdfStream);
|
|
PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream);
|
|
|
|
|
|
for (int p = 0; p < pdfReader.NumberOfPages; p++)
|
|
{
|
|
Rectangle pageSize = pdfReader.GetPageSize(p + 1);
|
|
PdfContentByte overContent = pdfStamper.GetOverContent(p + 1);
|
|
|
|
overContent.AddImage(stampImage, (float)stampWidth, 0, 0, (float)stampHeight, lx, (float)(pageSize.Height - ly - stampHeight));
|
|
}
|
|
|
|
pdfStamper.Close();
|
|
pdfReader.Close();
|
|
}
|
|
|
|
}
|
|
}
|