Java編輯PDF寫入文字 插入圖片

來源:互聯網
上載者:User

標籤:name   rgb   操作   idt   position   ring   hello   not   exce   

package com.test;import com.itextpdf.text.BaseColor;import com.itextpdf.text.Font;import com.itextpdf.text.Image;import com.itextpdf.text.pdf.*;import org.junit.Test;import java.io.FileOutputStream;/** *編輯PDF */public class PDFTest {    @Test    public void testPDF(){        generatePDF("D:/1.pdf","D:/2.pdf","D:1.png");    }    /**     * 產生新的PDF     * @param pdfPath 要編輯的PDF路徑     * @param newPDFPath 產生新的PDF路徑     * @param imagePath 插入圖片路徑     */    public void generatePDF(String pdfPath,String newPDFPath,String imagePath){        try{            //建立一個pdf讀入流            PdfReader reader = new PdfReader(pdfPath);            //根據一個pdfreader建立一個pdfStamper.用來產生新的pdf.            PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(newPDFPath));            //這個字型是itext-asian.jar中內建的 所以不用考慮作業系統環境問題.            BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);            //baseFont不支援字型樣式設定.但是font字型要求作業系統支援此字型會帶來移植問題.            Font font = new Font(bf,10);            font.setStyle(Font.BOLD);            font.getBaseFont();            PdfContentByte over;            //頁數是從1開始的            for (int i=1; i<=reader.getNumberOfPages(); i++){                //獲得pdfstamper在當前頁的上層列印內容.也就是說 這些內容會覆蓋在原先的pdf內容之上.                over = stamper.getOverContent(i);                //當前頁的下層列印內容  按自己需求選擇                //over = stamper.getUnderContent(i);                //用pdfreader獲得當前頁字典對象.包含了該頁的一些資料.比如該頁的座標軸資訊.                PdfDictionary p = reader.getPageN(i);                //拿到mediaBox 裡面放著該頁pdf的大小資訊.                PdfObject po =  p.get(new PdfName("MediaBox"));                //po是一個數組對象.裡麵包含了該頁pdf的座標軸範圍.                PdfArray pa = (PdfArray) po;                //開始寫入文本                over.beginText();                //設定字型和大小                over.setFontAndSize(font.getBaseFont(), 90);                //設定字型顏色                over.setColorFill(new BaseColor(0,110,107,100));                com.itextpdf.text.pdf.PdfGState gState = new PdfGState();                gState.setStrokeOpacity(0.1f);                over.setGState(gState);                //要輸出的text          對齊          寫的字        設定字型的輸出位置  字型是否旋轉                over.showTextAligned(0,"HELLO WORLD",0,100,100);                over.endText();                //建立一個image對象.                Image image = Image.getInstance(imagePath);                //設定image對象的輸出位置pa.getAsNumber(pa.size()-1).floatValue() 是該頁pdf座標軸的y軸的最大值  0, 0, 841.92, 595.32                image.setAbsolutePosition(0,pa.getAsNumber(pa.size()-1).floatValue());                //設定插入的圖片大小                image.scaleToFit(50,50);                over.addImage(image);                //畫一個圈.                over.setRGBColorStroke(0xFF, 0x00, 0x00);                over.setLineWidth(5f);                over.ellipse(250, 450, 350, 550);                over.stroke();            }            stamper.close();        }catch (Exception e){            e.printStackTrace();        }    }}
PDF開發包:http://pdfbox.apache.org/

Java編輯PDF寫入文字 插入圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.