Merge branch 'main' into feature/image-appearance

This commit is contained in:
2025-05-12 22:14:06 +08:00
8 changed files with 35 additions and 18 deletions

View File

@@ -28,9 +28,10 @@ func (context *SignContext) createCatalog() ([]byte, error) {
// written in the PDF file (for example, /1.4).
//
// If an incremental upgrade requires a version that is higher than specified by the document.
// if context.PDFReader.PDFVersion < "2.0" {
// catalog_buffer.WriteString(" /Version /2.0")
// }
// Ensure PDF version is at least 1.5 to support SigFlags in acroFormDict (1.4) and UF in the fileSpecDict (1.5)
if v, err := strconv.ParseFloat(context.PDFReader.PDFVersion, 64); err == nil && v < 1.5 {
catalog_buffer.WriteString(" /Version /1.5\n")
}
// Retrieve the root, its pointer and set the root string
root := context.PDFReader.Trailer().Key("Root")

View File

@@ -21,11 +21,11 @@ var testFiles = []struct {
},
},
{
file: "../testfiles/testfile21.pdf",
file: "../testfiles/testfile12.pdf",
expectedCatalogs: map[CertType]string{
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 /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 /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",
CertificationSignature: "<<\n /Type /Catalog\n /Version /1.5\n /Outlines 2 0 R\n /Pages 3 0 R\n /AcroForm <<\n /Fields [16 0 R]\n /SigFlags 3\n >>\n>>\n",
UsageRightsSignature: "<<\n /Type /Catalog\n /Version /1.5\n /Outlines 2 0 R\n /Pages 3 0 R\n /AcroForm <<\n /Fields [16 0 R]\n /SigFlags 1\n >>\n>>\n",
ApprovalSignature: "<<\n /Type /Catalog\n /Version /1.5\n /Outlines 2 0 R\n /Pages 3 0 R\n /AcroForm <<\n /Fields [16 0 R]\n /SigFlags 3\n >>\n>>\n",
},
},
}

View File

@@ -43,7 +43,7 @@ func (context *SignContext) writeTrailer() error {
lines[i] = " " + strings.TrimSpace(line)
}
}
trailer_string = strings.Join(lines, "\n")
trailer_string = strings.Join(lines, "\n") + "\n"
// Write the new trailer.
if _, err := context.OutputBuffer.Write([]byte(trailer_string)); err != nil {
@@ -54,7 +54,6 @@ func (context *SignContext) writeTrailer() error {
return err
}
}
// Write the new xref start position.
if _, err := context.OutputBuffer.Write([]byte(strconv.FormatInt(context.NewXrefStart, 10) + "\n")); err != nil {
return err

View File

@@ -128,9 +128,25 @@ func (context *SignContext) createIncPageUpdate(pageNumber, annot uint32) ([]byt
// TODO: Update digitorus/pdf to get raw values without resolving pointers
for _, key := range page.Keys() {
switch key {
case "Contents", "Parent":
case "Parent":
ptr := page.Key(key).GetPtr()
page_buffer.WriteString(fmt.Sprintf(" /%s %d 0 R\n", key, ptr.GetID()))
case "Contents":
// Special handling for Contents - must preserve stream structure
contentsValue := page.Key(key)
if contentsValue.Kind() == pdf.Array {
// If Contents is an array, keep it as an array reference
page_buffer.WriteString(" /Contents [")
for i := 0; i < contentsValue.Len(); i++ {
ptr := contentsValue.Index(i).GetPtr()
page_buffer.WriteString(fmt.Sprintf(" %d 0 R", ptr.GetID()))
}
page_buffer.WriteString(" ]\n")
} else {
// If Contents is a single reference, keep it as a single reference
ptr := contentsValue.GetPtr()
page_buffer.WriteString(fmt.Sprintf(" /%s %d 0 R\n", key, ptr.GetID()))
}
case "Annots":
page_buffer.WriteString(" /Annots [\n")
for i := 0; i < page.Key("Annots").Len(); i++ {

View File

@@ -20,7 +20,6 @@ func TestGetLastObjectIDFromXref(t *testing.T) {
{"testfile16.pdf", 567},
{"testfile17.pdf", 20},
{"testfile20.pdf", 10},
{"testfile21.pdf", 16},
}
for _, tc := range testCases {