我正在使用java中的iTextPdf从其他pdf文件创建合并的pdf。某些PDF文件的页面大小非常大(3200x2432)。因此,当用户滚动文档时,较小的页面看起来像缩略图。
如何缩放PdfImported页面以确保它们的大小相同?
TIA
解决:切换到PdfWriter而不是PdfCopy。
新代码:
public static boolean CollecPdfs(Vector<File> pdfFiles, OutputStream pdfOutputStream)
{
try
{
Rectangle r=PageSize.LETTER;
// create intermediate output pdf in memory for new pages
Document odoc=new Document(r);
PdfWriter writer=PdfWriter.getInstance(odoc, pdfOutputStream);
odoc.open();
// add documents
for(File pdfFile : pdfFiles)
{
PdfReader reader=null;
try
{
FileInputStream is=new FileInputStream(pdfFile);
reader=new PdfReader(is);
int pgs=reader.getNumberOfPages();
PdfContentByte canvas=writer.getDirectContent();
for(int i=0; i<pgs;)
{
PdfImportedPage pg=writer.getImportedPage(reader, ++i);
float ws=PageSize.LETTER.getWidth()/pg.getWidth();
float hs=PageSize.LETTER.getHeight()/pg.getHeight();
canvas.addTemplate(pg,
ws, 0, 0,
hs, 0, 0);
odoc.newPage();
}
}
catch(ClientAbortException cex)
{
}
}
odoc.close();
}
catch(Exception e)
{
ExceptionRpt.sendStackTrace(e);
return false;
}
return true;
}
旧代码:
public static boolean CollecPdfs(Vector<File> pdfFiles, OutputStream pdfOutputStream)
{
try
{
Rectangle r=PageSize.LETTER;
Document odoc=new Document(r);
PdfCopy copy=new PdfCopy(odoc, pdfOutputStream);
odoc.open();
for(File pdfFile : pdfFiles)
{
PdfReader reader=null;
try
{
FileInputStream is=new FileInputStream(pdfFile);
reader=new PdfReader(is);
int pgs=reader.getNumberOfPages();
for(int i=0; i<pgs;)
{
PdfImportedPage pg=copy.getImportedPage(reader, ++i);
copy.addPage(pg);
}
}
catch(ClientAbortException cex)
{
}
copy.freeReader(reader);
}
odoc.close();
copy.close();
}
catch(Exception e)
{
ExceptionRpt.sendStackTrace(e);
return false;
}
return true;
}
答案 0 :(得分:1)
您需要在PDF页面属性中进行以下更改:
单页 显示 - 适合页面
答案 1 :(得分:0)
我也有同样的问题。经过一番研究,我终于解决了这个问题。
FileInputStream pdfInput = new FileInputStream(pdf);
PdfReader pdfReader = new PdfReader(pdfInput);
for (int index = 1; index <= pdfReader.getNumberOfPages(); index++) {
main.newPage();
PdfImportedPage page = pdfWriter.getImportedPage(pdfReader,
index);
Rectangle pagesize = pdfReader.getPageSizeWithRotation(index);
float oWidth = pagesize.getWidth();
float oHeight = pagesize.getHeight();
float scale = getScale(oWidth, oHeight);
float scaledWidth = oWidth * scale;
float scaledHeight = oHeight * scale;
int rotation = pdfReader.getPageRotation(index);
AffineTransform transform = new AffineTransform(scale, 0, 0, scale, 0, 0);
switch (rotation) {
case 0:
cb.addTemplate(page, transform);
break;
case 90:
AffineTransform rotate90 = new AffineTransform(0, -1f, 1f, 0, 0, scaledHeight);
rotate90.concatenate(transform);
cb.addTemplate(page, rotate90);
break;
case 180:
AffineTransform rotate180 = new AffineTransform(-1f, 0, 0, -1f, scaledWidth,
scaledHeight);
rotate180.concatenate(transform);
cb.addTemplate(page, rotate180);
break;
case 270:
AffineTransform rotate270 = new AffineTransform(0, 1f, -1f, 0, scaledWidth, 0);
rotate270.concatenate(transform);
cb.addTemplate(page, rotate270);
break;
default:
cb.addTemplate(page, scale, 0, 0, scale, 0, 0);
}
}
private static float getScale(float width, float height) {
float scaleX = PageSize.A4.getWidth() / width;
float scaleY = PageSize.A4.getHeight() / height;
return width > height ? Math.min(scaleX, scaleY) : Math.max(scaleX,
scaleY);
}