Removing code that is no longer necessary
This commit is contained in:
@@ -3,20 +3,18 @@ package sign
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/digitorus/pdf"
|
|
||||||
"io"
|
"io"
|
||||||
"slices"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/digitorus/pdf"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (context *SignContext) createCatalog() ([]byte, error) {
|
func (context *SignContext) createCatalog() ([]byte, error) {
|
||||||
var overwrittenCatalogKeys []string
|
|
||||||
var catalog_buffer bytes.Buffer
|
var catalog_buffer bytes.Buffer
|
||||||
|
|
||||||
// Start the catalog object
|
// Start the catalog object
|
||||||
catalog_buffer.WriteString("<<\n")
|
catalog_buffer.WriteString("<<\n")
|
||||||
catalog_buffer.WriteString(" /Type /Catalog\n")
|
catalog_buffer.WriteString(" /Type /Catalog\n")
|
||||||
overwrittenCatalogKeys = append(overwrittenCatalogKeys, "Type")
|
|
||||||
|
|
||||||
// (Optional; PDF 1.4) The version of the PDF specification to which
|
// (Optional; PDF 1.4) The version of the PDF specification to which
|
||||||
// the document conforms (for example, 1.4) if later than the version
|
// the document conforms (for example, 1.4) if later than the version
|
||||||
@@ -34,38 +32,21 @@ func (context *SignContext) createCatalog() ([]byte, error) {
|
|||||||
// catalog_buffer.WriteString(" /Version /2.0")
|
// catalog_buffer.WriteString(" /Version /2.0")
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Retrieve the root and check for necessary keys in one loop
|
// Retrieve the root, its pointer and set the root string
|
||||||
root := context.PDFReader.Trailer().Key("Root")
|
root := context.PDFReader.Trailer().Key("Root")
|
||||||
rootPtr := root.GetPtr()
|
rootPtr := root.GetPtr()
|
||||||
context.CatalogData.RootString = strconv.Itoa(int(rootPtr.GetID())) + " " + strconv.Itoa(int(rootPtr.GetGen())) + " R"
|
context.CatalogData.RootString = strconv.Itoa(int(rootPtr.GetID())) + " " + strconv.Itoa(int(rootPtr.GetGen())) + " R"
|
||||||
|
|
||||||
foundPages, foundNames := false, false
|
// Copy over existing catalog entries except for type and AcroForum
|
||||||
for _, key := range root.Keys() {
|
for _, key := range root.Keys() {
|
||||||
switch key {
|
if key != "Type" && key != "AcroForm" {
|
||||||
case "Pages":
|
_, _ = fmt.Fprintf(&catalog_buffer, " /%s ", key)
|
||||||
foundPages = true
|
context.serializeCatalogEntry(&catalog_buffer, rootPtr.GetID(), root.Key(key))
|
||||||
case "Names":
|
catalog_buffer.WriteString("\n")
|
||||||
foundNames = true
|
|
||||||
}
|
}
|
||||||
if foundPages && foundNames {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add Pages and Names references if they exist
|
|
||||||
if foundPages {
|
|
||||||
pages := root.Key("Pages").GetPtr()
|
|
||||||
catalog_buffer.WriteString(" /Pages " + strconv.Itoa(int(pages.GetID())) + " " + strconv.Itoa(int(pages.GetGen())) + " R\n")
|
|
||||||
overwrittenCatalogKeys = append(overwrittenCatalogKeys, "Pages")
|
|
||||||
}
|
|
||||||
if foundNames {
|
|
||||||
names := root.Key("Names").GetPtr()
|
|
||||||
catalog_buffer.WriteString(" /Names " + strconv.Itoa(int(names.GetID())) + " " + strconv.Itoa(int(names.GetGen())) + " R\n")
|
|
||||||
overwrittenCatalogKeys = append(overwrittenCatalogKeys, "Names")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the AcroForm dictionary with /NeedAppearances
|
// Start the AcroForm dictionary with /NeedAppearances
|
||||||
overwrittenCatalogKeys = append(overwrittenCatalogKeys, "AcroForm")
|
|
||||||
catalog_buffer.WriteString(" /AcroForm <<\n")
|
catalog_buffer.WriteString(" /AcroForm <<\n")
|
||||||
catalog_buffer.WriteString(" /Fields [")
|
catalog_buffer.WriteString(" /Fields [")
|
||||||
|
|
||||||
@@ -128,15 +109,7 @@ func (context *SignContext) createCatalog() ([]byte, error) {
|
|||||||
|
|
||||||
// Finalize the AcroForm and Catalog object
|
// Finalize the AcroForm and Catalog object
|
||||||
catalog_buffer.WriteString(" >>\n") // Close AcroForm
|
catalog_buffer.WriteString(" >>\n") // Close AcroForm
|
||||||
|
catalog_buffer.WriteString(">>\n") // Close Catalog
|
||||||
// Copy over existing catalog entries from the original document that we did not need to override.
|
|
||||||
for _, key := range root.Keys() {
|
|
||||||
if !slices.Contains(overwrittenCatalogKeys, key) {
|
|
||||||
_, _ = fmt.Fprintf(&catalog_buffer, "/%s ", key)
|
|
||||||
context.serializeCatalogEntry(&catalog_buffer, rootPtr.GetID(), root.Key(key))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catalog_buffer.WriteString(">>\n") // Close Catalog
|
|
||||||
|
|
||||||
return catalog_buffer.Bytes(), nil
|
return catalog_buffer.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
@@ -15,17 +15,17 @@ var testFiles = []struct {
|
|||||||
{
|
{
|
||||||
file: "../testfiles/testfile20.pdf",
|
file: "../testfiles/testfile20.pdf",
|
||||||
expectedCatalogs: map[CertType]string{
|
expectedCatalogs: map[CertType]string{
|
||||||
CertificationSignature: "<<\n /Type /Catalog\n /Pages 3 0 R\n /AcroForm <<\n /Fields [10 0 R]\n /SigFlags 3\n >>\n/Metadata 2 0 R>>\n",
|
CertificationSignature: "<<\n /Type /Catalog\n /Metadata 2 0 R\n /Pages 3 0 R\n /AcroForm <<\n /Fields [10 0 R]\n /SigFlags 3\n >>\n>>\n",
|
||||||
UsageRightsSignature: "<<\n /Type /Catalog\n /Pages 3 0 R\n /AcroForm <<\n /Fields [10 0 R]\n /SigFlags 1\n >>\n/Metadata 2 0 R>>\n",
|
UsageRightsSignature: "<<\n /Type /Catalog\n /Metadata 2 0 R\n /Pages 3 0 R\n /AcroForm <<\n /Fields [10 0 R]\n /SigFlags 1\n >>\n>>\n",
|
||||||
ApprovalSignature: "<<\n /Type /Catalog\n /Pages 3 0 R\n /AcroForm <<\n /Fields [10 0 R]\n /SigFlags 3\n >>\n/Metadata 2 0 R>>\n",
|
ApprovalSignature: "<<\n /Type /Catalog\n /Metadata 2 0 R\n /Pages 3 0 R\n /AcroForm <<\n /Fields [10 0 R]\n /SigFlags 3\n >>\n>>\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
file: "../testfiles/testfile21.pdf",
|
file: "../testfiles/testfile21.pdf",
|
||||||
expectedCatalogs: map[CertType]string{
|
expectedCatalogs: map[CertType]string{
|
||||||
CertificationSignature: "<<\n /Type /Catalog\n /Pages 9 0 R\n /Names 6 0 R\n /AcroForm <<\n /Fields [16 0 R]\n /SigFlags 3\n >>\n/Metadata 8 0 R>>\n",
|
CertificationSignature: "<<\n /Type /Catalog\n /Metadata 8 0 R\n /Names 6 0 R\n /Pages 9 0 R\n /AcroForm <<\n /Fields [16 0 R]\n /SigFlags 3\n >>\n>>\n",
|
||||||
UsageRightsSignature: "<<\n /Type /Catalog\n /Pages 9 0 R\n /Names 6 0 R\n /AcroForm <<\n /Fields [16 0 R]\n /SigFlags 1\n >>\n/Metadata 8 0 R>>\n",
|
UsageRightsSignature: "<<\n /Type /Catalog\n /Metadata 8 0 R\n /Names 6 0 R\n /Pages 9 0 R\n /AcroForm <<\n /Fields [16 0 R]\n /SigFlags 1\n >>\n>>\n",
|
||||||
ApprovalSignature: "<<\n /Type /Catalog\n /Pages 9 0 R\n /Names 6 0 R\n /AcroForm <<\n /Fields [16 0 R]\n /SigFlags 3\n >>\n/Metadata 8 0 R>>\n",
|
ApprovalSignature: "<<\n /Type /Catalog\n /Metadata 8 0 R\n /Names 6 0 R\n /Pages 9 0 R\n /AcroForm <<\n /Fields [16 0 R]\n /SigFlags 3\n >>\n>>\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user