如何使用itext更改现有的pdf pagesize?

时间:2015-06-04 10:19:43

标签: itext

我有一个pdf,但在当前页面之外,有些内容没有显示。我想更改pagesize,以便显示所有内容。有没有办法用itext做到这一点?

2 个答案:

答案 0 :(得分:2)

public PdfReader changePDFPageSize(String inpdf,String outpdf,float vertical,float horizontal)
{
    try 
    {
        PdfReader reader = new PdfReader(inpdf);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outpdf));
        for (int curPageNum = 1; curPageNum <= reader.getNumberOfPages(); ++curPageNum) {
            PdfDictionary pagedict = reader.getPageN(curPageNum);
            PdfArray mediabox = pagedict.getAsArray(PdfName.MEDIABOX);
            mediabox.set(0, new PdfNumber(mediabox.getAsNumber(0).intValue()-horizontal));//left add
            mediabox.set(1, new PdfNumber(mediabox.getAsNumber(1).intValue()-vertical));//down 
            mediabox.set(2, new PdfNumber(mediabox.getAsNumber(2).intValue()+horizontal));//right
            mediabox.set(3, new PdfNumber(mediabox.getAsNumber(3).intValue()+vertical));//up
        }

        stamper.close();

        return  new PdfReader(outpdf);
    } catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    } catch (DocumentException e)
    {
        e.printStackTrace();
    } catch (IOException e) 
    {
        e.printStackTrace();
    }
    return null;
}

答案 1 :(得分:0)

itextsharp VB.NET

    Dim objReader As PdfReader
    Dim objStream As FileStream
    Dim objStamper As PdfStamper
    Dim objContent As PdfContentByte
    Dim objImport As PdfImportedPage
    Dim objMark As Image

    objReader = New PdfReader(strBookPath)
    objStream = New FileStream(strTempPath, FileMode.Create)
    objStamper = New PdfStamper(objReader, objStream)

    objContent = objStamper.GetOverContent(1)
    objImport = objStamper.GetImportedPage(objReader, 1)
    objContent.AddTemplate(objImport, PageSize.A4.Width / objImport.Width, 0, 0, PageSize.A4.Height / objImport.Height, 0, 0)
    objReader.GetPageN(1).Put(PdfName.CROPBOX, New PdfRectangle(PageSize.A4.Width, PageSize.A4.Height))
    objReader.GetPageN(1).Put(PdfName.MEDIABOX, New PdfRectangle(PageSize.A4.Width, PageSize.A4.Height))