如何将 PDF 页面(永久)旋转到 90、180、270 度 c#

How to rotate PDF pages (permanent) to 90, 180, 270 degree c#

旋转页面时不应将其转换为图像,从而消除在 pdf 文档中的搜索。

using WebSupergoo.ABCpdf9;
Doc doc = new Doc();
byte[] source = // some source of pdf byte array
doc.Read(source);
for (int i = 1; i <= doc.PageCount; i++)
{
     doc.PageNumber = i;
     double w = doc.MediaBox.Width;
     double h = doc.MediaBox.Height;
     double l = doc.MediaBox.Left;
     double b = doc.MediaBox.Bottom;
     doc.Transform.Rotate(90, l, b);
     doc.Transform.Translate(w, 0);

     // rotate our rectangle
     doc.Rect.Width = h;
     doc.Rect.Height = w;
}
MemoryStream stream = new MemoryStream();
doc.Save(stream); 

上面的代码似乎不起作用。请适当建议。

我尝试使用 itextsharp 库来实现它。

            byte[] source = // some source of pdf byte array
            MemoryStream outStream = new MemoryStream();
            PdfReader reader = new PdfReader(scannedInvoice.imgImage);
            PdfStamper stamper = new PdfStamper(reader, outStream);

            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                PdfDictionary pageDict = reader.GetPageN(i);
                int desiredRot = 90; // 90 degrees clockwise
                PdfNumber rotation = pageDict.GetAsNumber(PdfName.ROTATE);
                if (rotation != null)
                {
                    desiredRot += rotation.IntValue;
                    desiredRot %= 360; // 0, 90, 180, 270
                }
                pageDict.Put(PdfName.ROTATE, new PdfNumber(desiredRot));
            }
            stamper.Close();
            var rotatedpdfArray = outStream.ToArray(); // The rotated output

希望对您有所帮助......