|
| 1 | +__author__ = 'fahadadeel' |
| 2 | +import jpype |
| 3 | +import re |
| 4 | +import datetime |
| 5 | + |
| 6 | +class AddJavascript: |
| 7 | + def __init__(self, dataDir): |
| 8 | + self.dataDir = dataDir |
| 9 | + self.Document = jpype.JClass("com.aspose.pdf.Document") |
| 10 | + self.JavascriptAction=jpype.JClass("com.aspose.pdf.JavascriptAction") |
| 11 | + |
| 12 | + def main(self): |
| 13 | + |
| 14 | + # Open a pdf document. |
| 15 | + doc= self.Document() |
| 16 | + pdf = self.Document() |
| 17 | + pdf=self.dataDir + 'Template.pdf' |
| 18 | + |
| 19 | + |
| 20 | + # Adding JavaScript at Document Level |
| 21 | + # Instantiate JavascriptAction with desried JavaScript statement |
| 22 | + javaScript = self.JavascriptAction("this.print({bUI:true,bSilent:false,bShrinkToFit:true});"); |
| 23 | + |
| 24 | + # Assign JavascriptAction object to desired action of Document |
| 25 | + doc.setOpenAction(javaScript) |
| 26 | + js=self.JavascriptAction("app.alert('page 2 is opened')") |
| 27 | + |
| 28 | + # Adding JavaScript at Page Level |
| 29 | + doc.getPages.get_Item(2) |
| 30 | + doc.getActions().setOnOpen(js()) |
| 31 | + doc.getPages().get_Item(2).getActions().setOnClose(self.JavascriptAction("app.alert('page 2 is closed')")) |
| 32 | + |
| 33 | + # Save PDF Document |
| 34 | + doc.save(self.dataDir + "JavaScript-Added.pdf") |
| 35 | + |
| 36 | + print "Added JavaScript Successfully, please check the output file." |
| 37 | + |
| 38 | +class AddToc: |
| 39 | + def __init__(self, dataDir): |
| 40 | + self.dataDir = dataDir |
| 41 | + self.Document = jpype.JClass("com.aspose.pdf.Document") |
| 42 | + self.TocInfo=jpype.JClass("com.aspose.pdf.TocInfo") |
| 43 | + self.TextFragment=jpype.JClass("com.aspose.pdf.TextFragment") |
| 44 | + self.TextSegment=jpype.JClass("com.aspose.pdf.TextSegment") |
| 45 | + self.Heading=jpype.JClass("com.aspose.pdf.Heading") |
| 46 | + |
| 47 | + def main(self): |
| 48 | + # Open a pdf document. |
| 49 | + doc= self.Document() |
| 50 | + pdf = self.Document() |
| 51 | + pdf=self.dataDir + 'input1.pdf' |
| 52 | + |
| 53 | + # Get access to first page of PDF file |
| 54 | + toc_page = doc.getPages().insert(1) |
| 55 | + |
| 56 | + # Create object to represent TOC information |
| 57 | + toc_info = self.TocInfo() |
| 58 | + title = self.TextFragment("Table Of Contents") |
| 59 | + title.getTextState().setFontSize(20) |
| 60 | + |
| 61 | + # Set the title for TOC |
| 62 | + toc_info.setTitle(title) |
| 63 | + toc_page.setTocInfo(toc_info) |
| 64 | + |
| 65 | + # Create string objects which will be used as TOC elements |
| 66 | + titles = ["First page", "Second page"] |
| 67 | + |
| 68 | + i = 0; |
| 69 | + while (i < 2): |
| 70 | + # Create Heading object |
| 71 | + heading2 = self.Heading(1); |
| 72 | + |
| 73 | + segment2 = self.TextSegment |
| 74 | + heading2.setTocPage(toc_page) |
| 75 | + heading2.getSegments().add(segment2) |
| 76 | + |
| 77 | + # Specify the destination page for heading object |
| 78 | + heading2.setDestinationPage(doc.getPages().get_Item(i + 2)) |
| 79 | + |
| 80 | + # Destination page |
| 81 | + heading2.setTop(doc.getPages().get_Item(i + 2).getRect().getHeight()) |
| 82 | + |
| 83 | + # Destination coordinate |
| 84 | + segment2.setText(titles[i]) |
| 85 | + |
| 86 | + # Add heading to page containing TOC |
| 87 | + toc_page.getParagraphs().add(heading2) |
| 88 | + |
| 89 | + i +=1; |
| 90 | + |
| 91 | + |
| 92 | + # Save PDF Document |
| 93 | + doc.save(self.dataDir + "TOC.pdf") |
| 94 | + |
| 95 | + print "Added TOC Successfully, please check the output file." |
| 96 | + |
| 97 | +class GetDocumentWindow: |
| 98 | + def __init__(self, dataDir): |
| 99 | + self.dataDir = dataDir |
| 100 | + self.Document = jpype.JClass("com.aspose.pdf.Document") |
| 101 | + |
| 102 | + def main(self): |
| 103 | + |
| 104 | + doc= self.Document() |
| 105 | + pdf = self.Document() |
| 106 | + pdf=self.dataDir + 'input1.pdf' |
| 107 | + |
| 108 | + # Get different document properties |
| 109 | + # Position of document's window - Default: false |
| 110 | + print "CenterWindow :- " + str(doc.getCenterWindow()) |
| 111 | + |
| 112 | + # Predominant reading order; determine the position of page |
| 113 | + # when displayed side by side - Default: L2R |
| 114 | + print "Direction :- " + str(doc.getDirection()) |
| 115 | + |
| 116 | + # Whether window's title bar should display document title. |
| 117 | + # If false, title bar displays PDF file name - Default: false |
| 118 | + print "DisplayDocTitle :- " + str(doc.getDisplayDocTitle()) |
| 119 | + |
| 120 | + #Whether to resize the document's window to fit the size of |
| 121 | + #first displayed page - Default: false |
| 122 | + print "FitWindow :- " + str(doc.getFitWindow()) |
| 123 | + |
| 124 | + # Whether to hide menu bar of the viewer application - Default: false |
| 125 | + print "HideMenuBar :-" + str(doc.getHideMenubar()) |
| 126 | + |
| 127 | + # Whether to hide tool bar of the viewer application - Default: false |
| 128 | + print "HideToolBar :-" + str(doc.getHideToolBar()) |
| 129 | + |
| 130 | + # Whether to hide UI elements like scroll bars |
| 131 | + # and leaving only the page contents displayed - Default: false |
| 132 | + print "HideWindowUI :-" + str(doc.getHideWindowUI()) |
| 133 | + |
| 134 | + # The document's page mode. How to display document on exiting full-screen mode. |
| 135 | + print "NonFullScreenPageMode :-" + str(doc.getNonFullScreenPageMode()) |
| 136 | + |
| 137 | + # The page layout i.e. single page, one column |
| 138 | + print "PageLayout :-" + str(doc.getPageLayout()) |
| 139 | + |
| 140 | + #How the document should display when opened. |
| 141 | + print "pageMode :-" + str(doc.getPageMode()) |
| 142 | + |
| 143 | +class GetPdfFileInfo: |
| 144 | + def __init__(self, dataDir): |
| 145 | + self.dataDir = dataDir |
| 146 | + self.Document = jpype.JClass("com.aspose.pdf.Document") |
| 147 | + |
| 148 | + def main(self): |
| 149 | + |
| 150 | + doc= self.Document() |
| 151 | + pdf = self.Document() |
| 152 | + pdf=self.dataDir + 'input1.pdf' |
| 153 | + |
| 154 | + # Get document information |
| 155 | + doc_info = doc.getInfo(); |
| 156 | + |
| 157 | + # Show document information |
| 158 | + print "Author:-" + str(doc_info.getAuthor()) |
| 159 | + print "Creation Date:-" + str(doc_info.getCreationDate()) |
| 160 | + print "Keywords:-" + str(doc_info.getKeywords()) |
| 161 | + print "Modify Date:-" + str(doc_info.getModDate()) |
| 162 | + print "Subject:-" + str(doc_info.getSubject()) |
| 163 | + print "Title:-" + str(doc_info.getTitle()) |
| 164 | + |
| 165 | +class GetXMPMetadata: |
| 166 | + def __init__(self, dataDir): |
| 167 | + self.dataDir = dataDir |
| 168 | + self.Document = jpype.JClass("com.aspose.pdf.Document") |
| 169 | + |
| 170 | + def main(self): |
| 171 | + |
| 172 | + doc= self.Document() |
| 173 | + pdf = self.Document() |
| 174 | + pdf=self.dataDir + 'input1.pdf' |
| 175 | + |
| 176 | + # Get properties |
| 177 | + print "xmp:CreateDate: " + str(doc.getMetadata().get_Item("xmp:CreateDate")) |
| 178 | + print "xmp:Nickname: " + str(doc.getMetadata().get_Item("xmp:Nickname")) |
| 179 | + print "xmp:CustomProperty: " + str(doc.getMetadata().get_Item("xmp:CustomProperty")) |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +class Optimize: |
| 184 | + def __init__(self, dataDir): |
| 185 | + self.dataDir = dataDir |
| 186 | + self.Document = jpype.JClass("com.aspose.pdf.Document") |
| 187 | +# self.OptimizationOptions=jpype.JClass("com.aspose.pdf.Document.OptimizationOptions") |
| 188 | + |
| 189 | + def main(self): |
| 190 | + |
| 191 | + doc= self.Document() |
| 192 | + pdf = self.Document() |
| 193 | + pdf=self.dataDir + 'input1.pdf' |
| 194 | + |
| 195 | + # Optimize for web |
| 196 | + doc.optimize(); |
| 197 | + |
| 198 | + #Save output document |
| 199 | + doc.save(self.dataDir + "Optimized_Web.pdf") |
| 200 | + |
| 201 | + print "Optimized PDF for the Web, please check output file." |
| 202 | + |
| 203 | +class RemoveMetadata: |
| 204 | + def __init__(self, dataDir): |
| 205 | + self.dataDir = dataDir |
| 206 | + self.Document = jpype.JClass("com.aspose.pdf.Document") |
| 207 | + |
| 208 | + def main(self): |
| 209 | + |
| 210 | + doc= self.Document() |
| 211 | + pdf = self.Document() |
| 212 | + pdf=self.dataDir + 'input1.pdf' |
| 213 | + |
| 214 | + if (re.findall('/pdfaid:part/',doc.getMetadata())): |
| 215 | + doc.getMetadata().removeItem("pdfaid:part") |
| 216 | + |
| 217 | + |
| 218 | + if (re.findall('/dc:format/',doc.getMetadata())): |
| 219 | + doc.getMetadata().removeItem("dc:format") |
| 220 | + |
| 221 | + |
| 222 | + # save update document with new information |
| 223 | + doc.save(self.dataDir + "Remove_Metadata.pdf") |
| 224 | + |
| 225 | + print "Removed metadata successfully, please check output file." |
| 226 | + |
| 227 | +class SetExpiration: |
| 228 | + |
| 229 | + def __init__(self, dataDir): |
| 230 | + self.dataDir = dataDir |
| 231 | + self.Document = jpype.JClass("com.aspose.pdf.Document") |
| 232 | + self.JavascriptAction=jpype.JClass("com.aspose.pdf.JavascriptAction") |
| 233 | + |
| 234 | + def main(self): |
| 235 | + doc= self.Document() |
| 236 | + pdf = self.Document() |
| 237 | + pdf=self.dataDir + 'input1.pdf' |
| 238 | + |
| 239 | + javascript = self.JavascriptAction( |
| 240 | + "var year=2014; var month=4;today = new Date();today = new Date(today.getFullYear(), today.getMonth());expiry = new Date(year, month);if (today.getTime() > expiry.getTime())app.alert('The file is expired. You need a new one.');"); |
| 241 | + |
| 242 | + doc.setOpenAction(javascript); |
| 243 | + |
| 244 | + # save update document with new information |
| 245 | + doc.save(self.dataDir + "set_expiration.pdf"); |
| 246 | + |
| 247 | + print "Update document information, please check output file." |
| 248 | + |
| 249 | + |
| 250 | +class SetPdfFileInfo: |
| 251 | + |
| 252 | + def __init__(self, dataDir): |
| 253 | + self.dataDir = dataDir |
| 254 | + self.Document = jpype.JClass("com.aspose.pdf.Document") |
| 255 | + |
| 256 | + def main(self): |
| 257 | + |
| 258 | + doc= self.Document() |
| 259 | + pdf = self.Document() |
| 260 | + pdf=self.dataDir + 'input1.pdf' |
| 261 | + |
| 262 | + # Get document information |
| 263 | + doc_info = doc.getInfo(); |
| 264 | + |
| 265 | + doc_info.setAuthor("Aspose.Pdf for java"); |
| 266 | + doc_info.setCreationDate(datetime.today.strftime("%m/%d/%Y")); |
| 267 | + doc_info.setKeywords("Aspose.Pdf, DOM, API"); |
| 268 | + doc_info.setModDate(datetime.today.strftime("%m/%d/%Y")); |
| 269 | + doc_info.setSubject("PDF Information"); |
| 270 | + doc_info.setTitle("Setting PDF Document Information"); |
| 271 | + |
| 272 | + # save update document with new information |
| 273 | + doc.save(self.dataDir + "Updated_Information.pdf") |
| 274 | + |
| 275 | + print "Update document information, please check output file." |
| 276 | + |
| 277 | + |
| 278 | + |
| 279 | + |
| 280 | + |
0 commit comments