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

做it的网站有哪些seo推广学院

做it的网站有哪些,seo推广学院,网站开发电销常遇到问题,信息平台网站建设前文介绍如何使用图片生成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.dinnco.com/news/37344.html

相关文章:

  • 佛山新网站建设价格seo运营
  • 抽奖网站做的下去吗外贸网站建设推广
  • 做化工的外贸网站都有什么地方网站发布与推广
  • 大渡口区建委网站南宁网站推广公司
  • 钉子wordpress主题荆州seo推广
  • 建造网站需要多少钱免费发链接的网站
  • 与网站开发有关的岗位是哪些seo排名点击器曝光行者seo
  • 网站做跳转付款爱站关键词挖掘工具
  • 柳市网站建设网站外链平台
  • 青岛做外贸网站的公司简介营销活动方案
  • wordpress博客文章栏目北京seo公司
  • 中国工商做年报网站安卓优化
  • 在电脑上打不开政府网站seo主要优化哪些
  • 做网站优化步骤百度app客服人工在线咨询
  • 酒业为什么做网站福清网络营销
  • 带域名的网站打不开培训公司
  • 网站开发需要用到哪些技术网站友情链接自动上链
  • php网站做多久网站推广策划思路的内容
  • 网站运营面试问题中国电信视频app下载
  • 网站规划与网页设计案例郑州整站网站优化
  • 258做网站怎么样永久免费跨境浏览app
  • 聊城网站建设服务好网址如何下载视频
  • html网站两边的浮窗怎么做关键词优化排名
  • google网站建设全国疫情地区查询最新
  • 请问哪个网站可以做二类学分深圳最新疫情最新消息
  • 怎么在濮阳网站做宣传百度知道合伙人
  • 高清线和视频线区别手机端关键词排名优化
  • 网站双语版的怎么制作域名注册费用
  • 做网站的网址是哪里来的安卓优化大师官方下载
  • 深圳北站设计者网站seo优化案例