简体   繁体   English

PdfSharp:旋转文档中的页面

[英]PdfSharp: Rotating a Page in a document

I'm creating a web app that will be used for maintaining PDF documents in our database that come in through an automated system when we receive faxes. 我正在创建一个Web应用程序,该应用程序将用于维护数据库中的PDF文档,当我们接收传真时,这些PDF文档是通过自动系统输入的。 Users need to be able to review these documents, and one of the more common things they need to be able to do with these documents is flip/rotate individual pages when they were put into the fax machine the wrong way. 用户需要能够查看这些文档,而他们需要对这些文档进行处理的更常见的事情之一就是当将它们以错误的方式放入传真机时翻转/旋转各个页面。 This is almost always a 180 degree rotation of the page. 这几乎总是页面旋转180度。 I've created a function to do this, which seems to work, but only the first time its called. 我已经创建了一个函数来执行此操作,该函数似乎可以运行,但仅在第一次调用时才起作用。 Any subsequent calls to this function don't seem to work anymore. 此函数的任何后续调用似乎都不再起作用。 Another strange thing associated with this function, is that I've also got another method that gets called that will add some text to the document at selected locations. 与该函数相关的另一个奇怪的事情是,我还调用了另一个方法,该方法将在选定位置的文档中添加一些文本。 I pass in the text and some coordinates, and it writes the text at those coordinates in the document, and all is well. 我传入文本和一些坐标,然后将文本写在文档中的那些坐标处,一切都很好。 The problem with this is, after the document is rotated (the one time it will rotate), if a user tries to add text to the document somewhere, it seems to reverse the coordinates it places the text at, and the text is upside down. 这里的问题是,该文件被旋转 (在一个时间它旋转),如果用户试图将文本添加到文档中的某个地方,似乎扭转它放置在文字的坐标,文字倒置。

All of this tells me that, ultimately, I'm doing the page rotation wrong somehow. 所有这些告诉我,最终,我以某种方式做错了页面旋转。 I can't seem to find any good samples of how to rotate a page in a PdfSharp document the correct way, so some guidance would be tremendously helpful and very much appreciated. 我似乎无法找到如何在PdfSharp文件以正确的方式旋转页面任何好的样本,所以一些指导,将大大有益的,非常感谢。 Thanks in advance. 提前致谢。

Here's the code I'm currently using for rotating pages, and adding text to pages: 这是我当前用于旋转页面以及向页面添加文本的代码:

// This is how I'm rotating the page...
public PdfDocument FlipPage(byte[] documentSource, int pageNumber)
{
    using (var stream = new MemoryStream())
    {
        stream.Write(documentSource, 0, documentSource.Length);
        var document = PdfReader.Open(stream);
        var page = document.Pages[pageNumber - 1];
        page.Rotate = 180;

        return document;
    }
}

// This is how I'm adding text to a page...
public static void AddTextToPage(this PdfDocument document, int pageNumber, Annotation annotation)
{
    var page = document.Pages[pageNumber - 1];
    annotation.TargetHeight = page.Height.Value;
    annotation.TargetWidth = page.Width.Value;

    var graphics = XGraphics.FromPdfPage(page);
    var textFormatter = new XTextFormatter(graphics);
    var font = new XFont("Arial", 10, XFontStyle.Regular);
    graphics.DrawString(annotation.Text, font, XBrushes.Red, new PointF((float)annotation.TargetX, (float)annotation.TargetY));
}

That's not my area of expertise, but instead of "page.Rotate = 180;" 那不是我的专业领域,而是“ page.Rotate = 180;”。 I'd try something like that: 我会尝试这样的事情:

page.Rotate = (page.Rotate + 180) % 360;

(code not tested, may require a cast) (代码未经测试,可能需要强制转换)

When adding text, you can test for "page.Rotate == 180" and use different code then (like "graphics.RotateTransform(180)"). 添加文本时,可以测试“ page.Rotate == 180”,然后使用其他代码(例如“ graphics.RotateTransform(180)”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM