Rotating PDF 90 degrees using iTextSharp in C#

こ雲淡風輕ζ 提交于 2019-11-28 03:45:10

问题


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.


回答1:


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?



来源:https://stackoverflow.com/questions/27020542/rotating-pdf-90-degrees-using-itextsharp-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!