Android中合多图片和文字合成PDF文件---路很长
生活随笔
收集整理的這篇文章主要介紹了
Android中合多图片和文字合成PDF文件---路很长
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一, Android中合多圖片和文字合成PDF文件
如果遇到什么問題可以留言,歡迎你留言,我希望能幫助到你。一直懷著感恩的心,感謝路途遇到過的貴人。
效果圖如下:
這個Demo中實現了圖片和文字,當然單純的文字和圖片都可以合成pdf。這里我只是展示了圖片和文字的合并。文字是寫死的。當然你可以通過editText來進行編輯。
如上圖就是我們項目中的需求:這一篇我先寫pdf的合成。后面關于智能裁剪方面的我會單獨寫一篇博客。
第二:需要技術:
1.pdf編輯jar:
iTextpdf.jar這個百度很多,很多csdn都需要錢的。這里需要的直接到我項目里面下載或者去官網下載都免費的。提供了圖片和文字合成pdf格式文件,以及文件內容的編輯設置,文字居中標題內容等等。以及圖片大小和圖片的寬高比例,pdf的頁面寬度等。都可以讓我們編輯出好看的pdf文件。
**2.pdf文件預覽:**谷歌提供了預覽pdf文件的庫
implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'第三:代碼部分:源碼:
public class Document implements DocListener, IAccessibleElement {public static boolean compress = true;public static boolean plainRandomAccess = false;public static float wmfFontCorrection = 0.86F;protected ArrayList<DocListener> listeners;protected boolean open;protected boolean close;protected Rectangle pageSize;protected float marginLeft;protected float marginRight;protected float marginTop;protected float marginBottom;protected boolean marginMirroring;protected boolean marginMirroringTopBottom;protected String javaScript_onLoad;protected String javaScript_onUnLoad;protected String htmlStyleClass;protected int pageN;protected int chapternumber;protected PdfName role;protected HashMap<PdfName, PdfObject> accessibleAttributes;protected AccessibleElementId id;public Document() {this(PageSize.A4);}/**通過這些構造方法,我想一個pfd的創建隨隨便便來了吧。1.第一個默認的是a4紙的大小。2.第二個是默認距離邊緣left,top,right,bootom大小。3.第三個是設置了很多參數。自己可以看其意思。**/public Document(Rectangle pageSize) {this(pageSize, 36.0F, 36.0F, 36.0F, 36.0F);}public Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom) {this.listeners = new ArrayList();this.marginLeft = 0.0F;this.marginRight = 0.0F;this.marginTop = 0.0F;this.marginBottom = 0.0F;this.marginMirroring = false;this.marginMirroringTopBottom = false;this.javaScript_onLoad = null;this.javaScript_onUnLoad = null;this.htmlStyleClass = null;this.pageN = 0;this.chapternumber = 0;this.role = PdfName.DOCUMENT;this.accessibleAttributes = null;this.id = new AccessibleElementId();this.pageSize = pageSize;this.marginLeft = marginLeft;this.marginRight = marginRight;this.marginTop = marginTop;this.marginBottom = marginBottom;}public void addDocListener(DocListener listener) {this.listeners.add(listener);if (listener instanceof IAccessibleElement) {IAccessibleElement ae = (IAccessibleElement)listener;ae.setRole(this.role);ae.setId(this.id);if (this.accessibleAttributes != null) {Iterator i$ = this.accessibleAttributes.keySet().iterator();while(i$.hasNext()) {PdfName key = (PdfName)i$.next();ae.setAccessibleAttribute(key, (PdfObject)this.accessibleAttributes.get(key));}}}}public void removeDocListener(DocListener listener) {this.listeners.remove(listener);}/**這里的add方法為我們提供了圖文混排的基礎document.add(Element);這里的Element我們可以看源碼:public interface Element {int HEADER = 0;//頭部int TITLE = 1;//標題int SUBJECT = 2;int KEYWORDS = 3;int AUTHOR = 4;int PRODUCER = 5;int CREATIONDATE = 6;int CREATOR = 7;int LANGUAGE = 8;int CHUNK = 10;int PHRASE = 11;int PARAGRAPH = 12;int SECTION = 13;int LIST = 14;int LISTITEM = 15;int CHAPTER = 16;int ANCHOR = 17;int PTABLE = 23;int ANNOTATION = 29;int RECTANGLE = 30;int JPEG = 32;//圖片int JPEG2000 = 33;//圖片int IMGRAW = 34;int IMGTEMPLATE = 35;int JBIG2 = 36;int DIV = 37;int MARKED = 50;int YMARK = 55;int WRITABLE_DIRECT = 666;int ALIGN_UNDEFINED = -1;int ALIGN_LEFT = 0;int ALIGN_CENTER = 1;int ALIGN_RIGHT = 2;int ALIGN_JUSTIFIED = 3;int ALIGN_TOP = 4;int ALIGN_MIDDLE = 5;int ALIGN_BOTTOM = 6;int ALIGN_BASELINE = 7;int ALIGN_JUSTIFIED_ALL = 8;int CCITTG4 = 256;int CCITTG3_1D = 257;int CCITTG3_2D = 258;int CCITT_BLACKIS1 = 1;int CCITT_ENCODEDBYTEALIGN = 2;int CCITT_ENDOFLINE = 4;int CCITT_ENDOFBLOCK = 8;**/public boolean add(Element element) throws DocumentException {if (this.close) {throw new DocumentException(MessageLocalization.getComposedMessage("the.document.has.been.closed.you.can.t.add.any.elements", new Object[0]));} else if (!this.open && element.isContent()) {throw new DocumentException(MessageLocalization.getComposedMessage("the.document.is.not.open.yet.you.can.only.add.meta.information", new Object[0]));} else {boolean success = false;if (element instanceof ChapterAutoNumber) {this.chapternumber = ((ChapterAutoNumber)element).setAutomaticNumber(this.chapternumber);}DocListener listener;for(Iterator i$ = this.listeners.iterator(); i$.hasNext(); success |= listener.add(element)) {listener = (DocListener)i$.next();}if (element instanceof LargeElement) {LargeElement e = (LargeElement)element;if (!e.isComplete()) {e.flushContent();}}return success;}}public void open() {if (!this.close) {this.open = true;}Iterator i$ = this.listeners.iterator();while(i$.hasNext()) {DocListener listener = (DocListener)i$.next();listener.setPageSize(this.pageSize);listener.setMargins(this.marginLeft, this.marginRight, this.marginTop, this.marginBottom);listener.open();}}public boolean setPageSize(Rectangle pageSize) {this.pageSize = pageSize;Iterator i$ = this.listeners.iterator();while(i$.hasNext()) {DocListener listener = (DocListener)i$.next();listener.setPageSize(pageSize);}return true;}public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom) {this.marginLeft = marginLeft;this.marginRight = marginRight;this.marginTop = marginTop;this.marginBottom = marginBottom;Iterator i$ = this.listeners.iterator();while(i$.hasNext()) {DocListener listener = (DocListener)i$.next();listener.setMargins(marginLeft, marginRight, marginTop, marginBottom);}return true;}public boolean newPage() {if (this.open && !this.close) {Iterator i$ = this.listeners.iterator();while(i$.hasNext()) {DocListener listener = (DocListener)i$.next();listener.newPage();}return true;} else {return false;}}public void resetPageCount() {this.pageN = 0;Iterator i$ = this.listeners.iterator();while(i$.hasNext()) {DocListener listener = (DocListener)i$.next();listener.resetPageCount();}}//設置頁面的個數。每一頁是一個A4紙張的大小public void setPageCount(int pageN) {this.pageN = pageN;Iterator i$ = this.listeners.iterator();while(i$.hasNext()) {DocListener listener = (DocListener)i$.next();listener.setPageCount(pageN);}}public int getPageNumber() {return this.pageN;}//關閉。public void close() {if (!this.close) {this.open = false;this.close = true;}Iterator i$ = this.listeners.iterator();while(i$.hasNext()) {DocListener listener = (DocListener)i$.next();listener.close();}}//添加標題頭。直接調用或者寫個工具類就行。public boolean addHeader(String name, String content) {try {return this.add(new Header(name, content));} catch (DocumentException var4) {throw new ExceptionConverter(var4);}}//這是標題主題public boolean addTitle(String title) {try {return this.add(new Meta(1, title));} catch (DocumentException var3) {throw new ExceptionConverter(var3);}}//設置副主題public boolean addSubject(String subject) {try {return this.add(new Meta(2, subject));} catch (DocumentException var3) {throw new ExceptionConverter(var3);}}public boolean addKeywords(String keywords) {try {return this.add(new Meta(3, keywords));} catch (DocumentException var3) {throw new ExceptionConverter(var3);}}public boolean addAuthor(String author) {try {return this.add(new Meta(4, author));} catch (DocumentException var3) {throw new ExceptionConverter(var3);}}public boolean addCreator(String creator) {try {return this.add(new Meta(7, creator));} catch (DocumentException var3) {throw new ExceptionConverter(var3);}}public boolean addProducer() {try {return this.add(new Meta(5, Version.getInstance().getVersion()));} catch (DocumentException var2) {throw new ExceptionConverter(var2);}}/**這里進行語言的設置。我們可以通過這個來進行字體的設置**/public boolean addLanguage(String language) {try {return this.add(new Meta(8, language));} catch (DocumentException var3) {throw new ExceptionConverter(var3);}}public boolean addCreationDate() {try {SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");return this.add(new Meta(6, sdf.format(new Date())));} catch (DocumentException var2) {throw new ExceptionConverter(var2);}}public float leftMargin() {return this.marginLeft;}public float rightMargin() {return this.marginRight;}public float topMargin() {return this.marginTop;}public float bottomMargin() {return this.marginBottom;}public float left() {return this.pageSize.getLeft(this.marginLeft);}public float right() {return this.pageSize.getRight(this.marginRight);}public float top() {return this.pageSize.getTop(this.marginTop);}public float bottom() {return this.pageSize.getBottom(this.marginBottom);}public float left(float margin) {return this.pageSize.getLeft(this.marginLeft + margin);}public float right(float margin) {return this.pageSize.getRight(this.marginRight + margin);}public float top(float margin) {return this.pageSize.getTop(this.marginTop + margin);}public float bottom(float margin) {return this.pageSize.getBottom(this.marginBottom + margin);}public Rectangle getPageSize() {return this.pageSize;}public boolean isOpen() {return this.open;}public void setJavaScript_onLoad(String code) {this.javaScript_onLoad = code;}public String getJavaScript_onLoad() {return this.javaScript_onLoad;}public void setJavaScript_onUnLoad(String code) {this.javaScript_onUnLoad = code;}public String getJavaScript_onUnLoad() {return this.javaScript_onUnLoad;}public void setHtmlStyleClass(String htmlStyleClass) {this.htmlStyleClass = htmlStyleClass;}public String getHtmlStyleClass() {return this.htmlStyleClass;}public boolean setMarginMirroring(boolean marginMirroring) {this.marginMirroring = marginMirroring;Iterator i$ = this.listeners.iterator();while(i$.hasNext()) {Object element = (DocListener)i$.next();DocListener listener = (DocListener)element;listener.setMarginMirroring(marginMirroring);}return true;}public boolean setMarginMirroringTopBottom(boolean marginMirroringTopBottom) {this.marginMirroringTopBottom = marginMirroringTopBottom;Iterator i$ = this.listeners.iterator();while(i$.hasNext()) {Object element = (DocListener)i$.next();DocListener listener = (DocListener)element;listener.setMarginMirroringTopBottom(marginMirroringTopBottom);}return true;}public boolean isMarginMirroring() {return this.marginMirroring;}public PdfObject getAccessibleAttribute(PdfName key) {return this.accessibleAttributes != null ? (PdfObject)this.accessibleAttributes.get(key) : null;}public void setAccessibleAttribute(PdfName key, PdfObject value) {if (this.accessibleAttributes == null) {this.accessibleAttributes = new HashMap();}this.accessibleAttributes.put(key, value);}public HashMap<PdfName, PdfObject> getAccessibleAttributes() {return this.accessibleAttributes;}public PdfName getRole() {return this.role;}public void setRole(PdfName role) {this.role = role;}public AccessibleElementId getId() {return this.id;}public void setId(AccessibleElementId id) {this.id = id;}public boolean isInline() {return false;} }當然根據上面自己可以豐富自己的工具類通過構造這模式:
package com.example.luhenchang_pdf.pdfutils;import android.support.annotation.NonNull;import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Image; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.codec.PngImage;import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream;public class PdfItextUtil {//轉換private Document document;// savePath:保存pdf的路徑public PdfItextUtil(String savePath) throws Exception {//創建新的PDF文檔:A4大小,左右上下邊框均為0document = new Document(PageSize.ROYAL_QUARTO,50,50,0,0);//獲取PDF書寫器PdfWriter.getInstance(document, new FileOutputStream(savePath));//打開文檔document.open();}public void close(){if (document.isOpen()) {document.close();}}// 添加圖片到pdf中,這張圖片在pdf中居中顯示// imgPath:圖片的路徑,我使用的是sdcard中圖片// imgWidth:圖片在pdf中所占的寬// imgHeight:圖片在pdf中所占的高public PdfItextUtil addImageToPdfCenterH(@NonNull String imgPath, float imgWidth, float imgHeight) throws IOException, DocumentException {//獲取圖片Image img = Image.getInstance(imgPath);img.setAlignment(Element.ALIGN_CENTER);img.scaleToFit(imgWidth,imgHeight);//添加到PDF文檔document.add(img);return this;}public PdfItextUtil addPngToPdf(InputStream inputStream) throws DocumentException, IOException {Image img = PngImage.getImage(inputStream);img.setAlignment(Element.ALIGN_CENTER);//添加到PDF文檔document.add(img);return this;}// 添加文本到pdf中public PdfItextUtil addTextToPdf(String content) throws DocumentException {Paragraph elements = new Paragraph(content, setChineseFont());elements.setAlignment(Element.ALIGN_BASELINE); // elements.setIndentationLeft(55); //設置距離左邊的距離document.add(elements); // result為保存的字符串return this;}// 給pdf添加個標題,居中黑體public PdfItextUtil addTitleToPdf(String title){try {Paragraph elements = new Paragraph(title, setChineseTiltleFont(18));elements.setAlignment(Element.ALIGN_CENTER);document.add(elements); // result為保存的字符串} catch (DocumentException e) {e.printStackTrace();}return this;}private Font setChineseFont() {return setChineseFont(12);}private Font setChineseFont(int size) {BaseFont bf;Font fontChinese = null;try {// STSong-Light : Adobe的字體// UniGB-UCS2-H : pdf 字體bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);fontChinese = new Font(bf, size, Font.NORMAL);} catch (DocumentException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return fontChinese;}private Font setChineseTiltleFont(int size) {BaseFont bf;Font fontChinese = null;try {// STSong-Light : Adobe的字體// UniGB-UCS2-H : pdf 字體bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);fontChinese = new Font(bf, size, Font.BOLD);} catch (DocumentException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return fontChinese;} }項目地址碼云:
總結
以上是生活随笔為你收集整理的Android中合多图片和文字合成PDF文件---路很长的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java tcp 监听端口_【TCP/I
- 下一篇: Android应用程序消息处理机制(Lo