前不久使用Freemarker+Flying sauser +Itext 整合生成PDF,因打印要求,需要旋转PDF里的内容,使用过Pdf-renderer, PDFBox 和JPedal转PDF为图片再旋转图片的方式,效果不佳,被项目组否掉,几经周折,终于找到真正旋转PDF内容的方法。 方法如下:
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfSmartCopy;
public class PdfRotate {
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("d:\\1.pdf"); // 读取源文件
Document document = new Document(); // 建立文档
/*
切勿将源文件和输出文件使用一个路径,否则会出现异常:
Exception in thread "main" java.io.FileNotFoundException: d:\1.pdf
(请求的操作无法在使用用户映射区域打开的文件上执行。)
*/
PdfCopy p = new PdfSmartCopy(document,new FileOutputStream("d:\\2.pdf")); // 生成的目标PDF文件
document.open();
int n = reader.getNumberOfPages(); // 获取源文件的页数
PdfDictionary pd;
for(int j=1;j |