DWG conversion to PNG


#1

Hello, i have a problem converting DWG file to PNG
This the code i use:

Aspose.Imaging.ImageOptions.PngOptions OptionsI = new Aspose.Imaging.ImageOptions.PngOptions();
Aspose.Imaging.ResolutionSetting oResI = new Aspose.Imaging.ResolutionSetting(Convert.ToDouble(m_iResolution), Convert.ToDouble(m_iResolution));
OptionsI.ResolutionSettings = oResI;
((Aspose.Imaging.Image)m_oDocument).Save(_oOutputStream, OptionsI);


And this is exeption i have when trying saving:

Image export failed.
{“Cannot export non-raster images.”}


what can i do?
I attach DWG sample file i used.

Thank’s a lot


#2

I’m getting the same error saving a DFX to JPG -


using (var img = Aspose.Imaging.Image.Load("fanuc-430-arm.dfx"))
    img.Save("fanuc-430-arm.jpg", new Aspose.Imaging.ImageOptions.JpegOptions()
    {
        Quality = 80,
        ResolutionSettings = new Aspose.Imaging.ResolutionSetting(150, 150)
    });

Aspose version 2.5

#3

I’ve found if you go via PDF it works -

using (var img = Aspose.Imaging.Image.Load("fanuc-430-arm.dfx"))
using (var ms = new MemoryStream())
{
	img.Save(ms, new Aspose.Imaging.ImageOptions.PdfOptions()
		{
			PageWidth = img.Width,
			PageHeight = img.Height,
			DrawType = CadDrawTypeMode.UseDrawColor,
			ScaleMethod = ScaleType.GrowToFit,
			ResolutionSettings = new Aspose.Imaging.ResolutionSetting(75, 75)
		});
	var pdf = new Aspose.Pdf.Document(ms);
	using (var imageStream = new FileStream("fanuc-430-arm.jpg", FileMode.Create))
		new Aspose.Pdf.Devices.JpegDevice(150, 150, new Aspose.Pdf.Devices.Resolution(75), 80)
			.Process(pdf.Pages[1], imageStream);

}

#4

Hi all,


The exception message “Cannot export non-raster images.” thrown by the CAD module is correct because the current implementation of Aspose.Imaging APIs do not support the conversion of CAD file formats to raster images. We have now logged a feature request under the ticket IMAGING-34275 to bring this feature (Conversion of CAD formats to Raster Images) on our road map of Aspose.Imaging APIs. Please spare us little time to properly analyze the feasibility of the request, and to estimate the time required for the implementation. In the meanwhile, you can use the workaround suggested by Jonathan in this thread. Please note, Aspose.Pdf provides separate classes for different raster image formats under the Aspose.Pdf.Devices namespace.

Please feel free to write back in case you find any difficulty or have any questions.

#5

I’m trying to save attached DWG as PDF with the workaround suggested by Jonathan, but i obtain a completely white pdf: what’s wrong?

Here’s the code

Aspose.Imaging.ImageOptions.PdfOptions OptionsIP = new Aspose.Imaging.ImageOptions.PdfOptions();
OptionsIP.PageWidth = ((Aspose.Imaging.Image)m_oDocument).Width;
OptionsIP.PageHeight = ((Aspose.Imaging.Image)m_oDocument).Height;
OptionsIP.DrawType = Aspose.Imaging.FileFormats.Cad.CadDrawTypeMode.UseDrawColor;
OptionsIP.ScaleMethod = Aspose.Imaging.FileFormats.Cad.ScaleType.GrowToFit;
((Aspose.Imaging.Image)m_oDocument).Save(_oOutputStream, OptionsIP);


#6

Hi,


Thank you for writing back.

We have evaluated the code segment using the latest version of Aspose.Imaging for .NET 2.5.0. The problem seems to be related to the PDF canvas size, that is’ when you set the PDF dimensions as of loaded image dimensions, the resultant canvas is too large (24325 X 24325). However, Aspose.Imaging API should be able to fit the drawing on the canvas and render it properly. Unfortunately, the resultant PDF seems to be blank therefore we have logged an investigative ticket IMAGING-34281 in our bug tracking system to look further into this matter.

In the meanwhile, if you wish to check the conversion, please set the PdfOptions.PageWidth & PageHeight properties to a smaller values, such as 500. This would produce the correct results.

#7

Does not work even if i set 500 to heigth and width in pagesize: are you able to create a non-blank pdf?


#8

Hi,


Yes, I am able to create a non-blank PDF using the below code against the latest version of Aspose.Imaging for .NET 2.5.0. Please check the resultant PDF as attached.

C#

using (var img = Aspose.Imaging.Image.Load(myDir + “Office.dwg”))
{
var options = new Aspose.Imaging.ImageOptions.PdfOptions();
options.PageHeight = 500;
options.PageWidth = 500;
img.Save(myDir + “output.pdf”, options);
}


#9

OK so two more problems:

1 i can have a non blank pdf commenting this line
OptionsIP.ScaleMethod = Aspose.Imaging.FileFormats.Cad.ScaleType.GrowToFit;
the growtofit goes into page dimension issue itself ?

2 pdf i (and you) obtain is not complete!
if i open dwg file with a dwg reader i can see more items in it (see attached image)


#10

Hi,


First of all, please accept my sincere apology for a bit delayed response.

Please find the answers to your inquiries in the same sequence as follow.

  1. We have logged a new ticket (IMAGING-34285) for the problem of blank PDF generation when large DWG is rendered to smaller canvas with ScaleType.GrowToFit. We will investigate the matter separately, however, we have linked this issue to the previously logged ticket IMAGING-34281 for the problem related to the large page dimensions.
  2. Textual contents in the DWG drawings cannot be rendered to PDF with current implementation the API, therefore we have logged it as a feature request (IMAGING-34286) in our database, and requested the core team to analyze its feasibility. In case you meant anything else that has been missed while rendering the DWG drawing to PDF format, we would request you to please highlight those areas in a snapshot, and share here for further investigation.

Please feel free to write back in case you have any ambiguity or concerns.

#11

Thanks of the update Babar Raza!


#12

Hi all,


This is to update to regarding the ticket logged earlier as IMAGING-34286 against the feature request of rendering textual contents of DWG drawings to PDF format. The requested feature is already available through the CadImage.Layouts. By default, the rendering engine renders only the default layout (Model) which is without the textual contents.

Please review the following code snippet as well as the resultant PDF file.

C#

using (var image = (Aspose.Imaging.FileFormats.Cad.CadImage)Image.Load(myDir + “office.dwg”))
{
// Create options
PdfOptions pdfOptions = new PdfOptions();

// Set output file size
pdfOptions.PageWidth = 1500;
pdfOptions.PageHeight = 1200;

// Get layouts of the CadImage
string[] layouts = new string[image.Layouts.Keys.Count];
image.Layouts.Keys.CopyTo(layouts, 0);

// Set Layout name to render
pdfOptions.LayoutName = layouts[1];//“Layout1”;
pdfOptions.ScaleMethod = Aspose.Imaging.FileFormats.Cad.ScaleType.GrowToFit;

// Export
image.Save(myDir + “output.pdf”, pdfOptions);
}

Please feel free to write back in case you need our further assistance.

#13

So, using this code:

string[] layouts = new string[((Aspose.Imaging.FileFormats.Cad.CadImage)((Aspose.Imaging.Image)m_oDocument)).Layouts.Keys.Count];
((Aspose.Imaging.FileFormats.Cad.CadImage)((Aspose.Imaging.Image)m_oDocument)).Layouts.Keys.CopyTo(layouts, 0);
OptionsIP.LayoutName = layouts[1]; //“Layout1”;


will insert text layout… but there could be some other layouts? layout2, 3, etc?

that said (and page size issue solved), same option could be implemented in imagesaveoption for converting DWG/DXF to png?

moreover, i have other problem related to page size - see atached files - when i use above code for adding layouts, resultant pdf “shift” bottom in the pdf page: how can i avoid this problem?
in addition to code for layouts i use this code:

Aspose.Imaging.ImageOptions.PdfOptions OptionsIP = new Aspose.Imaging.ImageOptions.PdfOptions();
OptionsIP.PageWidth = 595; //a4
OptionsIP.PageHeight = 595; //a4


to avoid error of wrong pagesize (IMAGING-34285)
so… why you can create the same “mostly centered” image in pdf and we cannot?
thank’s



#14

Hi,


With current implementation of Aspose.Imaging for .NET API, you can select single layout in one save operation. If you have a requirement to render all the layouts of a DWG drawing, you have to iterate over the layouts, and select one layout at a time. I will discuss this matter with the core development team if we can enhance this feature with the future releases of Aspose.Imaging APIs.

We have already logged a ticket for the requested feature of exporting CAD files to raster image formats including PNG format. Unfortunately, the ticket (IMAGING-34275) is in analysis phase therefore we may not be able to comment on how we are going to provide the feature unless we have thoroughly checked the feasibility of the requested feature. As soon as receive any updates, we will post here for your kind reference.

As far as the alignment of the drawing is concerned, you may have notice that we are rendering the drawing (from office.dwg) on a canvas that is horizontal larger as compared to the vertical dimension. Reason being, the drawing is in landscape so when we render it to a PDF page that has larger width than height, the drawing seems to be rendered in the center. PdfOptions.CenterDrawing property can center the drawing on any canvas, but it does not seem to take effect with your provided drawings. We are evaluating the aforesaid property with different drawings, and we will log appropriate investigative ticket(s) in case the problem persists.

#15

Ok thank you
In the meanwhile i was tinking to solve the issue creating a new pdf document and resizing it, but i’m not able to do it: can you provide me some code for this operation?

This is what i was trying:
save dwg file as pdf …
((Aspose.Imaging.Image)m_oDocument).Save(_oOutputStream, OptionsIP);
open new pdf document
Aspose.Pdf.Document oPdfTest = new Aspose.Pdf.Document(_oOutputStream);

the new document has pagesize 842 * 595 points and mediabox/cropbox of 595 * 595 points

i’d like to resize the pagesize to 595 * 595, obviously keeping the entyre mediabox without cutting it, obtaining a pdf with page size same as mediabox/cropbox

is it possible?


#16

demo-1:

This is what i was trying:
save dwg file as pdf …
((Aspose.Imaging.Image)m_oDocument).Save(_oOutputStream, OptionsIP);
open new pdf document
Aspose.Pdf.Document oPdfTest = new Aspose.Pdf.Document(_oOutputStream);

the new document has pagesize 842 * 595 points and mediabox/cropbox of 595 * 595 points

i’d like to resize the pagesize to 595 * 595, obviously keeping the entyre mediabox without cutting it, obtaining a pdf with page size same as mediabox/cropbox
Hi,

In order to re-size the PDF pages, please follow the instructions specified over Update Page Dimensions. In the event of any further query, please feel free to contact.

#17

This doesn’t work
I tryed to resize page to (595,595) but the page remain 595, 842

moreover i don’t need actually to resize page but to crop page as the same dimension of cropbox - i can’t know if image in pdf is at the bottom or top of pdf page so i’m trying to crop the page accordingly: remember that my second operation is to save a png from pdf and i have to avoid extra white space in pdf and in images


#18

demo-1:
This doesn’t work
I tryed to resize page to (595,595) but the page remain 595, 842

Hi,

I have tested the scenario using latest release of Aspose.Pdf for .NET 9.4.0 where I have used one of my sample PDF files and as per my observations, the page dimensions are properly being updated/set. Can you please share your source PDF file so that we can test the scenario at our end. We are sorry for this inconvenience.

demo-1:
moreover i don’t need actually to resize page but to crop page as the same dimension of cropbox - i can’t know if image in pdf is at the bottom or top of pdf page so i’m trying to crop the page accordingly: remember that my second operation is to save a png from pdf and i have to avoid extra white space in pdf and in images
Concerning to above stated requirement, you may consider following the instructions specified over Trim White-space around page. Once the white space is removed, the you may consider converting PDF pages to Image format.

#19

my original test file is the same dwg you have (i attach final generated pdf file with extra white space)
as you can see in commented code, i tryied some different ways but final pdf contains extra white space in every case… can you give me right code for my scenario?

using (MemoryStream oMTempStream = new MemoryStream())
{
((Aspose.Imaging.Image)m_oDocument).Save(oMTempStream, OptionsIP);

Aspose.Pdf.Document oPdfTest = new Aspose.Pdf.Document(new MemoryStream(oMTempStream.ToArray()));


//Aspose.Pdf.Facades.PdfFileEditor oPdfFE = new Aspose.Pdf.Facades.PdfFileEditor();
//Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeParameters parameters = new Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeParameters(
// Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeValue.Units( Convert.ToDouble(oPdfTest.Pages[1].CropBox.Width - oPdfTest.PageInfo.Width)), //left
// Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeValue.Units( Convert.ToDouble(oPdfTest.Pages[1].CropBox.Width)), //w
// Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeValue.Units(0), //right
// Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeValue.Units( Convert.ToDouble(oPdfTest.Pages[1].CropBox.Height - oPdfTest.PageInfo.Height)), //top
// Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeValue.Units( Convert.ToDouble(oPdfTest.Pages[1].CropBox.Height)), //h
// Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeValue.Units(0)); //bottom

//oPdfFE.ResizeContents(oPdfTest, new int[]{1}, parameters);

oPdfTest.PageInfo.Width = oPdfTest.Pages[1].CropBox.Width;
oPdfTest.PageInfo.Height = oPdfTest.Pages[1].CropBox.Height;
oPdfTest.Pages[1].MediaBox = new Aspose.Pdf.Rectangle(0, 0, oPdfTest.Pages[1].CropBox.Width, oPdfTest.Pages[1].CropBox.Height);
oPdfTest.Pages[1].CropBox = new Aspose.Pdf.Rectangle(0, 0, oPdfTest.Pages[1].CropBox.Width, oPdfTest.Pages[1].CropBox.Height);
//new Aspose.Pdf.Rectangle(0, 0, oPdfTest.Pages[0].CropBox.Width, oPdfTest.Pages[0].CropBox.Height);
Aspose.Pdf.PageCollection opg = oPdfTest.Pages;
Aspose.Pdf.Page pdfPage = opg[1];
pdfPage.SetPageSize(595, 595);

oPdfTest.Save(_oOutputStream);
}


#20

Hi,


I have tested the scenario using Aspose.Pdf for .NET 9.4.0 where I have tried trimming white space around exportPDFaspose2.pdf (file which you have shared) using the instructions specified over Trim White-space around page and as per my observations, the final PDF is properly being generated. For your reference, I have also attached the output generated over my end. Can you please try using the code snippet shared over this link and in case the problem still persists, please share some further details regarding your working environment i.e. Operating System version, .NET Framework version etc.