以下代码打开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);
}
答案 0 :(得分:1)
使用PDFSharp,您可以完成此任务。这个例子向您展示了如何。
var souce = // your rotated image here
PdfDocument doc = new PdfDocument();
doc.Pages.Add(new PdfPage());
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
XImage img = XImage.FromFile(source);
xgr.DrawImage(img, 0, 0);
doc.Save(destinaton);
doc.Close();
从here找到了这个。
答案 1 :(得分:0)
您可以使用GroupDocs.Merger for .NET API旋转和保存页面。
string sourceFile ="source file";
Stream openFile = new FileStream(sourceFile, FileMode.Open);
DocumentResult result = new DocumentHandler().RotatePages(openFile, RotateMode.Rotate270);
Stream documentStream = result.Stream;
//output file
var fileStream = File.Create("output path" + "OutPut." + result.FileFormat);
documentStream.CopyTo(fileStream);
documentStream.Close();