以下代码打开 PDF 文件,找到 QR 码所在的位置,然后旋转 PDF 文件,使 QR 码位于左上角。
代码读取和旋转很好,但它使用原始布局保存文件,我希望它保存旋转的文件。
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);
}