Android Graphics library Skia (iv)-Generate PDF

Source: Internet
Author: User
Tags skia

Android Graphics Library Skia ( four )-Generate PDF


This article mainly records the useSkiaLibrary GenerationPDFthe process of the file, in factSkianot only can youAndroidused in the system, in general embeddedLinuxcan also be used on the system. The title was written in the previous notation. This article is basedAndroid Graphics library Skia ( i )-Basic test to generate PNG images AndAndroid Graphics library Skia ( three )-Combine Freetype display text 。 Give an example directly:

/* Simple PDF Demo utilizing Skia toolkit.

* Authored by Kangear <[email protected]>

*/

#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 ()

int main ()

{

Declare a raster bitmap, which have an integer width and height,

and a format (config), and a pointer to the actual pixels.

Bitmaps can drawn into a skcanvas, but they is also used to

Specify the target of a Skcanvas ' drawing operations.

Skbitmap bitmap;

Bitmap.setconfig (Skbitmap::kargb_8888_config, 200, 200);

Bitmap.allocpixels ();

Pdf

skpdfdevice* dev = new Skpdfdevice (827, 1169);

Skautounref aur (dev);

A Canvas encapsulates all of the State about drawing into a

Device (bitmap). This includes a reference to the device itself,

and a stack of matrix/clip values. For any given draw call (e.g.

DrawRect), the geometry of the object being drawn is transformed

By the concatenation of the matrices in the stack. The

Transformed geometry is clipped by the intersection of the

Clips in the stack.

Skcanvas canvas (Dev);

Skpaint class holds the style and color information about

Draw geometries, text and bitmaps.

Skpaint paint;

Skirect holds four the bit integer coordinates for a rectangle.

Skrect R;

Paint.setargb (255, 255, 0, 0);

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

Canvas.drawrect (R, Paint); /** Draw the specified rectangle using

The specified paint. The rectangle

would be filled or stroked based on

The Style in the paint. */

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);

const char gtext[] = "123 Ping 34 van Road 12!" ";

Sktypeface *font = Sktypeface::createfromfile ("Droidsansfallbackfull.ttf");

Usr/share/fonts/truetype/msttcorefonts/droidsansfallbackfull.ttf

Sktypeface *font = Sktypeface::createfromname ("Droid Sans Fallback", 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, (+), paint);

}

Else

{

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

}

Skimageencoder is the base class for encoding compressed images

From a specific skbitmap.

Skimageencoder::encodefile ("Snapshot.png", Bitmap,

Skimageencoder::kpng_type,

/* Quality ranges from 0..100 */100);

Save as a PDF file

Skpdfdocument Doc;

Doc.appendpage (Dev);

Skdynamicmemorywstream Pdf_stream;

Doc.emitpdf (&pdf_stream);

Write (Stdout_fileno, Pdf_stream.getstream (), Pdf_stream.getoffset ());

return 0;

}

/*

Build

g++ \

-i./include \

-i./include/core \

-i./include/images \

-i./include/config \

-i./include/pdf/\

-wall-o Test-skia./test-skia.cpp \

OUT/SRC/IMAGES/SKIMAGEDECODER_LIBPNG.O \

OUT/LIBSKIA.A \

' Pkg-config freetype2--libs--cflags ' \

' Pkg-config libpng--libs--cflags ' \

-lpthread-g

Run

./test-skia > Test.pdf

*/

Effect:

Note: When creating a PDF , if you use the sktypeface::createfromfile method to create a font face, which belongs to the built-in font, Will cause the fonts in the PDF to not display properly in other environments. Using Adobe reader prompts you:

Text version:

Cannot extract the embedded font ' droidsansfallback '. Some characters may not display or print correctly.

Android Graphics library Skia (iv)-Generate PDF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.