Skip to content

Commit 64d4070

Browse files
authored
[US-1143] New utility functions used in examples. (#290)
* UniPDF version updated. * Updated examples printing PDF objects. * Update merge and split examples.
1 parent dcedf79 commit 64d4070

File tree

10 files changed

+29
-544
lines changed

10 files changed

+29
-544
lines changed

analysis/pdf_all_objects.go

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"os"
1313

1414
"github.com/unidoc/unipdf/v4/common/license"
15-
"github.com/unidoc/unipdf/v4/core"
1615
"github.com/unidoc/unipdf/v4/model"
1716
)
1817

@@ -42,46 +41,18 @@ func main() {
4241
inputPath := args[0]
4342

4443
fmt.Printf("Input file: %s\n", inputPath)
45-
err := inspectPdf(inputPath, opt)
46-
if err != nil {
47-
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
48-
os.Exit(1)
49-
}
50-
}
51-
52-
func inspectPdf(inputPath string, opt cmdOptions) error {
5344
readerOpts := model.NewReaderOpts()
5445
readerOpts.Password = opt.pdfPassword
5546

5647
pdfReader, f, err := model.NewPdfReaderFromFile(inputPath, readerOpts)
5748
if err != nil {
58-
return err
49+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
50+
os.Exit(1)
5951
}
6052
defer f.Close()
61-
62-
objNums := pdfReader.GetObjectNums()
63-
64-
// Output.
65-
fmt.Printf("%d PDF objects:\n", len(objNums))
66-
for i, objNum := range objNums {
67-
obj, err := pdfReader.GetIndirectObjectByNumber(objNum)
68-
if err != nil {
69-
return err
70-
}
71-
fmt.Println("=========================================================")
72-
fmt.Printf("%3d: %d 0 %T\n", i, objNum, obj)
73-
if stream, is := obj.(*core.PdfObjectStream); is {
74-
decoded, err := core.DecodeStream(stream)
75-
if err != nil {
76-
return err
77-
}
78-
fmt.Printf("Decoded:\n%s\n", decoded)
79-
} else if indObj, is := obj.(*core.PdfIndirectObject); is {
80-
fmt.Printf("%T\n", indObj.PdfObject)
81-
fmt.Printf("%s\n", indObj.PdfObject.String())
82-
}
83-
53+
err = pdfReader.PrintPdfObjects(os.Stdout)
54+
if err != nil {
55+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
56+
os.Exit(1)
8457
}
85-
86-
return nil
8758
}

analysis/pdf_get_object.go

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"strconv"
1414

1515
"github.com/unidoc/unipdf/v4/common/license"
16-
"github.com/unidoc/unipdf/v4/core"
1716
"github.com/unidoc/unipdf/v4/model"
1817
)
1918

@@ -46,48 +45,15 @@ func main() {
4645
}
4746

4847
fmt.Printf("Input file: %s\n", inputPath)
49-
err := inspectPdfObject(inputPath, objNum)
48+
pdfReader, f, err := model.NewPdfReaderFromFile(inputPath, nil)
5049
if err != nil {
5150
fmt.Printf("Error: %v\n", err)
5251
os.Exit(1)
5352
}
54-
}
55-
56-
func inspectPdfObject(inputPath string, objNum int) error {
57-
pdfReader, f, err := model.NewPdfReaderFromFile(inputPath, nil)
58-
if err != nil {
59-
return err
60-
}
6153
defer f.Close()
62-
63-
// Print trailer
64-
if objNum == -1 {
65-
trailer, err := pdfReader.GetTrailer()
66-
if err != nil {
67-
return err
68-
}
69-
70-
fmt.Printf("Trailer: %s\n", trailer.String())
71-
return nil
72-
}
73-
74-
obj, err := pdfReader.GetIndirectObjectByNumber(objNum)
54+
err = pdfReader.PrintPdfObject(os.Stdout, objNum)
7555
if err != nil {
76-
return err
77-
}
78-
79-
fmt.Printf("Object %d: %s\n", objNum, obj.String())
80-
81-
if stream, is := obj.(*core.PdfObjectStream); is {
82-
decoded, err := core.DecodeStream(stream)
83-
if err != nil {
84-
return err
85-
}
86-
fmt.Printf("Decoded:\n%s", decoded)
87-
} else if indObj, is := obj.(*core.PdfIndirectObject); is {
88-
fmt.Printf("%T\n", indObj.PdfObject)
89-
fmt.Printf("%s\n", indObj.PdfObject.String())
56+
fmt.Printf("Error: %v\n", err)
57+
os.Exit(1)
9058
}
91-
92-
return nil
9359
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/unidoc/globalsign-dss v0.0.0-20220330092912-b69d85b63736
1414
github.com/unidoc/pkcs7 v0.3.0
1515
github.com/unidoc/unichart v0.5.1
16-
github.com/unidoc/unipdf/v4 v4.3.0
16+
github.com/unidoc/unipdf/v4 v4.4.0
1717
golang.org/x/crypto v0.41.0
1818
golang.org/x/image v0.30.0
1919
golang.org/x/text v0.28.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ github.com/unidoc/unichart v0.5.1 h1:qnYavwBV5sg9NUF59KbMOqJdh2kA454nVxdDTPPtSz8
136136
github.com/unidoc/unichart v0.5.1/go.mod h1:/8yJsL49OqBOyG53JFVZOwwDXDquo/ZRMkfz9fNsVgc=
137137
github.com/unidoc/unipdf/v4 v4.3.0 h1:eA4zjRHTULtV5thy3MausfFYDP1i59qGdsfxe709oUY=
138138
github.com/unidoc/unipdf/v4 v4.3.0/go.mod h1:oR0EX7TmS7KaAuzFQPA9t9HjbU4f2NbWMvzXNqtXo70=
139+
github.com/unidoc/unipdf/v4 v4.4.0 h1:JvfRBjQgaT1FkdUiZXmrGLp5RFhk9GB/NfHEzsiU4i0=
140+
github.com/unidoc/unipdf/v4 v4.4.0/go.mod h1:oR0EX7TmS7KaAuzFQPA9t9HjbU4f2NbWMvzXNqtXo70=
139141
github.com/unidoc/unitype v0.5.1 h1:UwTX15K6bktwKocWVvLoijIeu4JAVEAIeFqMOjvxqQs=
140142
github.com/unidoc/unitype v0.5.1/go.mod h1:3dxbRL+f1otNqFQIRHho8fxdg3CcUKrqS8w1SXTsqcI=
141143
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=

pages/pdf_merge.go

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"os"
1414

1515
"github.com/unidoc/unipdf/v4/common/license"
16-
"github.com/unidoc/unipdf/v4/model"
16+
"github.com/unidoc/unipdf/v4/pdfutil"
1717
)
1818

1919
func init() {
@@ -47,56 +47,12 @@ func main() {
4747
inputPaths = append(inputPaths, arg)
4848
}
4949

50-
err := mergePdf(inputPaths, outputPath)
50+
// Merge pdfs without forms.
51+
err := pdfutil.MergePdf(inputPaths, outputPath, false)
5152
if err != nil {
5253
fmt.Printf("Error: %v\n", err)
5354
os.Exit(1)
5455
}
5556

5657
fmt.Printf("Complete, see output file: %s\n", outputPath)
5758
}
58-
59-
func mergePdf(inputPaths []string, outputPath string) error {
60-
pdfWriter := model.NewPdfWriter()
61-
62-
for _, inputPath := range inputPaths {
63-
pdfReader, f, err := model.NewPdfReaderFromFile(inputPath, nil)
64-
if err != nil {
65-
return err
66-
}
67-
defer f.Close()
68-
69-
numPages, err := pdfReader.GetNumPages()
70-
if err != nil {
71-
return err
72-
}
73-
74-
for i := 0; i < numPages; i++ {
75-
pageNum := i + 1
76-
77-
page, err := pdfReader.GetPage(pageNum)
78-
if err != nil {
79-
return err
80-
}
81-
82-
err = pdfWriter.AddPage(page)
83-
if err != nil {
84-
return err
85-
}
86-
}
87-
}
88-
89-
fWrite, err := os.Create(outputPath)
90-
if err != nil {
91-
return err
92-
}
93-
94-
defer fWrite.Close()
95-
96-
err = pdfWriter.Write(fWrite)
97-
if err != nil {
98-
return err
99-
}
100-
101-
return nil
102-
}

0 commit comments

Comments
 (0)