Hi,
we use apsose.pdf Java on one file pdf, i want to replace some labels on pdf.
when we call the library it is very slow, the processing of the replacement of about 120 labels takes almost a minute, is it possible to improve the performance?
Thanks
Would you kindly share your sample PDF along with sample code snippet with us? We will test the scenario in our environment and address it accordingly.
Hi, of course.
Here my code:
public GeneratorResp modifyPdf(GeneratorReq generatorReq) throws IOException {
com.aspose.pdf.LocaleOptions.setLocale(new Locale( "en", "US"));
// find the fil in file system and verify that exist
Path path = Paths.get(pdfDir);
File input;
try {
input = findByFileName(path, generatorReq.getFileName());
if(!input.exists())
throw new IOException("File non trovato.");
} catch (IOException e) {
log.error(e.getLocalizedMessage());
throw e;
}
Document pdfDocument = new Document(input.getAbsolutePath());
// Create TextAbsorber object to find all instances of the input search phrase
for (LabelModify l : generatorReq.getListLabelModify()){
try {
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(l.getLabel());
// Accept the absorber for first page of document
pdfDocument.getPages().accept(textFragmentAbsorber);
// Get the extracted text fragments into collection
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
// Loop through the fragments
for (TextFragment textFragment : (Iterable<TextFragment>) textFragmentCollection) {
// Update text and other properties
textFragment.setText(l.getValue());
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
GeneratorResp generatorResp = new GeneratorResp();
String fileChange = String.format("%s.pdf", UUID.randomUUID());
pdfDocument.save(pdfDirModify + fileChange );
generatorResp.setUuid(fileChange);
generatorResp.setFileName(input.getName());
log.info(String.format("Created File %s%s ", pdfDirModify, fileChange));
return generatorResp;
}
This is the json labels and value:
{ "listLabelModify" : [{"label":"TTV","value":"Value"},{"label":"TTUS","value":"Buffer Shocks"},{"label":"THUG","value":"buffer force"},{"label":"THUC","value":"transverse buffer force"},{"label":"TTTC","value":"Crab"},{"label":"TTT","value":"SUM"},{"label":"TTST","value":"Static Test"},{"label":"TTSS","value":"Stress history"},{"label":"TTSP","value":"Load spectrum"},{"label":"TTPP","value":"Crane self weight"},{"label":"TTP","value":"Item"},{"label":"TTMH","value":"Weight of the hoist load"},{"label":"TTL","value":"Crane"},{"label":"TTIR","value":"wheel index ij"},{"label":"TTIN","value":"Track irregularities"},{"label":"TTHR","value":"Load release"},{"label":"TTH","value":"Hoist"},{"label":"TTDT","value":"Dynamic Test"},{"label":"TTDS","value":"Displacements"},{"label":"TTDC","value":"Dynamic Coefficients"},{"label":"TTCY","value":"Cycles"},{"label":"THCV","value":"vertical loads"},{"label":"THCS","value":"horizontal loads (skew)"},{"label":"TTCS","value":"Hoist class"},{"label":"TTCL","value":"Classification"},{"label":"THCF","value":"horizontal loads (acc/braking)"},{"label":"TTC","value":"Hoist Control class"},{"label":"TTAS","value":"Hoist Control Acceleration"},{"label":"TTAH","value":"Horizontal Drive Acceleration"},{"label":"TT100","value":"% of released load"},{"label":"Label","value":"Valore"},{"label":"LU","value":"U5"},{"label":"LU","value":"U5"},{"label":"LSS","value":"S3"},{"label":"LSS","value":"S3"},{"label":"LS","value":"23,39"},{"label":"LS","value":"23,39"},{"label":"LQR24","value":"18,04"},{"label":"LQR24","value":"18,04"},{"label":"LQR23","value":"0,00"},{"label":"LQR23","value":"0,00"},{"label":"LQR22","value":"0,00"},{"label":"LQR22","value":"0,00"},{"label":"LQR21","value":"17,63"},{"label":"LQR21","value":"17,63"},{"label":"LQR14","value":"69,68"},{"label":"LQR14","value":"69,68"},{"label":"LQR13","value":"0,00"},{"label":"LQR13","value":"0,00"},{"label":"LQR12","value":"0,00"},{"label":"LQR12","value":"0,00"},{"label":"LQR11","value":"68,11"},{"label":"LQR11","value":"68,11"},{"label":"LQR","value":"173,46"},{"label":"LQR","value":"173,46"},{"label":"LQH24","value":"1,93"},{"label":"LQH24","value":"1,93"},{"label":"LQH23","value":"0,00"},{"label":"LQH23","value":"0,00"},{"label":"LQH22","value":"0,00"},{"label":"LQH22","value":"0,00"},{"label":"LQH21","value":"2,14"},{"label":"LQH21","value":"2,14"},{"label":"LQH14","value":"47,12"},{"label":"LQH14","value":"47,12"},{"label":"LQH13","value":"0,00"},{"label":"LQH13","value":"0,00"},{"label":"LQH12","value":"0,00"},{"label":"LQH12","value":"0,00"},{"label":"LQH11","value":"46,91"},{"label":"LQH11","value":"46,91"},{"label":"LQH","value":"98,10"},{"label":"LQH","value":"98,10"},{"label":"LQC24","value":"16,11"},{"label":"LQC24","value":"16,11"},{"label":"LQC23","value":"0,00"},{"label":"LQC23","value":"0,00"},{"label":"LQC22","value":"0,00"},{"label":"LQC22","value":"0,00"},{"label":"LQC21","value":"15,49"},{"label":"LQC21","value":"15,49"},{"label":"LQC14","value":"22,56"},{"label":"LQC14","value":"22,56"},{"label":"LQC13","value":"0,00"},{"label":"LQC13","value":"0,00"},{"label":"LQC12","value":"0,00"},{"label":"LQC12","value":"0,00"},{"label":"LQC11","value":"21,20"},{"label":"LQC11","value":"21,20"},{"label":"LQC","value":"75,36"},{"label":"LQC","value":"75,36"},{"label":"LQ","value":"Q4"},{"label":"LQ","value":"Q4"},{"label":"LPHI7","value":"1,25"},{"label":"LPHI7","value":"1,25"},{"label":"LPHI6S","value":"1,00"},{"label":"LPHI6S","value":"1,00"},{"label":"LPHI6D","value":"1,06"},{"label":"LPHI6D","value":"1,06"},{"label":"LPHI5H","value":"1,15"},{"label":"LPHI5H","value":"1,15"},{"label":"LPHI5","value":"1,40"},{"label":"LPHI5","value":"1,40"},{"label":"LPHI4","value":"1,09"},{"label":"LPHI4","value":"1,09"},{"label":"LPHI3","value":"1,00"},{"label":"LPHI3","value":"1,00"},{"label":"LPHI2","value":"1,11"},{"label":"LPHI2","value":"1,11"},{"label":"LPHI1","value":"1,10"},{"label":"LPHI1","value":"1,10"},{"label":"LHT3","value":"1,34"},{"label":"LHT3","value":"1,34"},{"label":"LHT24","value":"6,71"},{"label":"LHT24","value":"6,71"},{"label":"LHT23","value":"0,00"},{"label":"LHT23","value":"0,00"},{"label":"LHT22","value":"0,00"},{"label":"LHT22","value":"0,00"},{"label":"LHT21","value":"6,71"},{"label":"LHT21","value":"6,71"},{"label":"LHT14","value":"1,74"},{"label":"LHT14","value":"1,74"},{"label":"LHT13","value":"0,00"},{"label":"LHT13","value":"0,00"},{"label":"LHT12","value":"0,00"},{"label":"LHT12","value":"0,00"},{"label":"LHT11","value":"1,74"},{"label":"LHT11","value":"1,74"},{"label":"LHST24","value":"0,00"},{"label":"LHST24","value":"0,00"},{"label":"LHST23","value":"0,00"},{"label":"LHST23","value":"0,00"},{"label":"LHST22","value":"0,00"},{"label":"LHST22","value":"0,00"},{"label":"LHST21","value":"4,81"},{"label":"LHST21","value":"4,81"},{"label":"LHST14","value":"0,00"},{"label":"LHST14","value":"0,00"},{"label":"LHST13","value":"0,00"},{"label":"LHST13","value":"0,00"},{"label":"LHST12","value":"0,00"},{"label":"LHST12","value":"0,00"},{"label":"LHST11","value":"18,58"},{"label":"LHST11","value":"18,58"},{"label":"LHSL24","value":"0,00"},{"label":"LHSL24","value":"0,00"},{"label":"LHSL23","value":"0,00"},{"label":"LHSL23","value":"0,00"},{"label":"LHSL22","value":"0,00"},{"label":"LHSL22","value":"0,00"},{"label":"LHSL21","value":"0,00"},{"label":"LHSL21","value":"0,00"},{"label":"LHSL14","value":"0,00"},{"label":"LHSL14","value":"0,00"},{"label":"LHSL13","value":"0,00"},{"label":"LHSL13","value":"0,00"},{"label":"LHSL12","value":"0,00"},{"label":"LHSL12","value":"0,00"},{"label":"LHSL11","value":"0,00"},{"label":"LHSL11","value":"0,00"},{"label":"LHL2","value":"1,94"},{"label":"LHL2","value":"1,94"},{"label":"LHL1","value":"1,94"},{"label":"LHL1","value":"1,94"},{"label":"LHD","value":"HD4"},{"label":"LHD","value":"HD4"},{"label":"LHC","value":"HC2"},{"label":"LHC","value":"HC2"},{"label":"LHB2","value":"11,15"},{"label":"LHB2","value":"11,15"},{"label":"LHB12","value":"9,35"},{"label":"LHB12","value":"9,35"},{"label":"LHB11","value":"9,35"},{"label":"LHB11","value":"9,35"},{"label":"LDT","value":"D5"},{"label":"LDT","value":"D5"},{"label":"LDL","value":"D6"},{"label":"LDL","value":"D6"},{"label":"LDH","value":"D3"},{"label":"LDH","value":"D3"},{"label":"LBETA","value":"0,00"},{"label":"LBETA","value":"0,00"},{"label":"DXT2","value":"270"},{"label":"DXT1","value":"270"},{"label":"DXS","value":"20000"},{"label":"DXR","value":"250"},{"label":"DXP","value":"2700"},{"label":"DXM","value":"0"},{"label":"DXL","value":"3240"},{"label":"DXHSU2","value":"915"},{"label":"DXHSU1","value":"425"},{"label":"DXHSL","value":"470"},{"label":"DXHS","value":"870"},{"label":"DXHRB","value":"0"},{"label":"DXHR","value":"10"},{"label":"DXHP","value":"0"},{"label":"DXHL","value":"10000"},{"label":"DXHH","value":"0"},{"label":"DXHDU2","value":"1325"},{"label":"DXHDU1","value":"835"},{"label":"DXHD","value":"2000"},{"label":"DXHC","value":"445"},{"label":"DXHB","value":"245"},{"label":"DXDP","value":"0"},{"label":"DXDB","value":"0"},{"label":"DXD","value":"137"},{"label":"DXAOS","value":"1225"},{"label":"DXAOD","value":"583"},{"label":"DXAOA","value":"535"},{"label":"DXAO","value":"833"},{"label":"DXALS","value":"535"},{"label":"DXALD","value":"1423"},{"label":"DXALA","value":"975"},{"label":"DXAL","value":"1273"}],"fileName":"pasquale.pdf" }
pasquale.pdf (1.3 MB)
Instead of using TextFragmentAbsorber for all pages at once, you can use it at page level in order to reduce the resources consumption:
for(Page page: document.getPages()) {
TextFragmentAbsorber absorber = new TextFragmentAbsorber();
page.accept(absorber);
for(TextFragment textFragment: absorber.getTextFragments()) {
// further stuff
}
}
Please try using this suggestion with 22.8 version of the API and let us know if it impact the performance positively. If you still notice any performance issue, please share your environment details as well like OS Name and Version, Installed RAM size, JDK Version, etc.
Hello,
thanks a lot for your suggest, so I use, macOs Monterey ver:12.5.1 intel m1, java 11, 16GB RAM and i upgrada asposo from 21.5 at 22.8
but when I run the project and I call the json from postman, there are those error:
2022-09-07 12:44:18.798 ERROR 22692 — [nio-8088-exec-1] c.o.d.service.ServiceFilePdf : Collection is of a fixed size
com.aspose.pdf.internal.ms.System.l7k: Collection is of a fixed size
at com.aspose.pdf.internal.ms.System.ly$lb.removeAt(Unknown Source) ~[aspose-pdf-22.8.jar:22.8]
at com.aspose.pdf.ADocument.lI(Unknown Source) ~[aspose-pdf-22.8.jar:22.8]
at com.aspose.pdf.CharInfoCollection.copyTo(Unknown Source) ~[aspose-pdf-22.8.jar:22.8]
at com.aspose.pdf.l19y.lI(Unknown Source) ~[aspose-pdf-22.8.jar:22.8]
at com.aspose.pdf.l19y.lf(Unknown Source) ~[aspose-pdf-22.8.jar:22.8]
at com.aspose.pdf.TextFragment.setText(Unknown Source) ~[aspose-pdf-22.8.jar:22.8]
at com.omis.dimensioneddrawingsgenerator.service.ServiceFilePdf.modifyPdf(ServiceFilePdf.java:93) ~[classes/:na]
at com.omis.dimensioneddrawingsgenerator.controller.ControllerFilePdf.modifyPdfMethod(ControllerFilePdf.java:23) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.19.jar:5.3.19]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.19.jar:5.3.19]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.19.jar:5.3.19]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.19.jar:5.3.19]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.19.jar:5.3.19]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.19.jar:5.3.19]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.19.jar:5.3.19]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.19.jar:5.3.19]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.19.jar:5.3.19]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.19.jar:5.3.19]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:681) ~[tomcat-embed-core-9.0.62.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.19.jar:5.3.19]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.62.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.62.jar:9.0.62]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.19.jar:5.3.19]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.19.jar:5.3.19]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.19.jar:5.3.19]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.19.jar:5.3.19]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.19.jar:5.3.19]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.19.jar:5.3.19]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
at java.base/java.lang.Thread.run(Thread.java:829) ~[na:na]
Have you faced this error during first iteration or there is specific text value for which API threw it? If you can specify further, it would be helpful for us to replicate the issue and address it accordingly.
Hi, from first iteration i’ve a problem. In local with postman i call the API and in the body I pass the json.
We tested the scenario in our environment in a console application but we were unable to notice any issues. Would you kindly also try to use the same code snippet in a sample console application and see if issue still persists and share the same application with us in .zip format? We will be addressing it accordingly after replicating it.
ok, but in your test you can notice the problem of the performance?
Yes, we have noticed the delay in the execution. We need to investigate this further in detail. For this sake, an investigation ticket as PDFJAVA-41993 is logged in our issue tracking system. We will further analyze it and let you know as soon as it is resolved. Please be patient and spare us some time.
We are sorry for the inconvenience.
ok,
thanks a lot
Hi,
there are any news for me?
Thanks a lot
We are afraid that there are no updates at the moment. Please note that the ticket has recently been logged in free support model and will be investigated on a first come first serve basis. We will surely inform you once we have some updates about its resolution. Please spare us some time.
We are sorry for the inconvenience.
Hi you can help me, to align the label in the center? Hi try differents solutions but not set the textfragment in the center.
Thanks a lot
Do you want to align text in center after replacing it? OR do you want to change the text position for existing one? Please share a sample PDF with some details of your requirements. We will look into this matter and share our feedback with you accordingly.
You can use TextFragment.Position Property in order to change its position however, it will not be a dynamic approach to set the alignment to center. For the sake of further investigation, an investigation ticket as PDFJAVA-41993 has been logged in our issue tracking system. We will further look into its details and let you know as soon as it is resolved. Please be patient and spare us some time.
We are sorry for the inconvenience.
you can suggest any solution?
thanks a lot
We are afraid that we cannot suggest any workaround without investigating the ticket and structure of the PDF document that is being used. We will surely inform you as soon as we make some definite progress towards ticket resolution. Please spare us some time.
We are sorry for the inconvenience.
Hi,
there are any news for me?
thank you