Android图形库Skia()-生成PDF


本文主要记录使用skia库生成pdf文件的过程,其实skia并不仅仅能在Android系统中使用,在一般的嵌入式Linux系统上也可以使用的。标题就以以前的写法写了。本文基于Android图形库Skia()-基本测试生成PNG图片Android图形库Skia()-结合Freetype显示文字。直接给出例子:

/*SimplePDFdemoutilizingSkiatoolkit.

*Authoredbykangear<kangear@163.com>

*/

#include"SkBitmap.h"

#include"SkDevice.h"

#include"SkPaint.h"

#include"SkRect.h"

#include"SkImageEncoder.h"

#include"SkPDFCatalog.h"

#include"SkPDFDevice.h"

#include"SkPDFStream.h"

#include"SkPDFTypes.h"

#include"SkPDFDocument.h"

#include<stdio.h>

#include<unistd.h>

#include"SkTypeface.h"

//draw_content()

intmain()

{

//Declarearasterbitmap,whichhasanintegerwidthandheight,

//andaformat(config),andapointertotheactualpixels.

//BitmapscanbedrawnintoaSkCanvas,buttheyarealsousedto

//specifythetargetofaSkCanvas'drawingoperations.

SkBitmapbitmap;

bitmap.setConfig(SkBitmap::kARGB_8888_Config,200,200);

bitmap.allocPixels();

//PDF

SkPDFDevice*dev=newSkPDFDevice(827,1169);

SkAutoUnrefaur(dev);

//ACanvasencapsulatesallofthestateaboutdrawingintoa

//device(bitmap).Thisincludesareferencetothedeviceitself,

//andastackofmatrix/clipvalues.Foranygivendrawcall(e.g.

//drawRect),thegeometryoftheobjectbeingdrawnistransformed

//bytheconcatenationofallthematricesinthestack.The

//transformedgeometryisclippedbytheintersectionofallofthe

//clipsinthestack.

SkCanvascanvas(dev);

//SkPaintclassholdsthestyleandcolorinformationabouthowto

//drawgeometries,textandbitmaps.

SkPaintpaint;

//SkIRectholdsfour32bitintegercoordinatesforarectangle.

SkRectr;

paint.setARGB(255,255,0,0);

r.set(25,25,145,145);

canvas.drawRect(r,paint);/**Drawthespecifiedrectangleusing

thespecifiedpaint.Therectangle

willbefilledorstrokedbasedon

theStyleinthepaint.*/

paint.setARGB(255,0,255,0);

r.offset(20,20);

canvas.drawRect(r,paint);

paint.setARGB(255,0,0,255);

r.offset(20,20);

canvas.drawRect(r,paint);

constchargText[]="123平34凡之路12!";

//SkTypeface*font=SkTypeface::CreateFromFile("DroidSansFallbackFull.ttf");

//usr/share/fonts/truetype/msttcorefonts/DroidSansFallbackFull.ttf

SkTypeface*font=SkTypeface::CreateFromName("DroidSansFallback",SkTypeface::kNormal);

if(font)

{

paint.setARGB(255,255,0,0);

paint.setTypeface(font);

paint.setTextSize(25);

canvas.drawText(gText,sizeof(gText)/sizeof(gText[0]),0,25,paint);

}

else

{

printf("font==null!\n");

}

//SkImageEncoderisthebaseclassforencodingcompressedimages

//fromaspecificSkBitmap.

SkImageEncoder::EncodeFile("snapshot.png",bitmap,

SkImageEncoder::kPNG_Type,

/*Qualityrangesfrom0..100*/100);

//saveasapdffile

SkPDFDocumentdoc;

doc.appendPage(dev);

SkDynamicMemoryWStreampdf_stream;

doc.emitPDF(&pdf_stream);

write(STDOUT_FILENO,pdf_stream.getStream(),pdf_stream.getOffset());

return0;

}

/*

build:

g++\

-I./include\

-I./include/core\

-I./include/images\

-I./include/config\

-I./include/pdf/\

-Wall-otest-skia./test-skia.cpp\

out/src/images/SkImageDecoder_libpng.o\

out/libskia.a\

`pkg-configfreetype2--libs--cflags`\

`pkg-configlibpng--libs--cflags`\

-lpthread-g

run:

./test-skia>test.pdf

*/

效果:

注意事项:生成PDF时,如果使用SkTypeface::CreateFromFile方法创建字体face,属于内置字体,会造成在其它环境下PDF中字体无法正常显示。使用Adobereader会提示:

打印效果:


文字版本:

Cannotextracttheembeddedfont‘DroidSansFallback’.Somecharactersmaynotdisplayorprintcorrectly.

参考文档:

1.https://code.google.com/p/skia/wiki/PDFTheoryOfOperation

2.List_of_typefaces

3.PDFReference



更多相关文章

  1. Android学习路线总结,绝对干货
  2. 【Android(安卓)应用开发】Android资源文件 - 使用资源存储字符
  3. 使用平台的风格和主题
  4. 【Android】注解框架(二) 基础知识(Java注解)& 运行时注解框架
  5. Android跨进程通信IPC
  6. Android中的shape中的属性大全
  7. WebView的使用之Android与JS通过WebView互调方法
  8. Android开发规范详解
  9. Android下基于XML的Graphics

随机推荐

  1. Android短信数据库字段描述
  2. Android实现随机圆形云标签效果
  3. 在studio中使用libsdl2.so,编译SDL Demo
  4. Bitmap和libyuv在JNI中的字节序
  5. Android图片查看器(图片可移动、缩放、旋
  6. Android(安卓)ViewPager禁止滑动、滑动效
  7. (1)Android开源项目介绍
  8. Android学习笔记(十九)分享简单数据
  9. Android(安卓)studio gradle太慢解决办法
  10. android中事件传递机制(2)