a | b | |
---|
| 0 | + | import java.io.IOException; |
---|
| 0 | + | import java.util.Iterator; |
---|
| 0 | + | import java.util.List; |
---|
| 0 | + | |
---|
| 0 | + | import org.apache.commons.lang3.time.StopWatch; |
---|
| 0 | + | import org.joda.time.DateTime; |
---|
| 0 | + | import org.joda.time.Instant; |
---|
| 0 | + | import org.joda.time.Interval; |
---|
| 0 | + | |
---|
| 0 | + | import de.intarsys.pdf.cds.CDSRectangle; |
---|
| 0 | + | import de.intarsys.pdf.content.CSContent; |
---|
| 0 | + | import de.intarsys.pdf.content.IContentStreamProvider; |
---|
| 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.COSBasedObject; |
---|
| 0 | + | import de.intarsys.pdf.cos.COSCatalog; |
---|
| 0 | + | import de.intarsys.pdf.cos.COSDictionary; |
---|
| 0 | + | import de.intarsys.pdf.cos.COSObject; |
---|
| 0 | + | import de.intarsys.pdf.cos.COSStream; |
---|
| 0 | + | import de.intarsys.pdf.encoding.WinAnsiEncoding; |
---|
| 0 | + | import de.intarsys.pdf.font.PDFont; |
---|
| 0 | + | import de.intarsys.pdf.font.PDFontType1; |
---|
| 0 | + | import de.intarsys.pdf.font.PDFontType3; |
---|
| 0 | + | import de.intarsys.pdf.font.outlet.FontFactoryException; |
---|
| 0 | + | import de.intarsys.pdf.font.outlet.FontOutlet; |
---|
| 0 | + | import de.intarsys.pdf.font.outlet.FontQuery; |
---|
| 0 | + | import de.intarsys.pdf.font.outlet.IFontFactory; |
---|
| 0 | + | import de.intarsys.pdf.parser.COSLoadException; |
---|
| 0 | + | import de.intarsys.pdf.pd.PDCollection; |
---|
| 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.st.EnumWriteMode; |
---|
| 0 | + | import de.intarsys.tools.locator.FileLocator; |
---|
| 0 | + | import de.intarsys.tools.locator.ILocator; |
---|
| 0 | + | |
---|
| 0 | + | public class WaterMarkingJpod { |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | private PDFontType1 font; |
---|
| 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); |
---|
| 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) throws FontFactoryException, IOException, COSLoadException { |
---|
| 0 | + | StopWatch stopWatch = new StopWatch(); |
---|
| 0 | + | stopWatch.start(); |
---|
| 0 | + | new WaterMarkingJpod().run(i,j,k,l,m); |
---|
| 0 | + | stopWatch.split(); |
---|
| 0 | + | stopWatch.stop(); |
---|
| 0 | + | return stopWatch; |
---|
| 0 | + | } |
---|
| 0 | + | |
---|
| 0 | + | private static final float halfSqrt2 = (float) (0.5 * Math.sqrt(2)); |
---|
| 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, 100); |
---|
| 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"); //$NON-NLS-1$ |
---|
| 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) throws FontFactoryException, IOException, COSLoadException { |
---|
| 0 | + | StopWatch stopWatch = new StopWatch(); |
---|
| 0 | + | stopWatch.start(); |
---|
| 0 | + | |
---|
| 0 | + | PDDocument document = PDDocument.createNew(); |
---|
| 0 | + | try { |
---|
| 0 | + | |
---|
| 0 | + | document.setAuthor(" S.p.A."); |
---|
| 0 | + | document.setCreator("2pdf"); |
---|
| 0 | + | ILocator locator = new FileLocator("output_test.pdf"); |
---|
| 0 | + | |
---|
| 0 | + | PDDocument fromDocument = PDDocument.createFromLocator(new FileLocator("input_test.pdf")); |
---|
| 0 | + | |
---|
| 0 | + | //PDPageNode node= fontFromDocument.getPageTree().getFirstNode(); |
---|
| 0 | + | PDPageTree pageTree = fromDocument.getPageTree(); |
---|
| 0 | + | COSArray cosKids = pageTree.cosGetField(PDPageTree.DK_Kids).asArray(); |
---|
| 0 | + | |
---|
| 0 | + | Iterator iterrator = cosKids.iterator(); |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | PDPageTree newNode = null; |
---|
| 0 | + | PDFont font = getFont(document); |
---|
| 0 | + | |
---|
| 0 | + | PDForm form = createForm(); |
---|
| 0 | + | |
---|
| 0 | + | for (int i = 0; i <= iterations; i++) { |
---|
| 0 | + | |
---|
| 0 | + | PDPageNode origPageNode = (PDPageNode) PDPageNode.META |
---|
| 0 | + | .createFromCos((COSObject) iterrator.next()); |
---|
| 0 | + | |
---|
| 0 | + | document.setWriteModeHint(EnumWriteMode.INCREMENTAL); |
---|
| 0 | + | |
---|
| 0 | + | if(i % nodeEvery == 0) { |
---|
| 0 | + | newNode = (PDPageTree) PDPageTree.META.createNew(); |
---|
| 0 | + | document.addPageNode(newNode); |
---|
| 0 | + | |
---|
| 0 | + | } |
---|
| 0 | + | |
---|
| 0 | + | COSObject origPage = origPageNode.cosGetObject(); |
---|
| 0 | + | PDPageNode pageNode = (PDPageNode) PDPageTree.META.createFromCos(origPage); |
---|
| 0 | + | // inlocator |
---|
| 0 | + | |
---|
| 0 | + | COSDictionary cosGetDict = pageNode.cosGetDict(); |
---|
| 0 | + | |
---|
| 0 | + | cosGetDict = (COSDictionary) cosGetDict.copyDeep(); |
---|
| 0 | + | |
---|
| 0 | + | PDPage page = (PDPage) PDPage.META.createFromCos(cosGetDict); |
---|
| 0 | + | newNode.addNode(page); |
---|
| 0 | + | |
---|
| 0 | + | CSCreator creator = CSCreator.createNew(page); |
---|
| 0 | + | creator.close(); |
---|
| 0 | + | |
---|
| 0 | + | if(i % sampleEvery == 0) { |
---|
| 0 | + | stopWatch.split(); |
---|
| 0 | + | stopWatch.stop(); |
---|
| 0 | + | long partTime = stopWatch.getTime(); |
---|
| 0 | + | String a = (sampleEvery*1.0d/partTime)*1000+" p/s "; |
---|
| 0 | + | System.err.println(stopWatch.toSplitString()+" "+a); |
---|
| 0 | + | System.err.println(i/sampleEvery); |
---|
| 0 | + | stopWatch.reset(); |
---|
| 0 | + | stopWatch.start(); |
---|
| 0 | + | } |
---|
| 0 | + | System.err.println(i); |
---|
| 0 | + | |
---|
| 0 | + | if(i % saveEvery == 0) { |
---|
| 0 | + | document.save(locator); |
---|
| 0 | + | } |
---|
| 0 | + | if(i % resetEvery == 0) { |
---|
| 0 | + | //System.out.println(document.cosGetDoc().stGetDoc().getXRefSection().getSize()); |
---|
| 0 | + | |
---|
| 0 | + | //System.gc(); |
---|
| 0 | + | //document.close(); |
---|
| 0 | + | //document = PDDocument.createFromLocator(new FileLocator("test.pdf")); |
---|
| 0 | + | } |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | } |
---|
| 0 | + | |
---|
| 0 | + | } catch (Exception e) { |
---|
| 0 | + | e.printStackTrace(); |
---|
| 0 | + | } finally { |
---|
| 0 | + | document.close(); |
---|
| 0 | + | document = null; |
---|
| 0 | + | } |
---|
| 0 | + | } |
---|
| 0 | + | |
---|
| 0 | + | private void writeText(PDFont font, CSCreator creator,int kkk) { |
---|
| 0 | + | creator.textSetFont(null, font, 20f); |
---|
| 0 | + | creator.textLineMoveTo(1+(10*kkk), 1); |
---|
| 0 | + | creator.textShow("Hello, world."); |
---|
| 0 | + | } |
---|
| 0 | + | |
---|
| 0 | + | private PDFont getFont(PDDocument document) throws FontFactoryException { |
---|
| 0 | + | //IFontFactory factory = FontOutlet.get().lookupFontFactory(document); |
---|
| 0 | + | //FontQuery fontquery = new FontQuery("Arial", ""); |
---|
| 0 | + | //PDFont font = factory.getFont(fontquery); |
---|
| 0 | + | if(font==null) |
---|
| 0 | + | font = PDFontType1.createNew(PDFontType1.FONT_Helvetica); |
---|
| 0 | + | return font; |
---|
| 0 | + | } |
---|
| 0 | + | |
---|
| 0 | + | } |
---|
... | |
---|