次のコードは、PDF ファイルを開き、QR コードが配置されている場所を見つけてから、QR コードが左上隅に配置されるように PDF ファイルを回転させます。
コードは問題なく読み取り、回転しますが、元のレイアウトを使用してファイルを保存するので、ファイルを回転して保存したいと思います。
for (int pg = 0; pg < inputDocument.PageCount; pg++)
{
QRCodeDecoder decoder = new QRCodeDecoder();
string workGif = workingFilename.Replace(".pdf", string.Format(".{0}.gif", pg + 1));
GhostscriptWrapper.GeneratePageThumb(workingFilename, workGif, pg + 1, 300, 300); // size (last two params) does not seem to have any effect
using (var fullImg = new Bitmap(workGif))
{
var bandImg = fullImg.Clone(new System.Drawing.Rectangle(0, 0, 375, 375), fullImg.PixelFormat);
//AA THIS SECTION NEEDS TO BE CLEANED UP FOR ACTUAL USE
Bitmap result = fullImg;
//top-left
var bandImg1 = result.Clone(new System.Drawing.Rectangle(0, 0, result.Width/2, result.Height/2), fullImg.PixelFormat);
//top-right
var bandImg2 = result.Clone(new System.Drawing.Rectangle(result.Width / 2, 0, result.Width / 2, result.Height / 2), fullImg.PixelFormat);
//bottom-left
var bandImg3 = result.Clone(new System.Drawing.Rectangle(0, result.Height / 2, result.Width / 2, result.Height / 2), fullImg.PixelFormat);
//bottom-right
var bandImg4 = result.Clone(new System.Drawing.Rectangle(result.Width / 2, result.Height / 2, result.Width / 2, result.Height / 2), fullImg.PixelFormat);
Bitmap[] corners = new Bitmap[] { bandImg1, bandImg2, bandImg3, bandImg4 };
string QRinfo = "";
for (int i = 0; i < corners.Length; ++i)
{
string tempQRinfo = Process(corners[i]);
if (tempQRinfo != null)
{
QRinfo = tempQRinfo;
switch (i)
{
case 0: break; //already upper left
case 1: fullImg.RotateFlip(RotateFlipType.Rotate270FlipNone); break;//upper right: rotate -90
case 2: fullImg.RotateFlip(RotateFlipType.Rotate90FlipNone); break;//lower left: rotate 90
case 3: fullImg.RotateFlip(RotateFlipType.Rotate180FlipNone); break;//lower right: rotate 180
}
break;
}
}
if (QRinfo == null)
{
Console.WriteLine("QRCode was not found");
}
else
{
MessageBox.Show(QRinfo);
Console.WriteLine(QRinfo);
}
string[] qcode = QRinfo.Split('/');
string gid = qcode[qcode.Count() - 1];
Guid pgGuid = new Guid(gid);
var ar = dc.Assessments.FirstOrDefault(c => c.ID == pgGuid);
if (ar != null)
{
var p = inputDocument.Pages[pg];
string opdName = FILESTORELOCATION + pgGuid.ToString() + ".pdf";
PdfDocument opd = new PdfDocument(opdName);
opd.Pages.Add(p);
opd.Close();
ar.StoragePath = opdName;
ar.LastUploadedDT = DateTime.UtcNow;
ar.UploadedByUserID = uploadingUser;
dc.SubmitChanges();
}
}
File.Delete(workGif);
}