大家好,
我在使用 Aspose.CAD for Java 尝试将 DWG 文件转换为 PDF 文件时遇到了问题。在我尝试转换以下附带的 DWG 文件时,我收到了一个错误消息。
我在这里提供了我的转换代码:
import java.io.File;
import java.io.InputStream;
import com.aspose.cad.imageoptions.CadRasterizationOptions;
import com.aspose.cad.imageoptions.PdfOptions;
import ext.plm.util.CtCommonUtil;
import wt.content.ApplicationData;
import wt.content.ContentHelper;
import wt.content.ContentHolder;
import wt.content.ContentRoleType;
import wt.content.ContentServerHelper;
import wt.content.FormatContentHolder;
import wt.epm.EPMDocument;
import wt.fc.PersistenceHelper;
import wt.pom.Transaction;
import wt.util.WTException;
public class CtCadLogic {
public static void convertDWGToPDF(ContentHolder contentHolder) throws Exception {
com.aspose.cad.Image image = null;
File saveFile = null;
try {
String cadFolderPath = CtCommonUtil.getWTHomePath() + File.separator + "codebase" + File.separator + "ext"
+ File.separator + "plm" + File.separator + "cad";
String savePath = cadFolderPath + File.separator + "temp" + File.separator;
String licensePath = cadFolderPath + File.separator + "config" + File.separator + "DownloadLicense.aspx";
File licenseFile = new File(licensePath);
com.aspose.cad.License lic = new com.aspose.cad.License();
lic.setLicense(licenseFile);
ApplicationData data = getPrimary(contentHolder);
String fileName = "" ;
if(contentHolder instanceof EPMDocument){
fileName = ((EPMDocument)contentHolder).getCADName();
}else{
if (data != null) {
fileName = data.getFileName();
}
}
if (data != null) {
InputStream is = ContentServerHelper.service.findContentStream(data);
image = com.aspose.cad.Image.load(is);
CadRasterizationOptions options = new CadRasterizationOptions();
options.setBackgroundColor(com.aspose.cad.Color.getWhite());
options.setPageWidth(image.getWidth());
options.setPageHeight(image.getHeight());
PdfOptions pdfOptions = new PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.setVectorRasterizationOptions(options);
// Export the DWG to PDF
savePath = savePath + fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf";
image.save(savePath, pdfOptions);
saveFile = new File(savePath);
System.out.println(saveFile.getAbsolutePath());
uploadAttachment(saveFile, saveFile.getName(), contentHolder);
}
} catch (Exception e) {
e.printStackTrace();
// throw new WTException(e.getMessage());
} finally {
if (image != null) {
image.close();
}
if (saveFile != null && saveFile.exists()) {
saveFile.delete();
}
}
}
public static ApplicationData getPrimary(ContentHolder contentHolder) throws Exception {
ApplicationData data = null;
try {
contentHolder = ContentHelper.service.getContents(contentHolder);
data = (ApplicationData) ContentHelper.getPrimary((FormatContentHolder) contentHolder);
} catch (Exception e) {
e.printStackTrace();
throw new WTException(e.getMessage());
}
return data;
}
public static void uploadAttachment(File file, String fileName, ContentHolder contentHolder) throws Exception {
Transaction tran = null;
try {
tran = new Transaction();
tran.start();
contentHolder = ContentHelper.service.getContents(contentHolder);
ApplicationData attachmentData = ApplicationData.newApplicationData(contentHolder);
attachmentData.setRole(ContentRoleType.SECONDARY);
attachmentData = ContentServerHelper.service.updateContent(contentHolder, attachmentData, file.toString());
attachmentData.setFileName(fileName);
PersistenceHelper.manager.save(attachmentData);
tran.commit();
tran = null;
} catch (Exception e) {
tran.rollback();
e.printStackTrace();
throw new Exception(e.getMessage());
}
}
}
此外,我也附带了我尝试转换的 DWG 文件。你可以在下方找到它。
我已经尝试了一些可能的解决方案,但是没有成功。我也在 Aspose 的文档和论坛中寻找过相关的信息,但是没有找到有用的信息。
请问有人能帮我解决这个问题吗?我对此表示感谢。
附件:
- DWG 文件:309003-01156.zip (2.6 MB)