a | b | |
---|
| 0 | + | import java.io.File; |
---|
| 0 | + | import java.io.IOException; |
---|
| 0 | + | import java.util.Iterator; |
---|
| 0 | + | |
---|
| 0 | + | import org.apache.commons.io.FileUtils; |
---|
| 0 | + | import org.apache.commons.lang3.time.StopWatch; |
---|
| 0 | + | |
---|
| 0 | + | import de.intarsys.pdf.cds.CDSRectangle; |
---|
| 0 | + | import de.intarsys.pdf.content.CSContent; |
---|
| 0 | + | import de.intarsys.pdf.content.TextState; |
---|
| 0 | + | import de.intarsys.pdf.content.common.CSCreator; |
---|
| 0 | + | import de.intarsys.pdf.cos.COSArray; |
---|
| 0 | + | import de.intarsys.pdf.cos.COSObject; |
---|
| 0 | + | import de.intarsys.pdf.font.PDFont; |
---|
| 0 | + | import de.intarsys.pdf.font.PDFontType1; |
---|
| 0 | + | import de.intarsys.pdf.font.outlet.FontFactoryException; |
---|
| 0 | + | import de.intarsys.pdf.parser.COSLoadException; |
---|
| 0 | + | import de.intarsys.pdf.pd.PDDocument; |
---|
| 0 | + | import de.intarsys.pdf.pd.PDExtGState; |
---|
| 0 | + | import de.intarsys.pdf.pd.PDForm; |
---|
| 0 | + | import de.intarsys.pdf.pd.PDPage; |
---|
| 0 | + | import de.intarsys.pdf.pd.PDPageNode; |
---|
| 0 | + | import de.intarsys.pdf.pd.PDPageTree; |
---|
| 0 | + | import de.intarsys.pdf.pd.PDXObject; |
---|
| 0 | + | import de.intarsys.pdf.st.EnumWriteMode; |
---|
| 0 | + | import de.intarsys.tools.locator.FileLocator; |
---|
| 0 | + | |
---|
| 0 | + | public class SelfWaterMarkingJpod { |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | /** |
---|
| 0 | + | * @param args |
---|
| 0 | + | * @throws IOException |
---|
| 0 | + | * @throws FontFactoryException |
---|
| 0 | + | * @throws COSLoadException |
---|
| 0 | + | */ |
---|
| 0 | + | public static void main(String[] args) throws IOException, |
---|
| 0 | + | FontFactoryException, COSLoadException { |
---|
| 0 | + | StopWatch runTestTime; |
---|
| 0 | + | int pages = 1000000; |
---|
| 0 | + | runTestTime = runTest(pages,100,1000,5000,5000, -1); |
---|
| 0 | + | System.out.println("Total time: " +runTestTime.toSplitString()+ " "+(pages*1.0d/runTestTime.getTime())*1000+" p/s"); |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | } |
---|
| 0 | + | |
---|
| 0 | + | private static StopWatch runTest(int i, int j, int k, int l, int m, int n) throws FontFactoryException, IOException, COSLoadException { |
---|
| 0 | + | StopWatch stopWatch = new StopWatch(); |
---|
| 0 | + | stopWatch.start(); |
---|
| 0 | + | new SelfWaterMarkingJpod().run(i,j,k,l,m,n); |
---|
| 0 | + | stopWatch.split(); |
---|
| 0 | + | stopWatch.stop(); |
---|
| 0 | + | return stopWatch; |
---|
| 0 | + | } |
---|
| 0 | + | |
---|
| 0 | + | private static final float halfSqrt2 = (float) (0.5 * Math.sqrt(2)); |
---|
| 0 | + | private int i; |
---|
| 0 | + | |
---|
| 0 | + | private static PDForm createForm() { |
---|
| 0 | + | PDForm form = (PDForm) PDForm.META.createNew(); |
---|
| 0 | + | PDFont font = PDFontType1.createNew(PDFontType1.FONT_Helvetica); |
---|
| 0 | + | PDExtGState extGState = (PDExtGState) PDExtGState.META.createNew(); |
---|
| 0 | + | extGState.setStrokingAlphaConstant(0.6f); |
---|
| 0 | + | extGState.setNonStrokingAlphaConstant(0.2f); |
---|
| 0 | + | |
---|
| 0 | + | CSCreator creator = CSCreator.createNew(form); |
---|
| 0 | + | creator.textBegin(); |
---|
| 0 | + | creator.setExtendedState(null, extGState); |
---|
| 0 | + | creator.setNonStrokeColorRGB(1.0f, 204f / 256f, 0); // orange |
---|
| 0 | + | creator.setStrokeColorRGB(1.0f, 204f / 256f, 0); |
---|
| 0 | + | creator.textSetFont(null, font, 80); |
---|
| 0 | + | creator.setLineWidth(2.0f); |
---|
| 0 | + | creator.textSetTransform(halfSqrt2, halfSqrt2, -halfSqrt2, halfSqrt2, |
---|
| 0 | + | 80, 0); |
---|
| 0 | + | creator.textSetRenderingMode(TextState.RENDERING_MODE_FILL_STROKE); |
---|
| 0 | + | creator.textShow("COPIA CONFORME"); |
---|
| 0 | + | creator.textEnd(); |
---|
| 0 | + | creator.close(); |
---|
| 0 | + | |
---|
| 0 | + | form.setBoundingBox(new CDSRectangle(0, 0, 600, 600)); |
---|
| 0 | + | |
---|
| 0 | + | return form; |
---|
| 0 | + | } |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | private void run(int iterations, int nodeEvery, int saveEvery, int sampleEvery, int resetEvery, int stopAt) throws FontFactoryException, IOException, COSLoadException { |
---|
| 0 | + | StopWatch stopWatch = new StopWatch(); |
---|
| 0 | + | stopWatch.start(); |
---|
| 0 | + | String path = "output.pdf"; |
---|
| 0 | + | File file = new File(path); |
---|
| 0 | + | if(file.exists()) { |
---|
| 0 | + | FileUtils.forceDelete(file); |
---|
| 0 | + | } |
---|
| 0 | + | FileUtils.copyFile(new File("94951244_13336301894205_.pdf"),file); |
---|
| 0 | + | FileLocator locator = new FileLocator(path); |
---|
| 0 | + | PDDocument fromDocument = PDDocument.createFromLocator(locator); |
---|
| 0 | + | try { |
---|
| 0 | + | |
---|
| 0 | + | PDPageTree pageTree = fromDocument.getPageTree(); |
---|
| 0 | + | fromDocument.setWriteModeHint(EnumWriteMode.INCREMENTAL); |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | PDForm form = createForm(); |
---|
| 0 | + | |
---|
| 0 | + | i = 0; |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | navigateCosTree(pageTree, form, fromDocument); |
---|
| 0 | + | |
---|
| 0 | + | } catch (Exception e) { |
---|
| 0 | + | e.printStackTrace(); |
---|
| 0 | + | FileUtils.forceDelete(file); |
---|
| 0 | + | } finally { |
---|
| 0 | + | fromDocument.close(); |
---|
| 0 | + | fromDocument = null; |
---|
| 0 | + | } |
---|
| 0 | + | } |
---|
| 0 | + | private PDPageNode navigateCosTree(PDPageNode node, PDForm form,PDDocument fromDocument) { |
---|
| 0 | + | |
---|
| 0 | + | COSArray cosKids = getKidsIterator(node); |
---|
| 0 | + | if(cosKids==null) { |
---|
| 0 | + | return null; |
---|
| 0 | + | } |
---|
| 0 | + | Iterator iterrator = cosKids.iterator(); |
---|
| 0 | + | PDPageNode origPageNode = (PDPageNode) PDPageNode.META |
---|
| 0 | + | .createFromCos((COSObject) iterrator.next()); |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | PDPage page = origPageNode.getFirstPage(); |
---|
| 0 | + | |
---|
| 0 | + | navigateCosTree(origPageNode,form,fromDocument); |
---|
| 0 | + | |
---|
| 0 | + | try { |
---|
| 0 | + | if(true){ |
---|
| 0 | + | waterMarkThePage(page, form); |
---|
| 0 | + | fromDocument.save(); |
---|
| 0 | + | } |
---|
| 0 | + | return null; |
---|
| 0 | + | } catch (IOException e) { |
---|
| 0 | + | // TODO Auto-generated catch block |
---|
| 0 | + | e.printStackTrace(); |
---|
| 0 | + | } |
---|
| 0 | + | i++; |
---|
| 0 | + | return node; |
---|
| 0 | + | } |
---|
| 0 | + | |
---|
| 0 | + | private COSArray getKidsIterator(PDPageNode pageNode) { |
---|
| 0 | + | return pageNode.cosGetField(PDPageTree.DK_Kids).asArray(); |
---|
| 0 | + | } |
---|
| 0 | + | |
---|
| 0 | + | private void waterMarkThePage(PDPageNode origPageNode, PDForm form) throws IOException { |
---|
| 0 | + | // TODO Auto-generated method stub |
---|
| 0 | + | PDPage page = origPageNode.getFirstPage(); |
---|
| 0 | + | CSContent content = CSContent.createNew(); |
---|
| 0 | + | CSCreator creator = CSCreator.createFromContent(content, page); |
---|
| 0 | + | |
---|
| 0 | + | CDSRectangle rect = page.getCropBox(); |
---|
| 0 | + | |
---|
| 0 | + | float formWidth = form.getBoundingBox().getWidth(); |
---|
| 0 | + | float scale = 1f; |
---|
| 0 | + | float offsetY = (rect.getHeight() - (formWidth * scale)) / 2; |
---|
| 0 | + | float offsetX = (rect.getWidth() - (formWidth * scale)) / 2; |
---|
| 0 | + | |
---|
| 0 | + | creator.saveState(); |
---|
| 0 | + | creator.transform(scale, 0, 0, scale, offsetX, offsetY); |
---|
| 0 | + | creator.doXObject(null, form); |
---|
| 0 | + | creator.restoreState(); |
---|
| 0 | + | creator.close(); |
---|
| 0 | + | |
---|
| 0 | + | page.cosAddContents(content.createStream()); |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | } |
---|
| 0 | + | |
---|
| 0 | + | } |
---|
... | |
---|