| 1 | /** |
| 2 | * Copyright (C) 2009 by Dominik Seichter <[email protected]> |
| 3 | * Copyright (C) 2021 by Francesco Pretto <[email protected]> |
| 4 | * |
| 5 | * Licensed under GNU Library General Public 2.0 or later. |
| 6 | * Some rights reserved. See COPYING, AUTHORS. |
| 7 | */ |
| 8 | |
| 9 | #include <PdfTest.h> |
| 10 | |
| 11 | using namespace std; |
| 12 | using namespace PoDoFo; |
| 13 | |
| 14 | TEST_CASE("TestEmptyContentsStream") |
| 15 | { |
| 16 | PdfMemDocument doc; |
| 17 | auto& page1 = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); |
| 18 | REQUIRE(page1.GetDictionary().MustGetKey("Parent").GetReference() == doc.GetPages().GetObject().GetIndirectReference()); |
| 19 | auto& annot1 = page1.GetAnnotations().CreateAnnot<PdfAnnotationPopup>(Rect(300.0, 20.0, 250.0, 50.0)); |
| 20 | PdfString title("Author: Dominik Seichter"); |
| 21 | annot1.SetContents(title); |
| 22 | annot1.SetOpen(true); |
| 23 | |
| 24 | string filename = TestUtils::GetTestOutputFilePath("testEmptyContentsStream.pdf"); |
| 25 | doc.Save(filename); |
| 26 | |
| 27 | // Read annotation again |
| 28 | PdfMemDocument doc2; |
| 29 | doc2.Load(filename); |
| 30 | REQUIRE(doc2.GetPages().GetCount() == 1); |
| 31 | auto& page2 = doc2.GetPages().GetPageAt(0); |
| 32 | REQUIRE(page2.GetAnnotations().GetCount() == 1); |
| 33 | auto& annot2 = page2.GetAnnotations().GetAnnotAt(0); |
| 34 | REQUIRE(annot2.GetContents() == title); |
| 35 | |
| 36 | auto& pageObj = page2.GetObject(); |
| 37 | REQUIRE(!pageObj.GetDictionary().HasKey("Contents")); |
| 38 | } |
| 39 | |
| 40 | TEST_CASE("TestRotations") |
| 41 | { |
| 42 | // The two documents are rotated but still portrait |
| 43 | PdfMemDocument doc; |
| 44 | { |
| 45 | doc.Load(TestUtils::GetTestInputFilePath("blank-rotated-90.pdf")); |
| 46 | auto& page = doc.GetPages().GetPageAt(0); |
| 47 | REQUIRE(page.GetRect() == Rect(0, 0, 595, 842)); |
| 48 | REQUIRE(page.GetRectRaw() == Rect(0, 0, 842, 595)); |
| 49 | auto& annot = page.GetAnnotations().CreateAnnot<PdfAnnotationWatermark>(Rect(100, 600, 80, 20)); |
| 50 | REQUIRE(annot.GetRect() == Rect(100, 600, 80, 20)); |
| 51 | REQUIRE(annot.GetRectRaw() == Rect(222, 99.999999999999986, 20, 79.999999999999986)); |
| 52 | page.SetRect(Rect(0, 0, 500, 800)); |
| 53 | REQUIRE(page.GetRect() == Rect(0, 0, 500, 800)); |
| 54 | REQUIRE(page.GetRectRaw() == Rect(0, 0, 800, 500)); |
| 55 | } |
| 56 | |
| 57 | { |
| 58 | doc.Load(TestUtils::GetTestInputFilePath("blank-rotated-270.pdf")); |
| 59 | auto& page = doc.GetPages().GetPageAt(0); |
| 60 | REQUIRE(page.GetRect() == Rect(0, 0, 595, 842)); |
| 61 | REQUIRE(page.GetRectRaw() == Rect(0, 0, 842, 595)); |
| 62 | auto& annot = page.GetAnnotations().CreateAnnot<PdfAnnotationWatermark>(Rect(100, 600, 80, 20)); |
| 63 | REQUIRE(annot.GetRect() == Rect(100, 600, 80, 20)); |
| 64 | REQUIRE(annot.GetRectRaw() == Rect(600.00000000000011, 415, 20, 80)); |
| 65 | annot.SetRect(Rect(100, 500, 100, 30)); |
| 66 | REQUIRE(annot.GetRect() == Rect(100, 500.00000000000006, 100, 29.999999999999943)); |
| 67 | REQUIRE(annot.GetRectRaw() == Rect(500.00000000000011, 395, 30, 100)); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | TEST_CASE("TestFlattening") |
| 72 | { |
| 73 | PdfMemDocument doc; |
| 74 | doc.Load(TestUtils::GetTestInputFilePath("TechDocs", "pdf_implementation.pdf")); |
| 75 | doc.GetPages().FlattenStructure(); |
| 76 | auto pageRootRef = doc.GetPages().GetObject().GetIndirectReference(); |
| 77 | auto& dict = doc.GetPages().GetDictionary(); |
| 78 | REQUIRE(dict.GetKey("Count")->GetNumber() == 11); |
| 79 | auto& kidsArr = dict.MustFindKey("Kids").GetArray(); |
| 80 | REQUIRE(kidsArr.GetSize() == 11); |
| 81 | for (unsigned i = 0; i < dict.GetSize(); i++) |
| 82 | { |
| 83 | auto& child = kidsArr.MustFindAt(i); |
| 84 | REQUIRE(child.GetDictionary().MustGetKey("Type").GetName() == "Page"); |
| 85 | REQUIRE(child.GetDictionary().MustGetKey("Parent").GetReference() == pageRootRef); |
| 86 | } |
| 87 | } |