我希望连接现有的pdf,并将它们缩放并旋转到A4纵向页面。今天我用pdfWriter完成了这个:
for (int pageNum = 1; pageNum <= numberOfPages; pageNum++) {
PdfImportedPage importedPage = writer.getImportedPage( reader, pageNum );
AffineTransform transform = getAffineTransform(reader, writer, pageNum);
pdfContentByte.addTemplate(importedPage, transform);
document.newPage();
}
private AffineTransform getAffineTransform(PdfReader reader, PdfWriter writer, int pageNum) {
Rectangle readerPageSize = reader.getPageSize( pageNum );
Rectangle writerPageSize = writer.getPageSize();
float rPageHeight = readerPageSize.getHeight();
float rPageWidth = readerPageSize.getWidth();
float wPageHeight = writerPageSize.getHeight();
float wPageWidth = writerPageSize.getWidth();
int pageRotation = reader.getPageRotation( pageNum );
boolean rotate = (rPageWidth > rPageHeight) && (pageRotation == 0 || pageRotation == 180);
if(!rotate)
rotate = ((rPageHeight > rPageWidth) && (pageRotation == 90 || pageRotation ==270));
//if changing rotation gives us better space rotate an extra 90 degrees.
if(rotate) pageRotation += 90;
double randrotate = (double)pageRotation * Math.PI/(double)180;
AffineTransform transform = new AffineTransform();
float margin = 0;
float scale = 1.0f;
if(pageRotation == 90 || pageRotation == 270 ){
scale = Math.min((wPageHeight - 2 * margin) / rPageWidth, (wPageWidth- 2 * margin) / rPageHeight);
} else {
scale = Math.min(wPageHeight / rPageHeight, wPageWidth / rPageWidth);
}
transform.translate((wPageWidth/2) + margin, wPageHeight/2 + margin);
transform.rotate(-randrotate);
transform.scale(scale,scale);
transform.translate(-rPageWidth/2,-rPageHeight/2);
return transform;
}
这很好用,但删除了图层(OCG)和注释。 是否可以使用图层(OCG)和注释?这用于打印,所以我只需要pdf来显示它,我实际上并不需要图层或注释。