旋转在C#中使用iTextSharp的PDF 90度 [英] Rotating PDF 90 degrees using iTextSharp in C#

查看:3185
本文介绍了旋转在C#中使用iTextSharp的PDF 90度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PDF冲压,需要旋转90度正确地把它放到?任何人都知道如何做到这一点?似乎无法在网上找到它。


解决方案

Rotate90Degrees 例子使用 PdfReader 来获取文档的一个实例,然后更改 /旋转在每一页字典值。如果没有这样的条目,一个 /旋转与值输入 90 添加:



<预类=郎-java的prettyprint-覆盖> 最后PdfReader读卡器=新PdfReader(源);
最终诠释pagesCount = reader.getNumberOfPages();

为(INT N = 1; N< = pagesCount; N ++){
最终PdfDictionary页= reader.getPageN(N);
最终PdfNumber旋转= page.getAsNumber(PdfName.ROTATE);
最终诠释旋转=
旋转== NULL? 90:(rotate.intValue()+ 90)%360;

page.put(PdfName.ROTATE,新PdfNumber(旋转));
}



一旦做到这一点,我们使用了 PdfStamper 来持续变化:

  PdfStamper模子=新PdfStamper(阅读器,新的FileOutputStream(DEST)); 
stamper.close();
reader.close();

这是iText的Java的。对于iTextSharp的,移植到Java的C#是容易的术语是相同的。改变一些较低的情况下,为上情况是这样的:

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

有在这个职位的问题的一部分或多或少相同的代码片段:的How而不在ghostscript的错误造成与旋转PDF iTextSharp的页面?


I am trying to use a PDF for stamping and need to rotate it 90 degrees to lay it on correctly? Anyone know how to do this? Can't seem to find it online.

解决方案

The Rotate90Degrees example uses PdfReader to get an instance of the document then changes the /Rotate value in every page dictionary. If there is no such entry, a /Rotate entry with value 90 is added:

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

Once this is done, we use a PdfStamper to persist the change:

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

This is for iText Java. For iTextSharp, porting Java to C# is easy as the terminology is identical. Change some lower cases into upper cases like this:

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

There's a more or less identical code snippet in the question part of this post: How to rotate PDF page with iTextSharp without causing error in ghostscript?

这篇关于旋转在C#中使用iTextSharp的PDF 90度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆