我正在使用 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;
}