当前位置: 首页 > news >正文

电商网站的二级菜单怎么做网上电商平台开发

电商网站的二级菜单怎么做,网上电商平台开发,中国50强企业管理培训机构,本地的番禺网站建设前文介绍如何使用图片生成pdf,这里介绍如何使用文本生成pdf 使用mupdf生成 mupdf生成的pdf略大,字体可以自定义. 生成的代码不复杂,也有好几种,以story的方式生成为例 fun createPdfFromText(sourcePath: String, destPath: String): Boolean {val text EncodingDetect.rea…

前文介绍如何使用图片生成pdf,这里介绍如何使用文本生成pdf

使用mupdf生成

mupdf生成的pdf略大,字体可以自定义.

生成的代码不复杂,也有好几种,以story的方式生成为例

fun createPdfFromText(sourcePath: String, destPath: String): Boolean {val text = EncodingDetect.readFile(sourcePath)val mediabox = Rect(0f, 0f, 500f, 707f) //A2val margin = 10fvar writer = DocumentWriter(destPath, "PDF", "")var snark = "<!DOCTYPE html>" +"<style>" +"#body { font-family: \"Droid Sans\", sans-serif; }" +"</style>" +"<body>" +text +"</body></html>"val story = Story(snark, "", 12f)var more: Booleando {val filled = Rect()val where = Rect(mediabox.x0 + margin,mediabox.y0 + margin,mediabox.x1 - margin,mediabox.y1 - margin)val dev: Device = writer.beginPage(mediabox)more = story.place(where, filled)story.draw(dev, Matrix.Identity())writer.endPage()} while (more)writer.close()writer.destroy()story.destroy()return true}

文档的读取,对于中文,可能会涉及到编码的处理.

先定义mediabox,就是纸张大小,这里是a2

然后用html生成story,最后写入就完成了

如果要自定义样式,颜色这些,api要去查如何使用.并不那么方便.

使用系统sdk生成

系统sdk是基于福昕的代码.但生成时可以使用view来设置样式,然后生成页面.样式比较容易控制,毕竟view长什么样是容易修改的.

读取文本是一样的.

val pdfDocument = PdfDocument()val pageWidth = PDF_PAGE_WIDTH.toInt()val pageHeight = PDF_PAGE_HEIGHT.toInt()val contentView =LayoutInflater.from(context).inflate(R.layout.pdf_content, parent, false) as TextViewcontentView.text = contentval measureWidth = View.MeasureSpec.makeMeasureSpec(pageWidth, View.MeasureSpec.EXACTLY)val measuredHeight = View.MeasureSpec.makeMeasureSpec(pageHeight, View.MeasureSpec.EXACTLY)contentView.measure(measureWidth, measuredHeight)contentView.layout(0, 0, pageWidth, pageHeight)

建立文档对象,然后加载布局

val lineCount = contentView.lineCountvar lineHeight = contentView.lineHeightif (contentView.lineSpacingMultiplier > 0) {lineHeight = (lineHeight * contentView.lineSpacingMultiplier).toInt()}val layout = contentView.layoutvar start = 0var end: Intvar pageH = 0val paddingTopAndBottom: Int = Utils.dipToPixel(context, 40f)

设置边距40dp,读取行间距与行数.

因为设置了内容后,由textview自己就能计算出这些,然后根据这些信息,计算我们一页的高宽可以分成几页,然后每一页再用一个布局去生成就行了.

while (i < lineCount) {end = layout.getLineEnd(i)val line = content.substring(start, end) //指定行的内容start = endsb.append(line)pageH += lineHeightif (pageH >= pageHeight - paddingTopAndBottom) {Log.d("TextView",String.format("============page line:%s,lh:%s,ph:%s==========",i,lineHeight,pageHeight))createTxtPage(context,parent,pdfDocument,pageWidth,pageHeight,i + 1,sb.toString())pageH = 0sb.setLength(0)}i++}if (sb.length > 0) {Log.d("TextView", "last line ===")createTxtPage(context, parent, pdfDocument, pageWidth, pageHeight, i, sb.toString())}

这就是计算的过程,最后要再处理剩下的部分.

对于每一页的显示字符数处理完,接着就是对这些生成页面

private fun createTxtPage(context: Context?,parent: ViewGroup?,pdfDocument: PdfDocument,pageWidth: Int,pageHeight: Int,pageNo: Int,content: String?) {val contentView =LayoutInflater.from(context).inflate(R.layout.pdf_content, parent, false) as TextViewcontentView.text = contentval pageInfo: PdfDocument.PageInfo =PdfDocument.PageInfo.Builder(pageWidth, pageHeight, pageNo).create()val page: PdfDocument.Page = pdfDocument.startPage(pageInfo)val pageCanvas: Canvas = page.getCanvas()val measureWidth = View.MeasureSpec.makeMeasureSpec(pageWidth, View.MeasureSpec.EXACTLY)val measuredHeight = View.MeasureSpec.makeMeasureSpec(pageHeight, View.MeasureSpec.EXACTLY)contentView.measure(measureWidth, measuredHeight)contentView.layout(0, 0, pageWidth, pageHeight)contentView.draw(pageCanvas)// finish the pagepdfDocument.finishPage(page)}

页面同样用之前的布局,这样保持是一致的,否则高宽计算就不对了.这里因为不需要显示,所以我们要调用measure与layout再draw画出来 

保存就简单了

private fun savePdf(path: String?, document: PdfDocument): Boolean {val outputStream = FileOutputStream(path)try {document.writeTo(outputStream)return true} catch (e: IOException) {e.printStackTrace()} finally {document.close()}return false}

http://www.hkea.cn/news/607008/

相关文章:

  • 电商网站开发服务全网营销骗局揭秘
  • 个人网站怎么做互联网营销师培训课程免费
  • 微信网站建设价格网站开发报价方案
  • wordpress utc时间慢8小时大连seo关键词排名
  • 中国建设承包商网站创建软件平台该怎么做
  • 中小企业网站建设费用海外推广服务
  • 企业名称的英文做网站名seo是怎么优化推广的
  • 手机在线建站西安seo服务公司
  • 网站开发有前途吗我也要投放广告
  • 备案 网站名称怎么写crm软件
  • 扁平式网站模板b2b网站推广优化
  • 做外贸网站网络营销咨询服务
  • 江门网站建设方案报价淘宝seo优化怎么做
  • 盘龙城做网站推广网站推广
  • 如何做电子书网站域名站长工具
  • 物联网平台有哪些排名优化外包公司
  • 秦皇岛汽车网站制作数字营销工具
  • 培训教育的网站怎么做东莞做网站的联系电话
  • 云南做网站的公司外贸谷歌优化
  • 网页设计学徒培训可试学巢湖seo推广
  • 让顾客心动的句子seo模拟点击软件源码
  • 设计类专业包括哪些kj6699的seo综合查询
  • 手机网站制作哪家好查关键词
  • 米拓企业网站管理系统电商培训机构排名前十
  • 做效果图有哪些网站seo点击排名
  • 网络营销推广网站收录seo推广排名平台有哪些
  • 产品经理如何看待网站开发广州软件系统开发seo推广
  • wordpress 忘记管理员如何做网站seo
  • app和网站哪个有优势淘宝关键词排名
  • wordpress该域名宁波网站seo公司