PdfSharp:旋转文档中的页面

发布于 2024-12-20 23:43:28 字数 1500 浏览 1 评论 0原文

我正在创建一个网络应用程序,用于在我们的数据库中维护 PDF 文档,当我们接收传真时,这些文档通过自动化系统传入。用户需要能够审阅这些文档,而他们需要能够对这些文档执行的最常见的操作之一就是当它们以错误的方式放入传真机时翻转/旋转各个页面。这几乎总是页面旋转 180 度。我创建了一个函数来执行此操作,该函数似乎有效,但仅在第一次调用时有效。对该函数的任何后续调用似乎都不再起作用。与此函数相关的另一个奇怪的事情是,我还有另一个被调用的方法,该方法将在选定位置的文档中添加一些文本。我传入文本和一些坐标,它会在文档中的这些坐标处写入文本,一切都很好。这样做的问题是,文档旋转之后(它旋转的一次),如果用户尝试在文档中的某处添加文本,它似乎会反转它放置文本的坐标,并且文本是颠倒的。

所有这些都告诉我,最终,我以某种方式进行了错误的页面旋转。我似乎找不到任何关于如何以正确方式在 PdfSharp 文档中旋转页面的好示例,因此一些指导将非常有帮助,并且非常感谢。提前致谢。

这是我当前用于旋转页面并向页面添加文本的代码:

// 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));
}

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. 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. 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. 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));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜夜流光相皎洁 2024-12-27 23:43:28

这不是我的专业领域,但不是“page.Rotate = 180;”我会尝试类似的方法:(

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

代码未测试,可能需要强制转换)

添加文本时,您可以测试“page.Rotate == 180”并使用不同的代码(例如“graphics.RotateTransform(180)”) 。

That's not my area of expertise, but instead of "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)").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文