Export current opened drawing to pdf file.

Export current opened drawing to pdf file.

MarkJamesRogolino
Advocate Advocate
2,119 Views
6 Replies
Message 1 of 7

Export current opened drawing to pdf file.

MarkJamesRogolino
Advocate
Advocate

Hello, everyone. 

Now I am making a function to save current drawing file to pdf file.

my simple code is as followings.

[CommandMethod("svp")]
        public static void ExportPdf()
        {
            var documentManager = Application.DocumentManager;
            var currentDocument = documentManager.MdiActiveDocument;
            var db = currentDocument.Database;
            var ed = currentDocument.Editor;
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.DefaultExt = ".dwg"; // Required file extension 
            fileDialog.Filter = "Acad Files (*.dwg)|*.dwg|All Files (*.*)|*.*"; // Optional file extensions

            fileDialog.ShowDialog();
            string fname = fileDialog.FileName;
            currentDocument.CloseAndSave(fname);
            //using (Aspose.CAD.Image image = Aspose.CAD.Image.)
            //{
            //    // Create an instance of CadRasterizationOptions and set its various properties
            //    Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
            //    rasterizationOptions.PageWidth = 1600;
            //    rasterizationOptions.PageHeight = 1600;

            //    // Create an instance of PdfOptions
            //    Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();

            //    // Set the VectorRasterizationOptions property
            //    pdfOptions.VectorRasterizationOptions = rasterizationOptions;

            //    // Export CAD to PDF
            //    image.Save(fname, pdfOptions);
            //}

        }

 

I hope someone's help.  Thanks.

0 Likes
2,120 Views
6 Replies
Replies (6)
Message 2 of 7

Norman_Yuan
Mentor
Mentor

While your command is running, you cannot close the current document, because the command runs in document context.

 

Seeing the code you commented out below, I am not sure why you need to close the current document. If you have to, your command has to be session command (adding CommandFlags.Session to the CommandMethod attributes).

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 7

MarkJamesRogolino
Advocate
Advocate
Thanks for your reply.
How can I export without closing?
0 Likes
Message 4 of 7

ntclmain
Advocate
Advocate

@MarkJamesRogolino 
As Norman.yuan pointed out,  the Close and save command caused error

currentDocument.CloseAndSave(fname)

 Perhaps a "Save as" command may work?   

currentDatabase.SaveAs(strTempFileName, currentDatabase.OriginalFileVersion)

or you may interested in working with side database in case your current drawing is closed.
https://adndevblog.typepad.com/autocad/2012/04/batch-process-in-memory-1.html

0 Likes
Message 5 of 7

Norman_Yuan
Mentor
Mentor

@MarkJamesRogolino 

 

You may want to clarify your question: do you want to know why you get the error, or how to "export" current drawing as PDF?

 

For the former, I already answered. So, you need to explain why you have to close current drawing? If you do not have to close it, have you tried to go ahead with the other code without closing current drawing?

 

For the latter, since you are using third part component to do the export, sorry, I have no knowledge of it. However, it is a very common topic to use AutoCAD .NET API to generate PDF from drawing, and you can find a lot of sample code in this forum/online.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 7

MarkJamesRogolino
Advocate
Advocate

goldhorsemillion_0-1661274252601.png

Thanks for your help.

I save a dwg file wity database.saveas but when I open that pdf file, I can find this error message.

How can I resolve this?

0 Likes
Message 7 of 7

ntclmain
Advocate
Advocate

@MarkJamesRogolino 
It seems that we have misunderstood.

In my previous post, the "saveas" command just save the drawing with .dwg file format, not  .pdf format.

You used "saveas" command to save a drawing with a .pdf extension, just like edit the filename extension ---- You failed because that fake .pdf file is still in .dwg format.  

*
There is no built-in "save" method from .dwg to .pdf. 

In fact, you must do a plot (print) to a PDF plotter, such as "DWG To PDF.pc3", just like your printing action to PDF,  but the difference is: now you print by code.
I found a old topic which may help
https://forums.autodesk.com/t5/net/export-pdf-from-vb-net/td-p/3471934

0 Likes