diff options
author | Jane Liu <janeliulwq@google.com> | 2017-06-29 13:40:22 -0400 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-06-29 20:11:24 +0000 |
commit | 8ce58f522e046ae3b1561d592ca7b3cd7c894731 (patch) | |
tree | 571c245639b8321dae3c3a1254e317d59ee2b8bc /fpdfsdk | |
parent | f1eae2c37cb4bbf7a536cf251706147bbac51880 (diff) | |
download | pdfium-8ce58f522e046ae3b1561d592ca7b3cd7c894731.tar.xz |
Added FPDFAnnot_RemoveAnnot()
Added an API for removing annotation and an embedder test for it.
Bug=pdfium:737
Change-Id: I6f01625e8103078b83967a57a5c1a7a26bc0c70a
Reviewed-on: https://pdfium-review.googlesource.com/7039
Commit-Queue: Jane Liu <janeliulwq@google.com>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Diffstat (limited to 'fpdfsdk')
-rw-r--r-- | fpdfsdk/fpdfannot.cpp | 13 | ||||
-rw-r--r-- | fpdfsdk/fpdfannot_embeddertest.cpp | 69 | ||||
-rw-r--r-- | fpdfsdk/fpdfview_c_api_test.c | 1 |
3 files changed, 83 insertions, 0 deletions
diff --git a/fpdfsdk/fpdfannot.cpp b/fpdfsdk/fpdfannot.cpp index 4a288b7df7..3d003d738e 100644 --- a/fpdfsdk/fpdfannot.cpp +++ b/fpdfsdk/fpdfannot.cpp @@ -201,6 +201,19 @@ DLLEXPORT void STDCALL FPDFPage_CloseAnnot(FPDF_ANNOTATION annot) { delete CPDFAnnotContextFromFPDFAnnotation(annot); } +DLLEXPORT FPDF_BOOL STDCALL FPDFPage_RemoveAnnot(FPDF_PAGE page, int index) { + CPDF_Page* pPage = CPDFPageFromFPDFPage(page); + if (!pPage || !pPage->m_pFormDict || index < 0) + return false; + + CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots"); + if (!pAnnots || static_cast<size_t>(index) >= pAnnots->GetCount()) + return false; + + pAnnots->RemoveAt(index); + return true; +} + DLLEXPORT FPDF_ANNOTATION_SUBTYPE STDCALL FPDFAnnot_GetSubtype(FPDF_ANNOTATION annot) { if (!annot) diff --git a/fpdfsdk/fpdfannot_embeddertest.cpp b/fpdfsdk/fpdfannot_embeddertest.cpp index 9d9e2d365f..85fe1326bc 100644 --- a/fpdfsdk/fpdfannot_embeddertest.cpp +++ b/fpdfsdk/fpdfannot_embeddertest.cpp @@ -392,3 +392,72 @@ TEST_F(FPDFAnnotEmbeddertest, ModifyRectQuadpointsWithAP) { FPDFPage_CloseAnnot(annot); UnloadPage(page); } + +TEST_F(FPDFAnnotEmbeddertest, RemoveAnnotation) { + // Open a file with 3 annotations on its first page. + ASSERT_TRUE(OpenDocument("annotation_ink_multiple.pdf")); + FPDF_PAGE page = FPDF_LoadPage(document(), 0); + ASSERT_TRUE(page); + EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); + + // Check that the annotations have the expected rectangle coordinates. + FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); + FS_RECTF rect = FPDFAnnot_GetRect(annot); + EXPECT_NEAR(86.1971f, rect.left, 0.001f); + FPDFPage_CloseAnnot(annot); + + annot = FPDFPage_GetAnnot(page, 1); + rect = FPDFAnnot_GetRect(annot); + EXPECT_NEAR(149.8127f, rect.left, 0.001f); + FPDFPage_CloseAnnot(annot); + + annot = FPDFPage_GetAnnot(page, 2); + rect = FPDFAnnot_GetRect(annot); + EXPECT_NEAR(351.8204f, rect.left, 0.001f); + FPDFPage_CloseAnnot(annot); + + // Check that nothing happens when attempting to remove an annotation with an + // out-of-bound index. + EXPECT_FALSE(FPDFPage_RemoveAnnot(page, 4)); + EXPECT_FALSE(FPDFPage_RemoveAnnot(page, -1)); + EXPECT_EQ(3, FPDFPage_GetAnnotCount(page)); + + // Remove the second annotation. + EXPECT_TRUE(FPDFPage_RemoveAnnot(page, 1)); + EXPECT_EQ(2, FPDFPage_GetAnnotCount(page)); + EXPECT_FALSE(FPDFPage_GetAnnot(page, 2)); + + // Save the document, closing the page and document. + EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); + FPDF_ClosePage(page); + + // Open the saved document. + std::string new_file = GetString(); + FPDF_FILEACCESS file_access; + memset(&file_access, 0, sizeof(file_access)); + file_access.m_FileLen = new_file.size(); + file_access.m_GetBlock = GetBlockFromString; + file_access.m_Param = &new_file; + FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); + ASSERT_TRUE(new_doc); + FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); + ASSERT_TRUE(new_page); + + // Check that the saved document has 2 annotations on the first page. + EXPECT_EQ(2, FPDFPage_GetAnnotCount(new_page)); + + // Check that the remaining 2 annotations are the original 1st and 3rd ones by + // verifying their rectangle coordinates. + annot = FPDFPage_GetAnnot(new_page, 0); + rect = FPDFAnnot_GetRect(annot); + EXPECT_NEAR(86.1971f, rect.left, 0.001f); + FPDFPage_CloseAnnot(annot); + + annot = FPDFPage_GetAnnot(new_page, 1); + rect = FPDFAnnot_GetRect(annot); + EXPECT_NEAR(351.8204f, rect.left, 0.001f); + FPDFPage_CloseAnnot(annot); + + FPDF_ClosePage(new_page); + FPDF_CloseDocument(new_doc); +} diff --git a/fpdfsdk/fpdfview_c_api_test.c b/fpdfsdk/fpdfview_c_api_test.c index 05242c6fe1..f9a3c387c2 100644 --- a/fpdfsdk/fpdfview_c_api_test.c +++ b/fpdfsdk/fpdfview_c_api_test.c @@ -40,6 +40,7 @@ int CheckPDFiumCApi() { CHK(FPDFPage_GetAnnotCount); CHK(FPDFPage_GetAnnot); CHK(FPDFPage_CloseAnnot); + CHK(FPDFPage_RemoveAnnot); CHK(FPDFAnnot_GetSubtype); CHK(FPDFAnnot_SetColor); CHK(FPDFAnnot_GetColor); |