Spaces:
Sleeping
Sleeping
File size: 31,350 Bytes
5cee033 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 |
/*
* Copyright (C) 2009-2011, Pino Toscano <[email protected]>
* Copyright (C) 2016 Jakub Alba <[email protected]>
* Copyright (C) 2017, 2022, Albert Astals Cid <[email protected]>
* Copyright (C) 2018, 2020, Adam Reichold <[email protected]>
* Copyright (C) 2019, Masamichi Hosoda <[email protected]>
* Copyright (C) 2019, 2020, Oliver Sander <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
\file poppler-document.h
*/
#include "poppler-destination.h"
#include "poppler-document.h"
#include "poppler-embedded-file.h"
#include "poppler-page.h"
#include "poppler-toc.h"
#include "poppler-destination-private.h"
#include "poppler-document-private.h"
#include "poppler-embedded-file-private.h"
#include "poppler-page-private.h"
#include "poppler-private.h"
#include "poppler-toc-private.h"
#include "Catalog.h"
#include "DateInfo.h"
#include "ErrorCodes.h"
#include "GlobalParams.h"
#include "Link.h"
#include "Outline.h"
#include <algorithm>
#include <iterator>
#include <memory>
using namespace poppler;
document_private::document_private(std::unique_ptr<GooString> &&file_path, const std::string &owner_password, const std::string &user_password) : document_private()
{
doc = new PDFDoc(std::move(file_path), GooString(owner_password.c_str()), GooString(user_password.c_str()));
}
document_private::document_private(byte_array *file_data, const std::string &owner_password, const std::string &user_password) : document_private()
{
file_data->swap(doc_data);
MemStream *memstr = new MemStream(&doc_data[0], 0, doc_data.size(), Object(objNull));
doc = new PDFDoc(memstr, GooString(owner_password.c_str()), GooString(user_password.c_str()));
}
document_private::document_private(const char *file_data, int file_data_length, const std::string &owner_password, const std::string &user_password) : document_private()
{
raw_doc_data = file_data;
raw_doc_data_length = file_data_length;
MemStream *memstr = new MemStream(raw_doc_data, 0, raw_doc_data_length, Object(objNull));
doc = new PDFDoc(memstr, GooString(owner_password.c_str()), GooString(user_password.c_str()));
}
document_private::document_private() : GlobalParamsIniter(detail::error_function), doc(nullptr), raw_doc_data(nullptr), raw_doc_data_length(0), is_locked(false) { }
document_private::~document_private()
{
delete_all(embedded_files);
delete doc;
}
document *document_private::check_document(document_private *doc, byte_array *file_data)
{
if (doc->doc->isOk() || doc->doc->getErrorCode() == errEncrypted) {
if (doc->doc->getErrorCode() == errEncrypted) {
doc->is_locked = true;
}
return new document(*doc);
} else {
// put back the document data where it was before
if (file_data) {
file_data->swap(doc->doc_data);
}
delete doc;
}
return nullptr;
}
/**
\class poppler::document poppler-document.h "poppler/cpp/poppler-document.h"
Represents a PDF %document.
*/
/**
\enum poppler::document::page_mode_enum
The various page modes available in a PDF %document.
*/
/**
\var poppler::document::page_mode_enum poppler::document::use_none
The %document specifies no particular page mode.
*/
/**
\var poppler::document::page_mode_enum poppler::document::use_outlines
The %document specifies its TOC (table of contents) should be open.
*/
/**
\var poppler::document::page_mode_enum poppler::document::use_thumbs
The %document specifies that should be open a view of the thumbnails of its
pages.
*/
/**
\var poppler::document::page_mode_enum poppler::document::fullscreen
The %document specifies it wants to be open in a fullscreen mode.
*/
/**
\var poppler::document::page_mode_enum poppler::document::use_oc
The %document specifies that should be open a view of its Optional Content
(also known as layers).
*/
/**
\var poppler::document::page_mode_enum poppler::document::use_attach
The %document specifies that should be open a view of its %document-level
attachments.
*/
document::document(document_private &dd) : d(&dd) { }
document::~document()
{
delete d;
}
/**
\returns whether the current %document is locked
*/
bool document::is_locked() const
{
return d->is_locked;
}
/**
Unlocks the current document, if locked.
\returns the new locking status of the document
*/
bool document::unlock(const std::string &owner_password, const std::string &user_password)
{
if (d->is_locked) {
document_private *newdoc = nullptr;
if (d->doc_data.size() > 0) {
newdoc = new document_private(&d->doc_data, owner_password, user_password);
} else if (d->raw_doc_data) {
newdoc = new document_private(d->raw_doc_data, d->raw_doc_data_length, owner_password, user_password);
} else {
newdoc = new document_private(std::make_unique<GooString>(d->doc->getFileName()), owner_password, user_password);
}
if (!newdoc->doc->isOk()) {
d->doc_data.swap(newdoc->doc_data);
delete newdoc;
} else {
delete d;
d = newdoc;
d->is_locked = false;
}
}
return d->is_locked;
}
/**
\returns the eventual page mode specified by the current PDF %document
*/
document::page_mode_enum document::page_mode() const
{
switch (d->doc->getCatalog()->getPageMode()) {
case Catalog::pageModeNone:
return use_none;
case Catalog::pageModeOutlines:
return use_outlines;
case Catalog::pageModeThumbs:
return use_thumbs;
case Catalog::pageModeFullScreen:
return fullscreen;
case Catalog::pageModeOC:
return use_oc;
case Catalog::pageModeAttach:
return use_attach;
default:
return use_none;
}
}
/**
\returns the eventual page layout specified by the current PDF %document
*/
document::page_layout_enum document::page_layout() const
{
switch (d->doc->getCatalog()->getPageLayout()) {
case Catalog::pageLayoutNone:
return no_layout;
case Catalog::pageLayoutSinglePage:
return single_page;
case Catalog::pageLayoutOneColumn:
return one_column;
case Catalog::pageLayoutTwoColumnLeft:
return two_column_left;
case Catalog::pageLayoutTwoColumnRight:
return two_column_right;
case Catalog::pageLayoutTwoPageLeft:
return two_page_left;
case Catalog::pageLayoutTwoPageRight:
return two_page_right;
default:
return no_layout;
}
}
/**
Gets the version of the current PDF %document.
Example:
\code
poppler::document *doc = ...;
// for example, if the document is PDF 1.6:
int major = 0, minor = 0;
doc->get_pdf_version(&major, &minor);
// major == 1
// minor == 6
\endcode
\param major if not NULL, will be set to the "major" number of the version
\param minor if not NULL, will be set to the "minor" number of the version
*/
void document::get_pdf_version(int *major, int *minor) const
{
if (major) {
*major = d->doc->getPDFMajorVersion();
}
if (minor) {
*minor = d->doc->getPDFMinorVersion();
}
}
/**
\returns all the information keys available in the %document
\see info_key, info_date
*/
std::vector<std::string> document::info_keys() const
{
if (d->is_locked) {
return std::vector<std::string>();
}
Object info = d->doc->getDocInfo();
if (!info.isDict()) {
return std::vector<std::string>();
}
Dict *info_dict = info.getDict();
std::vector<std::string> keys(info_dict->getLength());
for (int i = 0; i < info_dict->getLength(); ++i) {
keys[i] = std::string(info_dict->getKey(i));
}
return keys;
}
/**
Gets the value of the specified \p key of the document information.
\returns the value for the \p key, or an empty string if not available
\see info_keys, info_date
*/
ustring document::info_key(const std::string &key) const
{
if (d->is_locked) {
return ustring();
}
std::unique_ptr<GooString> goo_value(d->doc->getDocInfoStringEntry(key.c_str()));
if (!goo_value.get()) {
return ustring();
}
return detail::unicode_GooString_to_ustring(goo_value.get());
}
/**
Sets the value of the specified \p key of the %document information to \p val.
If \p val is empty, the entry specified by \p key is removed.
\returns true on success, false on failure
*/
bool document::set_info_key(const std::string &key, const ustring &val)
{
if (d->is_locked) {
return false;
}
GooString *goo_val;
if (val.empty()) {
goo_val = nullptr;
} else {
goo_val = detail::ustring_to_unicode_GooString(val);
}
d->doc->setDocInfoStringEntry(key.c_str(), goo_val);
return true;
}
/**
Gets the time_type value of the specified \p key of the document
information.
\returns the time_t value for the \p key
\see info_keys, info_date
*/
time_type document::info_date(const std::string &key) const
{
if (d->is_locked) {
return time_type(-1);
}
std::unique_ptr<GooString> goo_date(d->doc->getDocInfoStringEntry(key.c_str()));
if (!goo_date.get()) {
return time_type(-1);
}
return static_cast<time_type>(dateStringToTime(goo_date.get()));
}
/**
Gets the time_t value of the specified \p key of the document
information.
\returns the time_t value for the \p key
\see info_keys, info_date
*/
time_t document::info_date_t(const std::string &key) const
{
if (d->is_locked) {
return time_t(-1);
}
std::unique_ptr<GooString> goo_date(d->doc->getDocInfoStringEntry(key.c_str()));
if (!goo_date.get()) {
return time_t(-1);
}
return dateStringToTime(goo_date.get());
}
/**
Sets the time_type value of the specified \p key of the %document information
to \p val.
If \p val == time_type(-1), the entry specified by \p key is removed.
\returns true on success, false on failure
*/
bool document::set_info_date(const std::string &key, time_type val)
{
if (d->is_locked) {
return false;
}
GooString *goo_date;
if (val == time_type(-1)) {
goo_date = nullptr;
} else {
time_t t = static_cast<time_t>(val);
goo_date = timeToDateString(&t);
}
d->doc->setDocInfoStringEntry(key.c_str(), goo_date);
return true;
}
/**
Sets the time_t value of the specified \p key of the %document information
to \p val.
If \p val == time_t(-1), the entry specified by \p key is removed.
\returns true on success, false on failure
*/
bool document::set_info_date_t(const std::string &key, time_t val)
{
if (d->is_locked) {
return false;
}
GooString *goo_date;
if (val == time_t(-1)) {
goo_date = nullptr;
} else {
goo_date = timeToDateString(&val);
}
d->doc->setDocInfoStringEntry(key.c_str(), goo_date);
return true;
}
/**
Gets the %document's title.
\returns the document's title, or an empty string if not available
\see set_title, info_key
*/
ustring document::get_title() const
{
if (d->is_locked) {
return ustring();
}
std::unique_ptr<GooString> goo_title(d->doc->getDocInfoTitle());
if (!goo_title.get()) {
return ustring();
}
return detail::unicode_GooString_to_ustring(goo_title.get());
}
/**
Sets the %document's title to \p title.
If \p title is empty, the %document's title is removed.
\returns true on success, false on failure
*/
bool document::set_title(const ustring &title)
{
if (d->is_locked) {
return false;
}
GooString *goo_title;
if (title.empty()) {
goo_title = nullptr;
} else {
goo_title = detail::ustring_to_unicode_GooString(title);
}
d->doc->setDocInfoTitle(goo_title);
return true;
}
/**
Gets the document's author.
\returns the document's author, or an empty string if not available
\see set_author, info_key
*/
ustring document::get_author() const
{
if (d->is_locked) {
return ustring();
}
std::unique_ptr<GooString> goo_author(d->doc->getDocInfoAuthor());
if (!goo_author.get()) {
return ustring();
}
return detail::unicode_GooString_to_ustring(goo_author.get());
}
/**
Sets the %document's author to \p author.
If \p author is empty, the %document's author is removed.
\returns true on success, false on failure
*/
bool document::set_author(const ustring &author)
{
if (d->is_locked) {
return false;
}
GooString *goo_author;
if (author.empty()) {
goo_author = nullptr;
} else {
goo_author = detail::ustring_to_unicode_GooString(author);
}
d->doc->setDocInfoAuthor(goo_author);
return true;
}
/**
Gets the document's subject.
\returns the document's subject, or an empty string if not available
\see set_subject, info_key
*/
ustring document::get_subject() const
{
if (d->is_locked) {
return ustring();
}
std::unique_ptr<GooString> goo_subject(d->doc->getDocInfoSubject());
if (!goo_subject.get()) {
return ustring();
}
return detail::unicode_GooString_to_ustring(goo_subject.get());
}
/**
Sets the %document's subject to \p subject.
If \p subject is empty, the %document's subject is removed.
\returns true on success, false on failure
*/
bool document::set_subject(const ustring &subject)
{
if (d->is_locked) {
return false;
}
GooString *goo_subject;
if (subject.empty()) {
goo_subject = nullptr;
} else {
goo_subject = detail::ustring_to_unicode_GooString(subject);
}
d->doc->setDocInfoSubject(goo_subject);
return true;
}
/**
Gets the document's keywords.
\returns the document's keywords, or an empty string if not available
\see set_keywords, info_key
*/
ustring document::get_keywords() const
{
if (d->is_locked) {
return ustring();
}
std::unique_ptr<GooString> goo_keywords(d->doc->getDocInfoKeywords());
if (!goo_keywords.get()) {
return ustring();
}
return detail::unicode_GooString_to_ustring(goo_keywords.get());
}
/**
Sets the %document's keywords to \p keywords.
If \p keywords is empty, the %document's keywords are removed.
\returns true on success, false on failure
*/
bool document::set_keywords(const ustring &keywords)
{
if (d->is_locked) {
return false;
}
GooString *goo_keywords;
if (keywords.empty()) {
goo_keywords = nullptr;
} else {
goo_keywords = detail::ustring_to_unicode_GooString(keywords);
}
d->doc->setDocInfoKeywords(goo_keywords);
return true;
}
/**
Gets the document's creator.
\returns the document's creator, or an empty string if not available
\see set_creator, info_key
*/
ustring document::get_creator() const
{
if (d->is_locked) {
return ustring();
}
std::unique_ptr<GooString> goo_creator(d->doc->getDocInfoCreator());
if (!goo_creator.get()) {
return ustring();
}
return detail::unicode_GooString_to_ustring(goo_creator.get());
}
/**
Sets the %document's creator to \p creator.
If \p creator is empty, the %document's creator is removed.
\returns true on success, false on failure
*/
bool document::set_creator(const ustring &creator)
{
if (d->is_locked) {
return false;
}
GooString *goo_creator;
if (creator.empty()) {
goo_creator = nullptr;
} else {
goo_creator = detail::ustring_to_unicode_GooString(creator);
}
d->doc->setDocInfoCreator(goo_creator);
return true;
}
/**
Gets the document's producer.
\returns the document's producer, or an empty string if not available
\see set_producer, info_key
*/
ustring document::get_producer() const
{
if (d->is_locked) {
return ustring();
}
std::unique_ptr<GooString> goo_producer(d->doc->getDocInfoProducer());
if (!goo_producer.get()) {
return ustring();
}
return detail::unicode_GooString_to_ustring(goo_producer.get());
}
/**
Sets the %document's producer to \p producer.
If \p producer is empty, the %document's producer is removed.
\returns true on success, false on failure
*/
bool document::set_producer(const ustring &producer)
{
if (d->is_locked) {
return false;
}
GooString *goo_producer;
if (producer.empty()) {
goo_producer = nullptr;
} else {
goo_producer = detail::ustring_to_unicode_GooString(producer);
}
d->doc->setDocInfoProducer(goo_producer);
return true;
}
/**
Gets the document's creation date as a time_type value.
\returns the document's creation date as a time_type value
\see set_creation_date, info_date
*/
time_type document::get_creation_date() const
{
if (d->is_locked) {
return time_type(-1);
}
std::unique_ptr<GooString> goo_creation_date(d->doc->getDocInfoCreatDate());
if (!goo_creation_date.get()) {
return time_type(-1);
}
return static_cast<time_type>(dateStringToTime(goo_creation_date.get()));
}
/**
Gets the document's creation date as a time_t value.
\returns the document's creation date as a time_t value
\see set_creation_date, info_date
*/
time_t document::get_creation_date_t() const
{
if (d->is_locked) {
return time_t(-1);
}
std::unique_ptr<GooString> goo_creation_date(d->doc->getDocInfoCreatDate());
if (!goo_creation_date.get()) {
return time_t(-1);
}
return dateStringToTime(goo_creation_date.get());
}
/**
Sets the %document's creation date to \p creation_date.
If \p creation_date == time_type(-1), the %document's creation date is removed.
\returns true on success, false on failure
*/
bool document::set_creation_date(time_type creation_date)
{
if (d->is_locked) {
return false;
}
GooString *goo_creation_date;
if (creation_date == time_type(-1)) {
goo_creation_date = nullptr;
} else {
time_t t = static_cast<time_t>(creation_date);
goo_creation_date = timeToDateString(&t);
}
d->doc->setDocInfoCreatDate(goo_creation_date);
return true;
}
/**
Sets the %document's creation date to \p creation_date.
If \p creation_date == time_t(-1), the %document's creation date is removed.
\returns true on success, false on failure
*/
bool document::set_creation_date_t(time_t creation_date)
{
if (d->is_locked) {
return false;
}
GooString *goo_creation_date;
if (creation_date == time_t(-1)) {
goo_creation_date = nullptr;
} else {
goo_creation_date = timeToDateString(&creation_date);
}
d->doc->setDocInfoCreatDate(goo_creation_date);
return true;
}
/**
Gets the document's modification date as a time_type value.
\returns the document's modification date as a time_type value
\see set_modification_date, info_date
*/
time_type document::get_modification_date() const
{
if (d->is_locked) {
return time_type(-1);
}
std::unique_ptr<GooString> goo_modification_date(d->doc->getDocInfoModDate());
if (!goo_modification_date.get()) {
return time_type(-1);
}
return static_cast<time_type>(dateStringToTime(goo_modification_date.get()));
}
/**
Gets the document's modification date as a time_t value.
\returns the document's modification date as a time_t value
\see set_modification_date, info_date
*/
time_t document::get_modification_date_t() const
{
if (d->is_locked) {
return time_t(-1);
}
std::unique_ptr<GooString> goo_modification_date(d->doc->getDocInfoModDate());
if (!goo_modification_date.get()) {
return time_t(-1);
}
return dateStringToTime(goo_modification_date.get());
}
/**
Sets the %document's modification date to \p mod_date.
If \p mod_date == time_type(-1), the %document's modification date is removed.
\returns true on success, false on failure
*/
bool document::set_modification_date(time_type mod_date)
{
if (d->is_locked) {
return false;
}
GooString *goo_mod_date;
if (mod_date == time_type(-1)) {
goo_mod_date = nullptr;
} else {
time_t t = static_cast<time_t>(mod_date);
goo_mod_date = timeToDateString(&t);
}
d->doc->setDocInfoModDate(goo_mod_date);
return true;
}
/**
Sets the %document's modification date to \p mod_date.
If \p mod_date == time_t(-1), the %document's modification date is removed.
\returns true on success, false on failure
*/
bool document::set_modification_date_t(time_t mod_date)
{
if (d->is_locked) {
return false;
}
GooString *goo_mod_date;
if (mod_date == time_t(-1)) {
goo_mod_date = nullptr;
} else {
goo_mod_date = timeToDateString(&mod_date);
}
d->doc->setDocInfoModDate(goo_mod_date);
return true;
}
/**
Removes the %document's Info dictionary.
\returns true on success, false on failure
*/
bool document::remove_info()
{
if (d->is_locked) {
return false;
}
d->doc->removeDocInfo();
return true;
}
/**
\returns whether the document is encrypted
*/
bool document::is_encrypted() const
{
return d->doc->isEncrypted();
}
/**
\returns whether the document is linearized
*/
bool document::is_linearized() const
{
return d->doc->isLinearized();
}
/**
Check for available "document permission".
\returns whether the specified permission is allowed
*/
bool document::has_permission(permission_enum which) const
{
switch (which) {
case perm_print:
return d->doc->okToPrint();
case perm_change:
return d->doc->okToChange();
case perm_copy:
return d->doc->okToCopy();
case perm_add_notes:
return d->doc->okToAddNotes();
case perm_fill_forms:
return d->doc->okToFillForm();
case perm_accessibility:
return d->doc->okToAccessibility();
case perm_assemble:
return d->doc->okToAssemble();
case perm_print_high_resolution:
return d->doc->okToPrintHighRes();
}
return true;
}
/**
Reads the %document metadata string.
\return the %document metadata string
*/
ustring document::metadata() const
{
std::unique_ptr<GooString> md(d->doc->getCatalog()->readMetadata());
if (md.get()) {
return detail::unicode_GooString_to_ustring(md.get());
}
return ustring();
}
/**
Gets the IDs of the current PDF %document, if available.
\param permanent_id if not NULL, will be set to the permanent ID of the %document
\param update_id if not NULL, will be set to the update ID of the %document
\returns whether the document has the IDs
\since 0.16
*/
bool document::get_pdf_id(std::string *permanent_id, std::string *update_id) const
{
GooString goo_permanent_id;
GooString goo_update_id;
if (!d->doc->getID(permanent_id ? &goo_permanent_id : nullptr, update_id ? &goo_update_id : nullptr)) {
return false;
}
if (permanent_id) {
*permanent_id = goo_permanent_id.c_str();
}
if (update_id) {
*update_id = goo_update_id.c_str();
}
return true;
}
/**
Document page count.
\returns the number of pages of the document
*/
int document::pages() const
{
return d->doc->getNumPages();
}
/**
Document page by label reading.
This creates a new page representing the %document %page whose label is the
specified \p label. If there is no page with that \p label, NULL is returned.
\returns a new page object or NULL
*/
page *document::create_page(const ustring &label) const
{
std::unique_ptr<GooString> goolabel(detail::ustring_to_unicode_GooString(label));
int index = 0;
if (!d->doc->getCatalog()->labelToIndex(goolabel.get(), &index)) {
return nullptr;
}
return create_page(index);
}
/**
Document page by index reading.
This creates a new page representing the \p index -th %page of the %document.
\note the page indexes are in the range [0, pages()[.
\returns a new page object or NULL
*/
page *document::create_page(int index) const
{
if (index >= 0 && index < d->doc->getNumPages()) {
page *p = new page(d, index);
if (p->d->page) {
return p;
} else {
delete p;
return nullptr;
}
} else {
return nullptr;
}
}
/**
Reads all the font information of the %document.
\note this can be slow for big documents; prefer the use of a font_iterator
to read incrementally page by page
\see create_font_iterator
*/
std::vector<font_info> document::fonts() const
{
std::vector<font_info> result;
font_iterator it(0, d);
while (it.has_next()) {
const std::vector<font_info> l = it.next();
std::copy(l.begin(), l.end(), std::back_inserter(result));
}
return result;
}
/**
Creates a new font iterator.
This creates a new font iterator for reading the font information of the
%document page by page, starting at the specified \p start_page (0 if not
specified).
\returns a new font iterator
*/
font_iterator *document::create_font_iterator(int start_page) const
{
return new font_iterator(start_page, d);
}
/**
Reads the TOC (table of contents) of the %document.
\returns a new toc object if a TOC is available, NULL otherwise
*/
toc *document::create_toc() const
{
return toc_private::load_from_outline(d->doc->getOutline());
}
/**
Reads whether the current document has %document-level embedded files
(attachments).
This is a very fast way to know whether there are embedded files (also known
as "attachments") at the %document-level. Note this does not take into account
files embedded in other ways (e.g. to annotations).
\returns whether the document has embedded files
*/
bool document::has_embedded_files() const
{
return d->doc->getCatalog()->numEmbeddedFiles() > 0;
}
/**
Reads all the %document-level embedded files of the %document.
\returns the %document-level embedded files
*/
std::vector<embedded_file *> document::embedded_files() const
{
if (d->is_locked) {
return std::vector<embedded_file *>();
}
if (d->embedded_files.empty() && d->doc->getCatalog()->numEmbeddedFiles() > 0) {
const int num = d->doc->getCatalog()->numEmbeddedFiles();
d->embedded_files.resize(num);
for (int i = 0; i < num; ++i) {
std::unique_ptr<FileSpec> fs = d->doc->getCatalog()->embeddedFile(i);
d->embedded_files[i] = embedded_file_private::create(std::move(fs));
}
}
return d->embedded_files;
}
/**
Creates a map of all the named destinations in the %document.
\note The destination names may contain \\0 and other binary values
so they are not printable and cannot convert to null-terminated C strings.
\returns the map of the each name and destination
\since 0.74
*/
std::map<std::string, destination> document::create_destination_map() const
{
std::map<std::string, destination> m;
Catalog *catalog = d->doc->getCatalog();
if (!catalog) {
return m;
}
// Iterate from name-dict
const int nDests = catalog->numDests();
for (int i = 0; i < nDests; ++i) {
std::string key(catalog->getDestsName(i));
std::unique_ptr<LinkDest> link_dest = catalog->getDestsDest(i);
if (link_dest) {
destination dest(new destination_private(link_dest.get(), d->doc));
m.emplace(std::move(key), std::move(dest));
}
}
// Iterate from name-tree
const int nDestsNameTree = catalog->numDestNameTree();
for (int i = 0; i < nDestsNameTree; ++i) {
std::string key(catalog->getDestNameTreeName(i)->c_str(), catalog->getDestNameTreeName(i)->getLength());
std::unique_ptr<LinkDest> link_dest = catalog->getDestNameTreeDest(i);
if (link_dest) {
destination dest(new destination_private(link_dest.get(), d->doc));
m.emplace(std::move(key), std::move(dest));
}
}
return m;
}
/**
Saves the %document to file \p file_name.
\returns true on success, false on failure
*/
bool document::save(const std::string &file_name) const
{
if (d->is_locked) {
return false;
}
GooString fname(file_name.c_str());
return d->doc->saveAs(fname) == errNone;
}
/**
Saves the original version of the %document to file \p file_name.
\returns true on success, false on failure
*/
bool document::save_a_copy(const std::string &file_name) const
{
if (d->is_locked) {
return false;
}
GooString fname(file_name.c_str());
return d->doc->saveWithoutChangesAs(fname) == errNone;
}
/**
Tries to load a PDF %document from the specified file.
\param file_name the file to open
\returns a new document if the load succeeded (even if the document is locked),
NULL otherwise
*/
document *document::load_from_file(const std::string &file_name, const std::string &owner_password, const std::string &user_password)
{
document_private *doc = new document_private(std::make_unique<GooString>(file_name.c_str()), owner_password, user_password);
return document_private::check_document(doc, nullptr);
}
/**
Tries to load a PDF %document from the specified data.
\note if the loading succeeds, the document takes ownership of the
\p file_data (swap()ing it)
\param file_data the data representing a document to open
\returns a new document if the load succeeded (even if the document is locked),
NULL otherwise
*/
document *document::load_from_data(byte_array *file_data, const std::string &owner_password, const std::string &user_password)
{
if (!file_data || file_data->size() < 10) {
return nullptr;
}
document_private *doc = new document_private(file_data, owner_password, user_password);
return document_private::check_document(doc, file_data);
}
/**
Tries to load a PDF %document from the specified data buffer.
\note the buffer must remain valid for the whole lifetime of the returned
document
\param file_data the data buffer representing a document to open
\param file_data_length the length of the data buffer
\returns a new document if the load succeeded (even if the document is locked),
NULL otherwise
\since 0.16
*/
document *document::load_from_raw_data(const char *file_data, int file_data_length, const std::string &owner_password, const std::string &user_password)
{
if (!file_data || file_data_length < 10) {
return nullptr;
}
document_private *doc = new document_private(file_data, file_data_length, owner_password, user_password);
return document_private::check_document(doc, nullptr);
}
|