From 57f228d7527594de55951fa05e6082ba62382c57 Mon Sep 17 00:00:00 2001 From: Jane Liu Date: Thu, 13 Jul 2017 18:10:30 -0400 Subject: Updated pdfium_test's WriteAnnot() Updated pdfium_test's WriteAnnot() (corresponding to the --annot flag) to output more annotation info using the new APIs. Also fixed some nits in the annotation API code. Bug=pdfium:737 Change-Id: I3f40e83279ec82529f732eb94f309ab7d4992d3c Reviewed-on: https://pdfium-review.googlesource.com/7791 Reviewed-by: dsinclair Commit-Queue: Jane Liu --- samples/pdfium_test.cc | 90 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 77 insertions(+), 13 deletions(-) (limited to 'samples') diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc index 93821d963a..223a29f1f4 100644 --- a/samples/pdfium_test.cc +++ b/samples/pdfium_test.cc @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -265,6 +266,44 @@ std::string AnnotSubtypeToString(FPDF_ANNOTATION_SUBTYPE subtype) { return ""; } +std::string AnnotFlagsToString(int flags) { + std::string str = ""; + if (flags & FPDF_ANNOT_FLAG_INVISIBLE) + str += "Invisible"; + if (flags & FPDF_ANNOT_FLAG_HIDDEN) + str += std::string(str.empty() ? "" : ", ") + "Hidden"; + if (flags & FPDF_ANNOT_FLAG_PRINT) + str += std::string(str.empty() ? "" : ", ") + "Print"; + if (flags & FPDF_ANNOT_FLAG_NOZOOM) + str += std::string(str.empty() ? "" : ", ") + "NoZoom"; + if (flags & FPDF_ANNOT_FLAG_NOROTATE) + str += std::string(str.empty() ? "" : ", ") + "NoRotate"; + if (flags & FPDF_ANNOT_FLAG_NOVIEW) + str += std::string(str.empty() ? "" : ", ") + "NoView"; + if (flags & FPDF_ANNOT_FLAG_READONLY) + str += std::string(str.empty() ? "" : ", ") + "ReadOnly"; + if (flags & FPDF_ANNOT_FLAG_LOCKED) + str += std::string(str.empty() ? "" : ", ") + "Locked"; + if (flags & FPDF_ANNOT_FLAG_TOGGLENOVIEW) + str += std::string(str.empty() ? "" : ", ") + "ToggleNoView"; + return str; +} + +std::string PageObjectTypeToString(int type) { + if (type == FPDF_PAGEOBJ_TEXT) + return "Text"; + if (type == FPDF_PAGEOBJ_PATH) + return "Path"; + if (type == FPDF_PAGEOBJ_IMAGE) + return "Image"; + if (type == FPDF_PAGEOBJ_SHADING) + return "Shading"; + if (type == FPDF_PAGEOBJ_FORM) + return "Form"; + NOTREACHED(); + return ""; +} + void WriteAnnot(FPDF_PAGE page, const char* pdf_name, int num) { // Open the output text file. char filename[256]; @@ -285,7 +324,7 @@ void WriteAnnot(FPDF_PAGE page, const char* pdf_name, int num) { fprintf(fp, "Number of annotations: %d\n\n", annot_count); // Iterate through all annotations on this page. - for (int i = 0; i < annot_count; i++) { + for (int i = 0; i < annot_count; ++i) { // Retrieve the annotation object and its subtype. fprintf(fp, "Annotation #%d:\n", i + 1); FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, i); @@ -296,6 +335,24 @@ void WriteAnnot(FPDF_PAGE page, const char* pdf_name, int num) { FPDF_ANNOTATION_SUBTYPE subtype = FPDFAnnot_GetSubtype(annot); fprintf(fp, "Subtype: %s\n", AnnotSubtypeToString(subtype).c_str()); + // Retrieve the annotation flags. + fprintf(fp, "Flags set: %s\n", + AnnotFlagsToString(FPDFAnnot_GetFlags(annot)).c_str()); + + // Retrieve the annotation's object count and object types. + const int obj_count = FPDFAnnot_GetObjectCount(annot); + fprintf(fp, "Number of objects: %d\n", obj_count); + if (obj_count > 0) { + fprintf(fp, "Object types: "); + for (int j = 0; j < obj_count; ++j) { + fprintf(fp, "%s ", + PageObjectTypeToString( + FPDFPageObj_GetType(FPDFAnnot_GetObject(annot, j))) + .c_str()); + } + fprintf(fp, "\n"); + } + // Retrieve the annotation's color and interior color. unsigned int R; unsigned int G; @@ -336,15 +393,19 @@ void WriteAnnot(FPDF_PAGE page, const char* pdf_name, int num) { // Retrieve the annotation's quadpoints if it is a markup annotation. if (FPDFAnnot_HasAttachmentPoints(annot)) { FS_QUADPOINTSF quadpoints = FPDFAnnot_GetAttachmentPoints(annot); - fprintf(fp, "Quadpoints: (%f, %f), (%f, %f), (%f, %f), (%f, %f)\n", + fprintf(fp, + "Quadpoints: (%.3f, %.3f), (%.3f, %.3f), (%.3f, %.3f), (%.3f, " + "%.3f)\n", quadpoints.x1, quadpoints.y1, quadpoints.x2, quadpoints.y2, quadpoints.x3, quadpoints.y3, quadpoints.x4, quadpoints.y4); } // Retrieve the annotation's rectangle coordinates. FS_RECTF rect = FPDFAnnot_GetRect(annot); - fprintf(fp, "Rectangle: l - %f, b - %f, r - %f, t - %f\n\n", rect.left, - rect.bottom, rect.right, rect.top); + fprintf(fp, "Rectangle: l - %.3f, b - %.3f, r - %.3f, t - %.3f\n\n", + rect.left, rect.bottom, rect.right, rect.top); + + FPDFPage_CloseAnnot(annot); } (void)fclose(fp); @@ -1227,18 +1288,21 @@ static const char kUsageString[] = " --scale= - scale output size by number (e.g. 0.5)\n" " --pages=(-) - only render the given 0-based page(s)\n" #ifdef _WIN32 - " --bmp - write page images ..bmp\n" - " --emf - write page meta files ..emf\n" - " --ps2 - write page raw PostScript (Lvl 2) ..ps\n" - " --ps3 - write page raw PostScript (Lvl 3) ..ps\n" + " --bmp - write page images ..bmp\n" + " --emf - write page meta files ..emf\n" + " --ps2 - write page raw PostScript (Lvl 2) " + "..ps\n" + " --ps3 - write page raw PostScript (Lvl 3) " + "..ps\n" #endif // _WIN32 - " --txt - write page text in UTF32-LE ..txt\n" - " --png - write page images ..png\n" - " --ppm - write page images ..ppm\n" + " --txt - write page text in UTF32-LE ..txt\n" + " --png - write page images ..png\n" + " --ppm - write page images ..ppm\n" + " --annot - write annotation info ..annot.txt\n" #ifdef PDF_ENABLE_SKIA - " --skp - write page images ..skp\n" + " --skp - write page images ..skp\n" #endif - " --md5 - write output image paths and their md5 hashes to stdout.\n" + " --md5 - write output image paths and their md5 hashes to stdout.\n" ""; int main(int argc, const char* argv[]) { -- cgit v1.2.3