DXF to PDF scaling issue and save(outputStream, pdfOptions); method takes too much time

Hii
I have pdf scaling
tinyPdf.pdf (2.4 MB)

SampleExpected Pdf.pdf (6.1 MB)

issue with some cad files like when i try to convert from Dxf to Pdf

  1. I am getting tiny pdf images i have attached 2 pdfs one is tiny and other with expected.
  2. And for some cad files objImage.save(outputStream, pdfOptions); method takes too much time that gives this error :
    18:41:55,584 WARN [com.arjuna.ats.arjuna] (Transaction Reaper) ARJUNA012117: TransactionReaper::check timeout for TX 0:ffffc0a8c865:-49bad931:66990ae5:132 in state RUN

18:41:55,585 WARN [com.arjuna.ats.arjuna] (Transaction Reaper Worker 0) ARJUNA012095: Abort of action id 0:ffffc0a8c865:-49bad931:66990ae5:132 invoked while multiple threads active within it.

18:41:55,586 WARN [com.arjuna.ats.arjuna] (Transaction Reaper Worker 0) ARJUNA012381: Action id 0:ffffc0a8c865:-49bad931:66990ae5:132 completed with multiple threads - thread default task-9 was in progress with sun.misc.Unsafe.park(Native Method)

java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)

java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)

java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:997)

java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1304)

java.util.concurrent.CountDownLatch.await(CountDownLatch.java:231)

com.aspose.cad.system.Threading.WaitHandle.waitAny(Unknown Source)

com.aspose.cad.system.Threading.WaitHandle.waitAny(Unknown Source)

com.aspose.cad.internal.b.d.a(Unknown Source)

com.aspose.cad.Image.a(Unknown Source)

com.aspose.cad.Image.save_internalized(Unknown Source)

com.aspose.cad.w.a(Unknown Source)

com.aspose.cad.internal.eK.b.a(Unknown Source)

com.aspose.cad.internal.eK.c.a(Unknown Source)

com.aspose.cad.Image.save(Unknown Source)

my code:
// Load the source CAD file
Image objImage = Image.load(filePath);

    // Create an instance of PdfOptions
    PdfOptions pdfOptions = new PdfOptions();

    // Create rasterization options and configure scaling
    CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
    rasterizationOptions.setBackgroundColor(Color.getWhite()); // Set background color if needed
    rasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor); // Ensure object colors are used
    
    // Set the page size (A0) size in points)
    rasterizationOptions.setPageWidth(3370); // A0 width in points
    rasterizationOptions.setPageHeight(2384); // A0 height in points

    // Ensure content fits within the page size
    rasterizationOptions.setAutomaticLayoutsScaling(true);
    rasterizationOptions.setNoScaling(false);
    
    pdfOptions.setVectorRasterizationOptions(rasterizationOptions);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    // Export CAD to PDF
    objImage.save(outputStream, pdfOptions);

byte[] pdfBytes = outputStream.toByteArray();

    try (PDDocument document = PDDocument.load(pdfBytes)) {
        // Get the first page to set the view
        PDPageTree pages = document.getPages();
        PDPage page = pages.get(0);

        // Set the destination to center of the page
        PDPageXYZDestination dest = new PDPageXYZDestination();
        dest.setPage(page);

        // Calculate the center coordinates
        float pageWidth = page.getMediaBox().getWidth();
        float pageHeight = page.getMediaBox().getHeight();
        int centerX = (int) (pageWidth / 2.0f);
        int centerY = (int) (pageHeight / 2.0f);

        System.out.println("CenterX: " + centerX + ", CenterY: " + centerY);

        dest.setLeft(centerX);
        dest.setTop(centerY);
        dest.setZoom(1.0f); // Adjust the zoom level if necessary

        // Set the open action
        PDDocumentCatalog catalog = document.getDocumentCatalog();
        catalog.setOpenAction(dest);

        // Save the modified PDF
        ByteArrayOutputStream modifiedOutputStream = new ByteArrayOutputStream();
        document.save(modifiedOutputStream);

I have attached Dxf files for tiny pdf issue and save method
DXF.zip (999.2 KB)

@johnhedo,
Hi.
Thank you for the details, let us check these issues on our side and come back.

@johnhedo,
Hi.

  1. For this case such result relates to the specific of the drawing. Here is the screenshot after ZOOM EXTENTS operation - tinyZoom.png (27.3 KB) - and 2 groups of entities are marked with red colors. So the entire drawing is scaled accordingly.

  2. The second drawing has two problematic areas.

I can see an empty space around the drawing in PDF after export. Sometimes this happens when the size of the drawing is incorrect in the original DXF/DWG file. For instance in this case the size is defined by EXTMIN = 1598.31,-9867.65,0.00 and EXTMAX = 2047.66,-9480.82,0.00 points and it creates additional empty spaces.

It is possible to apply

cadImage.updateSize();

before export to fix this.

This drawing requires more time to export because it contains hatches of complex structure and because their origin point is far away (relatively) from the areas being hatched. For such cases we need to do more calculations to recover positions of dashes in hatches. Possible solutions may include:

  • exploding the hatches in drawing before export (probably, the best choice);
  • moving origin point closer to borders being hatched;
  • applying low quality with (but this will destroy all hatches):
RasterizationQuality quality = new RasterizationQuality();
quality.setHatch(RasterizationQualityValue.Low);
rasterizationOptions.setQuality(quality);

hii

yes i understood issue for tinypdf,
and thanks for your solution for hatches i will work into it with your solution and update here.

@oleksii.gorokhovatskyi

thanks above code worked . i have one more drawing with same pdf issue i tried with this updated code can you please help me with it this is the error:

12:58:06,057 ERROR [stderr] (default task-9) at com.aspose.cad.internal.fB.e.(Unknown Source)

12:58:06,057 ERROR [stderr] (default task-9) at com.aspose.cad.internal.eT.m.a(Unknown Source)

12:58:06,057 ERROR [stderr] (default task-9) at com.aspose.cad.internal.eT.m.a(Unknown Source)

12:58:06,057 ERROR [stderr] (default task-9) at com.aspose.cad.internal.eT.m.a(Unknown Source)

12:58:06,057 ERROR [stderr] (default task-9) at com.aspose.cad.internal.eS.g.a(Unknown Source)

12:58:06,057 ERROR [stderr] (default task-9) at com.aspose.cad.internal.eS.g.a(Unknown Source)

12:58:06,057 ERROR [stderr] (default task-9) at com.aspose.cad.internal.fA.a.a(Unknown Source)

12:58:06,057 ERROR [stderr] (default task-9) … 9 more

12:58:06,087 ERROR [stderr] (default task-9) class com.aspose.cad.cadexceptions.ImageSaveException: Image export failed. Error during PDF export: index —> java.lang.RuntimeException: Error during PDF export: index

I have attached DXF file here Dropbox

@johnhedo,
I can not reproduce this error with Aspose.CAD for Java 24.3.
Here is the full example that works successfully, it requires about 6 minutes to complete and the time may be reduced with decreased quality settings:

final CadImage cadImage = (CadImage)Image.load(inputFile);

CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
rasterizationOptions.setBackgroundColor(Color.getWhite()); // Set background color if needed
rasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor); // Ensure object colors are used

//RasterizationQuality quality = new RasterizationQuality();
//quality.setHatch(RasterizationQualityValue.Low);
//quality.setObjectsPrecision(RasterizationQualityValue.Low);
//rasterizationOptions.setQuality(quality);

// Set the page size (A0) size in points)
rasterizationOptions.setPageWidth(3370); // A0 width in points
rasterizationOptions.setPageHeight(2384); // A0 height in points

// Ensure content fits within the page size
rasterizationOptions.setAutomaticLayoutsScaling(true);
rasterizationOptions.setNoScaling(false);

PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(rasterizationOptions);
cadImage.save(outputPdfFilePath, pdfOptions);

@oleksii.gorokhovatskyi

ok… I am using java 8 i will check for 6 min

@johnhedo,
if this example will not work for you, please, create some small demo project, so we can run it and see the issue.

@oleksii.gorokhovatskyi
yes It didn’t work for Dropbox this drawing

@johnhedo,
so is this the same file as previous? What is the result of running export for this file? Could you create small demo-project that reproducing the error?

@oleksii.gorokhovatskyi
yes creating demo project.

@oleksii.gorokhovatskyi
Hii
I have attached demo project that produces error for this drawing Dropbox
demo.zip (26.4 KB)

http://localhost:8080/api/convert/dxf-to-pdf?file

@johnhedo,
thank you, we have created CADJAVA-11660 issue ticket to investigate this case better.

@johnhedo,
please note, that you use 20.10 version of Aspose.CAD which is very old. Could you please switch to the latest 24.3 and try?

@oleksii.gorokhovatskyi
I tried Upgrade the version to 24.3 it is working with java 17 but not with java 1.8

@johnhedo,
but it seems that Spring Boot requires at least Java 17? Please try on your side if some demo application works with Aspose.CAD for Java 24.3 with Java 1.8.

1 Like

ok … I am Using Spring boot version 2. that supports java 1.8

@johnhedo,
could you please test if this example works for you with 1.8? If not - what is the error? What is your OS?
sample.zip (11.7 KB)