使用 itext 将编辑后的 ​​pdf 页面保存为 A4 尺寸

发布于 2024-12-11 02:48:39 字数 2624 浏览 0 评论 0原文

我需要编辑 PDF 文件中的特定页面,并且只能将该页面保存为单独的 PDF 文件。我已经成功地完成了工作。输入文件是信纸大小的页面。但现在我需要的是将编辑后的页面保存为A4尺寸。谁能帮助我。我正在等待答复。

以下是我的代码。

import java.awt.Color;

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

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

public class pdfEdit {

    private static String INPUTFILE = "./pdf/eng.pdf";
    private static String OUTPUTFILE = "./pdf/output.pdf";
    private static String footerRight = "Web content and services";
    private static String footerLeft = "All Rigths Reserved";
    private static Document document;

    public static void main(String[] args) throws IOException, DocumentException {

        document = new Document();      
        document.open();

        PdfReader reader = new PdfReader(INPUTFILE);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(OUTPUTFILE));      

        int editingPage = 3;

        PdfContentByte cb = stamper.getOverContent(editingPage);
        cb.rectangle(10, 10, 550, 30);
        cb.setRGBColorFill(255, 255, 255);
        cb.fill();

        PdfContentByte cByte = stamper.getOverContent(editingPage);
        editFooterText(cByte);
        reader.selectPages(Integer.toString(editingPage));

        stamper.close();
        document.close();
    }


    private static void editFooterText(PdfContentByte cByte) throws DocumentException {

        Font footerFont = new Font(Font.HELVETICA, 5f, Font.NORMAL, Color.BLACK);

        ColumnText cTextLeft = new ColumnText(cByte);
        Paragraph leftPara = new Paragraph();
        cTextLeft.setAlignment(Element.ALIGN_LEFT);
        cTextLeft.setSimpleColumn(document.left(), 10, 500, document.bottom());
        Chunk strFooterLeft = new Chunk(footerLeft, footerFont);
        leftPara.add(strFooterLeft);
        cTextLeft.addElement(leftPara);
        cTextLeft.go();

        ColumnText cTextRight = new ColumnText(cByte);
        cTextRight.setSimpleColumn(document.left(), 10, 430, document.bottom());
        Paragraph Rightpara = new Paragraph();
        Chunk strfooterRight = new Chunk(footerRight, footerFont);
        Rightpara.setAlignment(Element.ALIGN_RIGHT);
        Rightpara.add(strfooterRight);
        cTextRight.addElement(Rightpara);
        cTextRight.go();
    }       
}

I need to edit a specific page from a PDF file and I have to save that page only as a separate PDF file. I have successfully completed the job. The input file is of letter size pages. But now what I need is to save the edited page in A4 size. Can anyone help me. I am waiting for the responses.

Following is my code.

import java.awt.Color;

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

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

public class pdfEdit {

    private static String INPUTFILE = "./pdf/eng.pdf";
    private static String OUTPUTFILE = "./pdf/output.pdf";
    private static String footerRight = "Web content and services";
    private static String footerLeft = "All Rigths Reserved";
    private static Document document;

    public static void main(String[] args) throws IOException, DocumentException {

        document = new Document();      
        document.open();

        PdfReader reader = new PdfReader(INPUTFILE);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(OUTPUTFILE));      

        int editingPage = 3;

        PdfContentByte cb = stamper.getOverContent(editingPage);
        cb.rectangle(10, 10, 550, 30);
        cb.setRGBColorFill(255, 255, 255);
        cb.fill();

        PdfContentByte cByte = stamper.getOverContent(editingPage);
        editFooterText(cByte);
        reader.selectPages(Integer.toString(editingPage));

        stamper.close();
        document.close();
    }


    private static void editFooterText(PdfContentByte cByte) throws DocumentException {

        Font footerFont = new Font(Font.HELVETICA, 5f, Font.NORMAL, Color.BLACK);

        ColumnText cTextLeft = new ColumnText(cByte);
        Paragraph leftPara = new Paragraph();
        cTextLeft.setAlignment(Element.ALIGN_LEFT);
        cTextLeft.setSimpleColumn(document.left(), 10, 500, document.bottom());
        Chunk strFooterLeft = new Chunk(footerLeft, footerFont);
        leftPara.add(strFooterLeft);
        cTextLeft.addElement(leftPara);
        cTextLeft.go();

        ColumnText cTextRight = new ColumnText(cByte);
        cTextRight.setSimpleColumn(document.left(), 10, 430, document.bottom());
        Paragraph Rightpara = new Paragraph();
        Chunk strfooterRight = new Chunk(footerRight, footerFont);
        Rightpara.setAlignment(Element.ALIGN_RIGHT);
        Rightpara.add(strfooterRight);
        cTextRight.addElement(Rightpara);
        cTextRight.go();
    }       
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单 2024-12-18 02:48:39

尝试:reader.getPageN(Integer.toString(editingPage)).put(PdfName.MEDIABOX, new PdfRectangle(612,842));

try: reader.getPageN(Integer.toString(editingPage)).put(PdfName.MEDIABOX, new PdfRectangle(612,842));

心如荒岛 2024-12-18 02:48:39
private static void createPDFFile() throws FileNotFoundException {
        OutputStream outputStream = new FileOutputStream("./A4SizePdf.pdf");
        Document document = new Document(PageSize.A4, 40, 40, 40, 40);
        try {

            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            PdfReader reader = new PdfReader(./InputPdf.pdf);
            reader.setViewerPreferences(editedPageNo);

            PdfImportedPage page = writer.getImportedPage(reader, 1);
            document.open();                

            PdfContentByte cb = writer.getDirectContent();
            cb.addTemplate(page, 1.4f, 0, 0, 1.19f, -13, 7);
            document.close();
            outputStream.close();           
            setFooter();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document.isOpen())
                document.close();
            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }

    }
private static void createPDFFile() throws FileNotFoundException {
        OutputStream outputStream = new FileOutputStream("./A4SizePdf.pdf");
        Document document = new Document(PageSize.A4, 40, 40, 40, 40);
        try {

            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            PdfReader reader = new PdfReader(./InputPdf.pdf);
            reader.setViewerPreferences(editedPageNo);

            PdfImportedPage page = writer.getImportedPage(reader, 1);
            document.open();                

            PdfContentByte cb = writer.getDirectContent();
            cb.addTemplate(page, 1.4f, 0, 0, 1.19f, -13, 7);
            document.close();
            outputStream.close();           
            setFooter();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document.isOpen())
                document.close();
            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文