C# 旋转PDF页面内容,而不是实际页面

C# 旋转PDF页面内容,而不是实际页面,c#,itextsharp,C#,Itextsharp,我对此进行了研究,并尝试旋转一页PDF的内容。我可以将页面旋转90度、180度或270度。我不想旋转页面,而是想旋转内容 以下是我迄今为止采用的方法: public static byte[] RotatePdf(byte[] fileBytes, int degreesClockwise) { if (degreesClockwise % 90 != 0) throw new ApplicationException(string.Format("degreesClo

我对此进行了研究,并尝试旋转一页PDF的内容。我可以将页面旋转90度、180度或270度。我不想旋转页面,而是想旋转内容

以下是我迄今为止采用的方法:

public static byte[] RotatePdf(byte[] fileBytes, int degreesClockwise)
{
    if (degreesClockwise % 90 != 0) 
        throw new ApplicationException(string.Format("degreesClockwise must be 0, 90, 180, 360: {0}", degreesClockwise));

    PdfReader reader = new PdfReader(fileBytes);
    using (var fs = new MemoryStream())
    {
        PdfStamper stamper = new PdfStamper(reader, fs);

        PdfDictionary pageDict = reader.GetPageN(1);
        int desiredRotation = degreesClockwise; // x degrees clockwise from what it is now
        PdfNumber rotation = pageDict.GetAsNumber(PdfName.ROTATE);
        if (rotation != null)
        {
            desiredRotation += rotation.IntValue;
            desiredRotation %= 360; // must be 0, 90, 180, or 270
        }
        pageDict.Put(PdfName.ROTATE, new PdfNumber(desiredRotation));

        stamper.Close();

        return fs.ToArray();
    }
}

如果您有任何建议,我将不胜感激。

我使用PdfSharp库完成了代码,因为我找不到iTextSharp的任何示例或答案

下面是我用来实现我想要的东西的代码:

// Create the output document
PdfDocument outputDocument = new PdfDocument();

// Show single pages
// (Note: one page contains two pages from the source document)
outputDocument.PageLayout = PdfPageLayout.SinglePage;

// Open the external document as XPdfForm object
XPdfForm form = XPdfForm.FromFile(filename);

for (int i = 0; i < form.PageCount; i++)
{
    // Add a new page to the output document
    PdfPage page = outputDocument.AddPage();
    page.Orientation = PageOrientation.Landscape;
    double width = page.Width;
    double height = page.Height;

    int rotate = page.Elements.GetInteger("/Rotate");

    XGraphics gfx = XGraphics.FromPdfPage(page);

    XRect box = new XRect(0, 0, width, height * 2);
    // Draw the page identified by the page number like an image
    gfx.DrawImage(form, box);
}

// Save the document...
filename = "RotatedAndStretched_tempfile.pdf";
outputDocument.Save(filename);
//创建输出文档
PdfDocument outputDocument=新PdfDocument();
//显示单个页面
//(注意:一页包含源文档的两页)
outputDocument.PageLayout=PdfPageLayout.SinglePage;
//将外部文档作为XPdfForm对象打开
XPdfForm form=XPdfForm.FromFile(文件名);
for(int i=0;i
Hm,这个代码怎么不完整?也就是说,它现在实现了什么?这与您想要的有什么不同?我不想旋转页面,而是要旋转内容-有什么不同?@millimoose代码随内容旋转页面方向。我希望方向保持不变,但内容旋转90度。@mkl区别在于我希望页面上的内容旋转,而不是页面本身,即方向旋转或从横向切换到纵向,反之亦然。但这意味着旋转将在一个方向上隐藏一些内容,并在页面中留下一些空白其他的。这就是你的意思吗?除了旋转更改之外,您还可以通过操纵
pageDict
的mediabox和/或cropbox条目来实现这一点。