加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用C#中的iTextSharp将PDF旋转90度

发布时间:2020-12-15 08:23:58 所属栏目:百科 来源:网络整理
导读:我正在尝试使用PDF进行冲压,需要将其旋转90度才能正确铺设?有人知道怎么做吗?似乎无法在网上找到它. 解决方法 Rotate90Degrees示例使用PdfReader获取文档的实例,然后更改每个页面字典中的/ Rotate值.如果没有此类条目,则添加值为90的/ Rotate条目: final
我正在尝试使用PDF进行冲压,需要将其旋转90度才能正确铺设?有人知道怎么做吗?似乎无法在网上找到它.

解决方法

Rotate90Degrees示例使用PdfReader获取文档的实例,然后更改每个页面字典中的/ Rotate值.如果没有此类条目,则添加值为90的/ Rotate条目:
final PdfReader reader = new PdfReader(source);
final int pagesCount = reader.getNumberOfPages();

for (int n = 1; n <= pagesCount; n++) {
    final PdfDictionary page = reader.getPageN(n);
    final PdfNumber rotate = page.getAsNumber(PdfName.ROTATE);
    final int rotation =
            rotate == null ? 90 : (rotate.intValue() + 90) % 360;

    page.put(PdfName.ROTATE,new PdfNumber(rotation));
}

完成后,我们使用PdfStamper来保持更改:

PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(dest));
stamper.close();
reader.close();

这适用于iText Java.对于iTextSharp,将Java移植到C#很容易,因为术语是相同的.将一些小案例改为大写,如下所示:

PdfDictionary page = reader.GetPageN(1);
page.Put(PdfName.ROTATE,new PdfNumber(90));

在这篇文章的问题部分中有一个或多或少相同的代码片段:How to rotate PDF page with iTextSharp without causing error in ghostscript?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读