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?--> | 7 <!--?prettify lang=cc?--> |
8 | 8 |
9 #include "SkDocument.h" | 9 #include "SkDocument.h" |
10 | 10 |
11 bool WritePDF() { | 11 bool WritePDF(SkWStream* outputStream) { |
12 SkWStream* outputStream = ....; | 12 sk_sp<SkDocument> pdfDocument(SkDocument::CreatePDF(outputStream)); |
13 | 13 typedef SkDocument::Attribute Attr; |
14 SkAutoTUnref<SkDocument> pdfDocument( | 14 Attr info[] = { |
15 SkDocument::CreatePDF(outputStream)); | 15 Attr(SkString("Title"), SkString("....")), |
| 16 Attr(SkString("Author"), SkString("....")), |
| 17 Attr(SkString("Subject"), SkString("....")), |
| 18 Attr(SkString("Keywords"), SkString("....")), |
| 19 Attr(SkString("Creator"), SkString("....")), |
| 20 }; |
| 21 int infoCount = sizeof(info) / sizeof(info[0]); |
| 22 SkTime::DateTime now; |
| 23 SkTime::GetDateTime(&now); |
| 24 pdfDocument->setMetadata(info, infoCount, &now, &now); |
16 | 25 |
17 int numberOfPages = ....; | 26 int numberOfPages = ....; |
18 for (int page = 0; page < numberOfPages; ++page) { | 27 for (int page = 0; page < numberOfPages; ++page) { |
19 SkScalar pageWidth = ....; | 28 SkScalar pageWidth = ....; |
20 SkScalar pageHeight = ....; | 29 SkScalar pageHeight = ....; |
21 SkCanvas* pageCanvas = | 30 SkCanvas* pageCanvas = |
22 pdfDocument->beginPage(pageWidth, pageHeight); | 31 pdfDocument->beginPage(pageWidth, pageHeight); |
23 | 32 |
24 // ....insert canvas draw commands here.... | 33 // ....insert canvas draw commands here.... |
25 | 34 |
26 pdfDocument->endPage(); | 35 pdfDocument->endPage(); |
27 } | 36 } |
28 | |
29 SkTArray<SkDocument::Attribute> info; | |
30 info.emplace_back(SkString("Title"), SkString("....")); | |
31 info.emplace_back(SkString("Author"), SkString("....")); | |
32 info.emplace_back(SkString("Subject"), SkString("....")); | |
33 info.emplace_back(SkString("Keywords"), SkString("....")); | |
34 info.emplace_back(SkString("Creator"), SkString("....")); | |
35 SkTime::DateTime now; | |
36 SkTime::GetDateTime(&now); | |
37 pdfDocument->setMetadata(info, &now, &now); | |
38 | |
39 return pdfDocument->close(); | 37 return pdfDocument->close(); |
40 } | 38 } |
OLD | NEW |