OLD | NEW |
1 Using Skia's PDF Backend | 1 Using Skia's PDF Backend |
2 ======================== | 2 ======================== |
3 | 3 |
4 Here is an example of using Skia's PDF backend in the recommended way: | 4 Here is an example of using Skia's PDF backend in the recommended way: |
5 via the SkDocument and SkCanvas APIs. | 5 via the SkDocument and SkCanvas APIs. |
6 | 6 |
7 <!--?prettify lang=cc?--> | 7 <!--?prettify lang=cc?--> |
8 | 8 |
9 #include "SkDocument.h" | 9 #include "SkDocument.h" |
10 | 10 |
11 bool WritePDF(SkWStream* outputStream) { | 11 bool WritePDF(SkWStream* outputStream) { |
12 sk_sp<SkDocument> pdfDocument(SkDocument::CreatePDF(outputStream)); | 12 SkDocument::PDFMetadata metadata; |
13 typedef SkDocument::Attribute Attr; | 13 metadata.fCreator = "creator...."; |
14 Attr info[] = { | 14 metadata.fTitle = "title..."; |
15 Attr(SkString("Title"), SkString("....")), | 15 metadata.fAuthor = "author..."; |
16 Attr(SkString("Author"), SkString("....")), | 16 metadata.fSubject = "subject..."; |
17 Attr(SkString("Subject"), SkString("....")), | 17 metadata.fKeywords = "keywords..."; |
18 Attr(SkString("Keywords"), SkString("....")), | 18 metadata.fCreator = "creator..."; |
19 Attr(SkString("Creator"), SkString("....")), | 19 SkTime::DateTime now = get_current_date_and_time(); |
20 }; | 20 metadata.fCreation.fEnabled = true; |
21 int infoCount = sizeof(info) / sizeof(info[0]); | 21 metadata.fCreation.fDateTime = now; |
22 SkTime::DateTime now; | 22 metadata.fModified.fEnabled = true; |
23 SkTime::GetDateTime(&now); | 23 metadata.fModified.fDateTime = now; |
24 pdfDocument->setMetadata(info, infoCount, &now, &now); | 24 sk_sp<SkDocument> pdfDocument(SkDocument::MakePDF( |
| 25 outputStream, SK_ScalarDefaultRasterDPI, metadata, |
| 26 nullptr, true); |
| 27 assert(pdfDocument); |
25 | 28 |
26 int numberOfPages = ....; | 29 int numberOfPages = ....; |
27 for (int page = 0; page < numberOfPages; ++page) { | 30 for (int page = 0; page < numberOfPages; ++page) { |
28 SkScalar pageWidth = ....; | 31 SkScalar pageWidth = ....; |
29 SkScalar pageHeight = ....; | 32 SkScalar pageHeight = ....; |
30 SkCanvas* pageCanvas = | 33 SkCanvas* pageCanvas = |
31 pdfDocument->beginPage(pageWidth, pageHeight); | 34 pdfDocument->beginPage(pageWidth, pageHeight); |
32 | 35 |
33 // ....insert canvas draw commands here.... | 36 // ....insert canvas draw commands here.... |
34 | 37 |
35 pdfDocument->endPage(); | 38 pdfDocument->endPage(); |
36 } | 39 } |
37 return pdfDocument->close(); | 40 return pdfDocument->close(); |
38 } | 41 } |
OLD | NEW |