| 1 |
// SPDX-FileCopyrightText: 2026 Francesco Pretto <[email protected]> |
| 2 |
// SPDX-License-Identifier: MIT-0 |
| 3 |
|
| 4 |
#include <PdfTest.h> |
| 5 |
|
| 6 |
using namespace std; |
| 7 |
using namespace PoDoFo; |
| 8 |
|
| 9 |
TEST_CASE("TestAppendDocument") |
| 10 |
{ |
| 11 |
PdfMemDocument srcDoc; |
| 12 |
srcDoc.Load(TestUtils::GetTestInputFilePath("TechDocs", "Acrobat_SignatureCreationQuickKeyAll.pdf")); |
| 13 |
|
| 14 |
PdfMemDocument dstDoc; |
| 15 |
dstDoc.Load(TestUtils::GetTestInputFilePath("TechDocs", "Acrobat_DigitalSignatures_in_PDF.pdf")); |
| 16 |
|
| 17 |
unsigned dstPageCount = dstDoc.GetPages().GetCount(); |
| 18 |
unsigned srcPageCount = srcDoc.GetPages().GetCount(); |
| 19 |
|
| 20 |
dstDoc.GetPages().AppendDocumentPages(srcDoc); |
| 21 |
|
| 22 |
REQUIRE(dstDoc.GetPages().GetCount() == dstPageCount + srcPageCount); |
| 23 |
|
| 24 |
string outputPath = TestUtils::GetTestOutputFilePath("TestAppendDocument.pdf"); |
| 25 |
dstDoc.Save(outputPath); |
| 26 |
|
| 27 |
// Reload and verify |
| 28 |
PdfMemDocument reloaded; |
| 29 |
reloaded.Load(outputPath); |
| 30 |
REQUIRE(reloaded.GetPages().GetCount() == dstPageCount + srcPageCount); |
| 31 |
} |
| 32 |
|
| 33 |
TEST_CASE("TestFillXObjectMiniature") |
| 34 |
{ |
| 35 |
PdfMemDocument srcDoc; |
| 36 |
srcDoc.Load(TestUtils::GetTestInputFilePath("TechDocs", "Acrobat_SignatureCreationQuickKeyAll.pdf")); |
| 37 |
|
| 38 |
auto& srcPage = srcDoc.GetPages().GetPageAt(0); |
| 39 |
Rect srcBox = srcPage.GetMediaBox(); |
| 40 |
|
| 41 |
PdfMemDocument dstDoc; |
| 42 |
|
| 43 |
// Create an XObject filled from the source page |
| 44 |
auto xobj = dstDoc.CreateXObjectForm(Rect()); |
| 45 |
xobj->FillFromPage(srcPage); |
| 46 |
|
| 47 |
// Create a page sized to fit 4 miniatures (2x2 grid) |
| 48 |
double pageWidth = srcBox.Width; |
| 49 |
double pageHeight = srcBox.Height; |
| 50 |
auto& dstPage = dstDoc.GetPages().CreatePage(Rect(0, 0, pageWidth, pageHeight)); |
| 51 |
|
| 52 |
// Scale to roughly a quarter of the page (half width, half height) |
| 53 |
double scale = 0.5; |
| 54 |
double miniHeight = srcBox.Height * scale; |
| 55 |
|
| 56 |
PdfPainter painter; |
| 57 |
painter.SetCanvas(dstPage); |
| 58 |
// Draw at top-left quadrant |
| 59 |
painter.DrawXObject(*xobj, 0, pageHeight - miniHeight, scale, scale); |
| 60 |
painter.FinishDrawing(); |
| 61 |
|
| 62 |
string outputPath = TestUtils::GetTestOutputFilePath("TestFillXObjectMiniature.pdf"); |
| 63 |
dstDoc.Save(outputPath); |
| 64 |
|
| 65 |
// Reload and verify basic structure |
| 66 |
PdfMemDocument reloaded; |
| 67 |
reloaded.Load(outputPath); |
| 68 |
REQUIRE(reloaded.GetPages().GetCount() == 1); |
| 69 |
Rect reloadedBox = reloaded.GetPages().GetPageAt(0).GetMediaBox(); |
| 70 |
ASSERT_EQUAL(reloadedBox.Width, pageWidth); |
| 71 |
ASSERT_EQUAL(reloadedBox.Height, pageHeight); |
| 72 |
} |
| 73 |
|
| 74 |
TEST_CASE("TestAppendInheritedAttributes") |
| 75 |
{ |
| 76 |
// Start from a blank document and make its page inherit /Resources |
| 77 |
// from the /Pages tree root, with an indirect reference inside it |
| 78 |
PdfMemDocument srcDoc; |
| 79 |
srcDoc.Load(TestUtils::GetTestInputFilePath("blank.pdf")); |
| 80 |
|
| 81 |
auto& marker = srcDoc.GetObjects().CreateDictionaryObject(); |
| 82 |
marker.GetDictionary().AddKey("MarkerTag"_n, PdfString("INHERITED_MARKER")); |
| 83 |
|
| 84 |
auto& xobjects = srcDoc.GetObjects().CreateDictionaryObject(); |
| 85 |
xobjects.GetDictionary().AddKeyIndirect("XMarker"_n, marker); |
| 86 |
|
| 87 |
auto& resources = srcDoc.GetObjects().CreateDictionaryObject(); |
| 88 |
resources.GetDictionary().AddKeyIndirect("XObject"_n, xobjects); |
| 89 |
|
| 90 |
srcDoc.GetPages().GetPageAt(0).GetObject().GetDictionary().RemoveKey("Resources"); |
| 91 |
srcDoc.GetPages().GetObject().GetDictionary().AddKeyIndirect("Resources"_n, resources); |
| 92 |
|
| 93 |
// Append into a fresh destination so new object numbers overlap the |
| 94 |
// source ones, which would corrupt references if remapping happened twice |
| 95 |
PdfMemDocument dstDoc; |
| 96 |
dstDoc.GetPages().AppendDocumentPages(srcDoc); |
| 97 |
|
| 98 |
REQUIRE(dstDoc.GetPages().GetCount() == 1); |
| 99 |
|
| 100 |
auto& newPage = dstDoc.GetPages().GetPageAt(0); |
| 101 |
auto* res = newPage.GetDictionary().FindKeyParent("Resources"); |
| 102 |
REQUIRE(res != nullptr); |
| 103 |
auto* xobjDict = res->GetDictionary().FindKey("XObject"); |
| 104 |
REQUIRE(xobjDict != nullptr); |
| 105 |
auto* markerResolved = xobjDict->GetDictionary().FindKey("XMarker"); |
| 106 |
REQUIRE(markerResolved != nullptr); |
| 107 |
auto* tag = markerResolved->GetDictionary().FindKey("MarkerTag"); |
| 108 |
REQUIRE(tag != nullptr); |
| 109 |
REQUIRE(tag->GetString().GetString() == "INHERITED_MARKER"); |
| 110 |
} |