Update PDF annotations when changing bookmark text (#7411)

This commit is contained in:
Toromtomtom
2021-03-23 09:31:52 +00:00
committed by GitHub
parent dbb4bbbee4
commit 3706196bfe
4 changed files with 83 additions and 26 deletions

View File

@@ -176,34 +176,11 @@ function PdfDocument:getPageLinks(pageno)
end
function PdfDocument:saveHighlight(pageno, item)
local suffix = util.getFileNameSuffix(self.file)
if string.lower(suffix) ~= "pdf" then return end
local can_write = self:_checkIfWritable()
if can_write ~= true then return can_write end
if self.is_writable == nil then
local handle = io.open(self.file, 'r+b')
self.is_writable = handle ~= nil
if handle then handle:close() end
end
if self.is_writable == false then
return false
end
self.is_edited = true
-- will also need mupdf_h.lua to be evaluated once
-- but this is guaranteed at this point
local n = #item.pboxes
local quadpoints = ffi.new("float[?]", 8*n)
for i=1, n do
-- The order must be left bottom, right bottom, left top, right top.
-- https://bugs.ghostscript.com/show_bug.cgi?id=695130
quadpoints[8*i-8] = item.pboxes[i].x
quadpoints[8*i-7] = item.pboxes[i].y + item.pboxes[i].h
quadpoints[8*i-6] = item.pboxes[i].x + item.pboxes[i].w
quadpoints[8*i-5] = item.pboxes[i].y + item.pboxes[i].h
quadpoints[8*i-4] = item.pboxes[i].x
quadpoints[8*i-3] = item.pboxes[i].y
quadpoints[8*i-2] = item.pboxes[i].x + item.pboxes[i].w
quadpoints[8*i-1] = item.pboxes[i].y
end
local quadpoints, n = self:_quadpointsFromPboxes(item.pboxes)
local page = self._document:openPage(pageno)
local annot_type = C.PDF_ANNOT_HIGHLIGHT
if item.drawer == "lighten" then
@@ -217,6 +194,66 @@ function PdfDocument:saveHighlight(pageno, item)
page:close()
end
function Document:deleteHighlight(pageno, item)
local can_write = self:_checkIfWritable()
if can_write ~= true then return can_write end
self.is_edited = true
local quadpoints, n = self:_quadpointsFromPboxes(item.pboxes)
local page = self._document:openPage(pageno)
local annot = page:getMarkupAnnotation(quadpoints, n)
if annot ~= nil then
page:deleteMarkupAnnotation(annot)
end
page:close()
end
function PdfDocument:updateHighlightContents(pageno, item, contents)
local can_write = self:_checkIfWritable()
if can_write ~= true then return can_write end
self.is_edited = true
local quadpoints, n = self:_quadpointsFromPboxes(item.pboxes)
local page = self._document:openPage(pageno)
local annot = page:getMarkupAnnotation(quadpoints, n)
if annot ~= nil then
page:updateMarkupAnnotation(annot, contents)
end
page:close()
end
-- returns nil if file is not a pdf, true if document is a writable pdf, false else
function PdfDocument:_checkIfWritable()
local suffix = util.getFileNameSuffix(self.file)
if string.lower(suffix) ~= "pdf" then return nil end
if self.is_writable == nil then
local handle = io.open(self.file, 'r+b')
self.is_writable = handle ~= nil
if handle then handle:close() end
end
return self.is_writable
end
function PdfDocument:_quadpointsFromPboxes(pboxes)
-- will also need mupdf_h.lua to be evaluated once
-- but this is guaranteed at this point
local n = #pboxes
local quadpoints = ffi.new("float[?]", 8*n)
for i=1, n do
-- The order must be left bottom, right bottom, left top, right top.
-- https://bugs.ghostscript.com/show_bug.cgi?id=695130
quadpoints[8*i-8] = pboxes[i].x
quadpoints[8*i-7] = pboxes[i].y + pboxes[i].h
quadpoints[8*i-6] = pboxes[i].x + pboxes[i].w
quadpoints[8*i-5] = pboxes[i].y + pboxes[i].h
quadpoints[8*i-4] = pboxes[i].x
quadpoints[8*i-3] = pboxes[i].y
quadpoints[8*i-2] = pboxes[i].x + pboxes[i].w
quadpoints[8*i-1] = pboxes[i].y
end
return quadpoints, n
end
function PdfDocument:writeDocument()
logger.info("writing document to", self.file)
self._document:writeDocument(self.file)