|
| 1 | +/** |
| 2 | + * This example demonstrates how to copy pages from an existing PDF file to a new PDF file |
| 3 | + * and preserve the structure tree information. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * go run pdf_copy_page_with_accessibility.go INPUT_PDF_PATH OUTPUT_PDF_PATH SPACE_SEPARATED_LIST_OF_PAGE_NUMBER_TO_COPY |
| 7 | + */ |
| 8 | + |
| 9 | +package main |
| 10 | + |
| 11 | +import ( |
| 12 | + "fmt" |
| 13 | + "os" |
| 14 | + "strconv" |
| 15 | + |
| 16 | + "github.com/unidoc/unipdf/v3/common/license" |
| 17 | + "github.com/unidoc/unipdf/v3/core" |
| 18 | + "github.com/unidoc/unipdf/v3/creator" |
| 19 | + "github.com/unidoc/unipdf/v3/model" |
| 20 | +) |
| 21 | + |
| 22 | +func init() { |
| 23 | + // Make sure to load your metered License API key prior to using the library. |
| 24 | + // If you need a key, you can sign up and create a free one at https://cloud.unidoc.io |
| 25 | + err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)) |
| 26 | + if err != nil { |
| 27 | + panic(err) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func main() { |
| 32 | + args := os.Args |
| 33 | + if len(args) < 3 { |
| 34 | + fmt.Printf("Usage: %s INPUT_PDF_PATH OUTPUT_PDF_PATH SPACE_SEPARATED_LIST_OF_PAGE_NUMBER_TO_COPY\n", os.Args[0]) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + inputPath := args[1] |
| 39 | + outputPath := args[2] |
| 40 | + pageNumbers := args[3:] |
| 41 | + |
| 42 | + c := creator.New() |
| 43 | + c.SetLanguage("en-US") |
| 44 | + c.SetPdfWriterAccessFunc(func(w *model.PdfWriter) error { |
| 45 | + w.SetCatalogMarkInfo(core.MakeDictMap(map[string]core.PdfObject{ |
| 46 | + "Marked": core.MakeBool(true), |
| 47 | + })) |
| 48 | + |
| 49 | + w.SetVersion(1, 5) |
| 50 | + |
| 51 | + return nil |
| 52 | + }) |
| 53 | + |
| 54 | + // Set the viewer preferences. |
| 55 | + vp := model.NewViewerPreferences() |
| 56 | + vp.SetDisplayDocTitle(true) |
| 57 | + |
| 58 | + c.SetViewerPreferences(vp) |
| 59 | + |
| 60 | + // Set PDF title. |
| 61 | + model.SetPdfTitle("Output Sample PDF") |
| 62 | + |
| 63 | + // Construct the StructTreeRoot. |
| 64 | + newStr := model.NewStructTreeRoot() |
| 65 | + |
| 66 | + // Construct base K dictionary. |
| 67 | + docK := model.NewKDictionary() |
| 68 | + docK.S = core.MakeName(model.StructureTypeDocument) |
| 69 | + |
| 70 | + newStr.AddKDict(docK) |
| 71 | + |
| 72 | + readerOpt := model.ReaderOpts{ |
| 73 | + ComplianceMode: true, |
| 74 | + } |
| 75 | + |
| 76 | + r, _, err := model.NewPdfReaderFromFile(inputPath, &readerOpt) |
| 77 | + if err != nil { |
| 78 | + fmt.Printf("Error loading input file: %v\n", err) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + // Get the original `StructTreeRoot` object. |
| 83 | + strObj, found := r.GetCatalogStructTreeRoot() |
| 84 | + if !found { |
| 85 | + fmt.Printf("No StructTreeRoot found in the input PDF\n") |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + orgStr, err := model.NewStructTreeRootFromPdfObject(strObj) |
| 90 | + if err != nil { |
| 91 | + fmt.Printf("Error parsing StructTreeRoot object: %v\n", err) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + orgK := orgStr.K[0].GetChildren() |
| 96 | + |
| 97 | + newPageIdx := 0 |
| 98 | + |
| 99 | + for _, i := range pageNumbers { |
| 100 | + n, err := strconv.Atoi(i) |
| 101 | + if err != nil { |
| 102 | + fmt.Printf("Invalid page number: %s\n", i) |
| 103 | + continue |
| 104 | + } |
| 105 | + |
| 106 | + orgPage, err := r.GetPage(n) |
| 107 | + if err != nil { |
| 108 | + fmt.Printf("Error getting page %d: %v\n", n, err) |
| 109 | + continue |
| 110 | + } |
| 111 | + |
| 112 | + dupPage := orgPage.Duplicate() |
| 113 | + dupPage.SetStructParentsKey(newPageIdx) |
| 114 | + c.AddPage(dupPage) |
| 115 | + |
| 116 | + // Add section K object to store all original K objects from template page. |
| 117 | + sectK := model.NewKDictionary() |
| 118 | + sectK.S = core.MakeName(model.StructureTypeSection) |
| 119 | + sectK.T = core.MakeString(fmt.Sprintf("Page %d", n)) |
| 120 | + sectK.GenerateRandomID() |
| 121 | + |
| 122 | + sectKv := model.KValue{} |
| 123 | + sectKv.SetKDict(sectK) |
| 124 | + |
| 125 | + // Copy all K objects from the original page to the new page. |
| 126 | + for _, k := range orgK { |
| 127 | + copyK := deepCopyKObject(k) |
| 128 | + |
| 129 | + if copyK != nil { |
| 130 | + sectK.AddChild(copyK) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + setKPageNumber(§Kv, int64(n)) |
| 135 | + |
| 136 | + docK.AddChild(§Kv) |
| 137 | + |
| 138 | + newPageIdx++ |
| 139 | + } |
| 140 | + |
| 141 | + c.SetStructTreeRoot(newStr) |
| 142 | + |
| 143 | + err = c.WriteToFile(outputPath) |
| 144 | + if err != nil { |
| 145 | + fmt.Printf("Error writing output file: %v\n", err) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func setKPageNumber(kv *model.KValue, page int64) { |
| 150 | + if k := kv.GetKDict(); k != nil { |
| 151 | + k.SetPageNumber(page) |
| 152 | + |
| 153 | + for _, child := range k.GetChildren() { |
| 154 | + setKPageNumber(child, page) |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func deepCopyKObject(origin *model.KValue) *model.KValue { |
| 160 | + copyKVal := model.NewKValue() |
| 161 | + |
| 162 | + if oKDict := origin.GetKDict(); oKDict != nil { |
| 163 | + copy := model.NewKDictionary() |
| 164 | + copy.S = oKDict.S |
| 165 | + copy.ID = oKDict.ID |
| 166 | + copy.Lang = oKDict.Lang |
| 167 | + copy.Alt = oKDict.Alt |
| 168 | + copy.T = oKDict.T |
| 169 | + copy.Pg = oKDict.Pg |
| 170 | + copy.C = oKDict.C |
| 171 | + |
| 172 | + for _, child := range oKDict.GetChildren() { |
| 173 | + copy.AddChild(deepCopyKObject(child)) |
| 174 | + } |
| 175 | + |
| 176 | + copyKVal.SetKDict(copy) |
| 177 | + |
| 178 | + return copyKVal |
| 179 | + } else if refObj := origin.GetRefObject(); refObj != nil { |
| 180 | + copyKVal.SetRefObject(refObj) |
| 181 | + |
| 182 | + return copyKVal |
| 183 | + } else if mcid := origin.GetMCID(); mcid != nil { |
| 184 | + copyKVal.SetMCID(*mcid) |
| 185 | + |
| 186 | + return copyKVal |
| 187 | + } |
| 188 | + |
| 189 | + return nil |
| 190 | +} |
0 commit comments