Editing PDF text using Java

前端 未结 5 1102
天命终不由人
天命终不由人 2020-12-03 13:18

Is there a way I can edit a PDF from Java?
I have a PDF document which contains placeholders for text that I need to be replaced using Java, but all the libraries that

相关标签:
5条回答
  • 2020-12-03 13:58

    Take a look at iText and this sample code

    0 讨论(0)
  • 2020-12-03 13:59

    Take a look at aspose and this sample code

    0 讨论(0)
  • 2020-12-03 14:14

    I modified the code found a bit and it was working as follows

    public class Principal {
    public static final String SRC = "C:/tmp/244558.pdf";
    public static final String DEST = "C:/tmp/244558-2.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new Principal().manipulatePdf(SRC, DEST);
    }
    
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfDictionary dict = reader.getPageN(1);
        PdfObject object = dict.getDirectObject(PdfName.CONTENTS);
        PdfArray refs = null;
        if (dict.get(PdfName.CONTENTS).isArray()) {
            refs = dict.getAsArray(PdfName.CONTENTS);
        } else if (dict.get(PdfName.CONTENTS).isIndirect()) {
            refs = new PdfArray(dict.get(PdfName.CONTENTS));
        }
        for (int i = 0; i < refs.getArrayList().size(); i++) {
            PRStream stream = (PRStream) refs.getDirectObject(i);
            byte[] data = PdfReader.getStreamBytes(stream);
            stream.setData(new String(data).replace("NULA", "Nulo").getBytes());
        }
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        stamper.close();
        reader.close();
    }
    

    }

    0 讨论(0)
  • 2020-12-03 14:22

    You can do limited editing with Itext but PDF is an end file format so you cannot do anything too complex. I wrote an article explaining some of the limitations: PDF format and style information.

    0 讨论(0)
  • 2020-12-03 14:25

    You can do it with iText. I tested it with following code. It adds a chunk of text and a red circle over each page of an existing PDF.

    /* requires itextpdf-5.1.2.jar or similar */
    import java.io.*;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.pdf.*;
    
    public class AddContentToPDF {
    
        public static void main(String[] args) throws IOException, DocumentException {
    
            /* example inspired from "iText in action" (2006), chapter 2 */
    
            PdfReader reader = new PdfReader("C:/temp/Bubi.pdf"); // input PDF
            PdfStamper stamper = new PdfStamper(reader,
              new FileOutputStream("C:/temp/Bubi_modified.pdf")); // output PDF
            BaseFont bf = BaseFont.createFont(
                    BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); // set font
    
            //loop on pages (1-based)
            for (int i=1; i<=reader.getNumberOfPages(); i++){
    
                // get object for writing over the existing content;
                // you can also use getUnderContent for writing in the bottom layer
                PdfContentByte over = stamper.getOverContent(i);
    
                // write text
                over.beginText();
                over.setFontAndSize(bf, 10);    // set font and size
                over.setTextMatrix(107, 740);   // set x,y position (0,0 is at the bottom left)
                over.showText("I can write at page " + i);  // set text
                over.endText();
    
                // draw a red circle
                over.setRGBColorStroke(0xFF, 0x00, 0x00);
                over.setLineWidth(5f);
                over.ellipse(250, 450, 350, 550);
                over.stroke();
            }
    
            stamper.close();
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题