一, 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))