Add creator test case for checking referenced page destinations

This commit is contained in:
Adrian-George Bostan 2020-06-18 17:17:52 +03:00
parent ce74077390
commit 5fb6a070f1
2 changed files with 74 additions and 3 deletions

View File

@ -3067,6 +3067,77 @@ func TestPageLabels(t *testing.T) {
require.Equal(t, core.EqualObjects(genPageLabels, pageLabels), true)
}
func TestReferencedPageDestinations(t *testing.T) {
testPages := func(buf *bytes.Buffer, expectedPages, expectedNullDestPages int) {
reader, err := model.NewPdfReader(bytes.NewReader(buf.Bytes()))
require.NoError(t, err)
// Check number of pages in catalog.
numPages, err := reader.GetNumPages()
require.NoError(t, err)
require.Equal(t, expectedPages, numPages)
// Check outline destionation pages.
outlines, err := reader.GetOutlines()
require.NoError(t, err)
var nullDestPages int
var validDestPages int
for _, entry := range outlines.Entries {
pageObj := entry.Dest.PageObj
require.NotNil(t, pageObj)
if core.IsNullObject(entry.Dest.PageObj) {
nullDestPages++
continue
}
_, _, err := reader.PageFromIndirectObject(pageObj)
require.NoError(t, err)
validDestPages++
}
require.Equal(t, expectedPages, validDestPages)
require.Equal(t, expectedNullDestPages, nullDestPages)
}
// Generate and test input file.
c := New()
c.AddTOC = true
numPages := 10
for i := 0; i < numPages; i++ {
chapter := c.NewChapter(fmt.Sprintf("Chapter %d", i+1))
paragraph := c.NewParagraph(fmt.Sprintf("Content for chapter %d", i+1))
chapter.Add(paragraph)
require.NoError(t, c.Draw(chapter))
if i < numPages-1 {
c.NewPage()
}
}
buf := bytes.NewBuffer(nil)
require.NoError(t, c.Write(buf))
testPages(buf, 11, 0)
// Generate and test split input file.
reader, err := model.NewPdfReader(bytes.NewReader(buf.Bytes()))
require.NoError(t, err)
writer := model.NewPdfWriter()
for i, page := range reader.PageList {
if i%2 == 0 {
require.NoError(t, writer.AddPage(page))
}
}
writer.AddOutlineTree(reader.GetOutlineTree())
buf = bytes.NewBuffer(nil)
require.NoError(t, writer.Write(buf))
testPages(buf, 6, 5)
}
var errRenderNotSupported = errors.New("rendering pdf is not supported on this system")
// renderPDFToPNGs uses ghostscript (gs) to render specified PDF file into a set of PNG images (one per page).

View File

@ -308,9 +308,9 @@ func (w *PdfWriter) copyObject(obj core.PdfObject,
objectToObjectCopyMap[obj] = newObj
streamObj.PdfObjectDictionary = w.copyObject(t.PdfObjectDictionary, objectToObjectCopyMap, skipMap, skip).(*core.PdfObjectDictionary)
case *core.PdfObjectDictionary:
// Check if the object is a page dictionary and search it the writer
// pages. If not found, replace it with a null object and add the
// chain of children objects to the skip map.
// Check if the object is a page dictionary and search it in the
// writer pages. If not found, replace it with a null object and add
// the chain of children objects to the skip map.
var unused bool
if skipUnusedPages && !skip {
if dictType, _ := core.GetNameVal(t.Get("Type")); dictType == "Page" {