简体   繁体   English

使用Java IText为现有的PDF文档设置新页面大小

[英]Setting new page size to existing PDF document using Java IText

I want to set a new page size to my existing PDF document without cropping the contents. 我想为现有的PDF文档设置新的页面大小,而不会裁剪内容。 I am writing the following code but it just crops my PDF file from the bottom resulting in the loss of content. 我正在编写以下代码,但它只是从底部开始裁剪我的PDF文件,从而导致内容丢失。

The current size is 8.26" X 11.69 " and I need to make it 8.5" X 11". 当前大小为8.26英寸X 11.69英寸,我需要使其为8.5英寸X 11英寸。

My code only converts 11.59 to 11. Tried to change 8.26 but its not woking. 我的代码仅将11.59转换为11。试图更改8.26,但无法正常工作。

Can any one please help? 有人可以帮忙吗? I am using itextpdf-5.5.8 with Java. 我正在Java中使用itextpdf-5.5.8。

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfContentByte;
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.PdfRectangle;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;



public class PageSize {

    public static final String SRC = "C:/Temp/BC.pdf";
    public static final String DEST = "C:/Temp/BC_New.pdf";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new PageSize().manipulatePdf(SRC, DEST);
    }

    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        int n = reader.getNumberOfPages();
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        // properties
        PdfContentByte over;
        PdfDictionary pageDict;
        PdfRectangle rect = new PdfRectangle(55, 76, 560, 816);
        PdfArray mediabox;
        float llx, lly, ury,llz;
        // loop over every page
        for (int i = 1; i <= n; i++) {
            pageDict = reader.getPageN(i);
            mediabox = pageDict.getAsArray(PdfName.MEDIABOX);
            llx = mediabox.getAsNumber(0).floatValue();
            lly = mediabox.getAsNumber(1).floatValue();
            llz = mediabox.getAsNumber(2).floatValue();
            ury = mediabox.getAsNumber(3).floatValue();
            mediabox.set(1, new PdfNumber((lly + 50)));
            over = stamper.getOverContent(i);
            over.saveState();
                       over.restoreState();
        }



        stamper.close();
        reader.close();
    }
}

UPDATED Here is the code I used to reduce 11.69" to 11". 更新这是我用来将11.69“减少到11”的代码。 It works fine. 工作正常。 But, it won't increase the width 8.26 to 8.5" 但是,它不会将宽度8.26增加到8.5“

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfContentByte;
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.PdfRectangle;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;



public class PageSize {

    public static final String SRC = "C:/Temp/Test.pdf";
    public static final String DEST = "C:/Temp/BC_New.pdf";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new PageSize().manipulatePdf(SRC, DEST);
    }

    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        int n = reader.getNumberOfPages();
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        // properties
        PdfContentByte over;
        PdfDictionary pageDict;
        PdfArray mediabox;
        float llx, lly, ury,llz;
        // loop over every page
        for (int i = 1; i <= n; i++) {
            pageDict = reader.getPageN(i);
            mediabox = pageDict.getAsArray(PdfName.MEDIABOX);
            llx = mediabox.getAsNumber(0).floatValue();
            lly = mediabox.getAsNumber(1).floatValue();
            ury = mediabox.getAsNumber(3).floatValue();
            mediabox.set(0, new PdfNumber((llx - 17)));
            mediabox.set(1, new PdfNumber((lly + 50)));
            over = stamper.getOverContent(i);
            over.saveState();
                       over.restoreState();
        }



        stamper.close();
        reader.close();
    }
}

SOLVED: 解决了:

It was a problem with the source file which I was trying to modify. 我试图修改的源文件有问题。 The original source file was a 8.26" X 11.69". 原始源文件是8.26“ X 11.69”。 A hava program adds a logo to the file and saves it into a new file. hava程序会将徽标添加到文件中,并将其保存到新文件中。

Somehow due to this conversion, the new source file's X-axis got locked and hence my program was not able to modify the x axis. 由于某种原因,这种转换导致新源文件的X轴被锁定,因此我的程序无法修改X轴。

Hence, I move my program as the first step and it worked. 因此,我将我的程序作为第一步,它可以正常工作。 My program first modifies the x and y axis and then passes the file to the other program which does the logo adding stuff. 我的程序首先修改x和y轴,然后将文件传递给另一个进行徽标添加的程序。

Thanks Bruno on this. 谢谢布鲁诺。 :) :)

You are changing the page size without changing the content. 您正在更改页面大小而不更改内容。

If you want to shrink the content, then you need code that is more complex. 如果要缩小内容,则需要更复杂的代码。 See Shrinking the contents of a pdf page for an example. 有关示例,请参见收缩pdf页面的内容 There's also an example in the official documentation: How to shrink pages in an existing PDF? 官方文档中还有一个示例: 如何缩小现有PDF中的页面?

PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
int n = reader.getNumberOfPages();
PdfDictionary page;
PdfArray crop;
PdfArray media;
for (int p = 1; p <= n; p++) {
    page = reader.getPageN(p);
    media = page.getAsArray(PdfName.CROPBOX);
    if (media == null) {
        media = page.getAsArray(PdfName.MEDIABOX);
    }
    crop = new PdfArray();
    crop.add(new PdfNumber(0));
    crop.add(new PdfNumber(0));
    crop.add(new PdfNumber(media.getAsNumber(2).floatValue() / 2));
    crop.add(new PdfNumber(media.getAsNumber(3).floatValue() / 2));
    page.put(PdfName.MEDIABOX, crop);
    page.put(PdfName.CROPBOX, crop);
    stamper.getUnderContent(p).setLiteral("\nq 0.5 0 0 0.5 0 0 cm\nq\n");
    stamper.getOverContent(p).setLiteral("\nQ\nQ\n");
}
stamper.close();
reader.close();

If you want to enlarge the content, then you can simply change the UserUnit. 如果要放大内容,则只需更改UserUnit。 Changing the UserUnit is also explained in the official documentation: How to rotate and scale pages in an existing PDF? 官方文档中还介绍了更改UserUnit的方法: 如何旋转和缩放现有PDF中的页面?

float factor = 2.5f;
PdfReader reader = new PdfReader(src);
int n = reader.getNumberOfPages();
PdfDictionary page;
for (int p = 1; p <= n; p++) {
    page = reader.getPageN(p);
    if (page.getAsNumber(PdfName.USERUNIT) == null)
        page.put(PdfName.USERUNIT, new PdfNumber(factor));
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
reader.close();

This code will scale up the page and its content with a factor 2.5 (because that's what we defined as factor ). 这段代码将页面及其内容按2.5比例缩放(因为这就是我们定义的factor )。

UPDATE 更新

In your update, you have this: 在您的更新中,您有:

    mediabox = pageDict.getAsArray(PdfName.MEDIABOX);
    llx = mediabox.getAsNumber(0).floatValue();
    lly = mediabox.getAsNumber(1).floatValue();
    ury = mediabox.getAsNumber(3).floatValue();
    mediabox.set(0, new PdfNumber((llx - 17)));
    mediabox.set(1, new PdfNumber((lly + 50)));

By doing so, you add 0.236111 inches in x direction ( llx -17 ) and you subtract 0.69445 inches in y direction. 这样做,您在x方向( llx -17 )加0.236111英寸,而在y方向减0.69445英寸。 If you say that this changes 11.69 to 11 inch, then I assume that you're talking about the Y direction. 如果您说这将11.69更改为11英寸,那么我假设您是在谈论Y方向。 If the original width in X direction was originally 8.26, it should be about 8.50. 如果X方向的原始宽度最初为8.26,则应为8.50左右。

I don't understand your problem. 我不明白你的问题。 Of course: my calculations assume that there's no CropBox in your document. 当然:我的计算假设您的文档中没有CropBox。 Is there a CropBox present? 有没有CropBox?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM