Open PDF in command

Open PDF in command

Anonymous
Not applicable
5,171 Views
15 Replies
Message 1 of 16

Open PDF in command

Anonymous
Not applicable

Hi, I'm trying to figure out a way to open a pdf using the command line. AutoCAD has code for how to open a DWG but this does not work for a pdf. Below is AutoCAD's "Open an existing drawing command". How do you modify this for PDF.

 

using System.IO;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Runtime;

 

[CommandMethod("OpenDrawing", CommandFlags.Session)]

public static void OpenDrawing()

{

string strFileName = "C:\\campus.dwg";

 

DocumentCollection acDocMgr = Application.DocumentManager;

if (File.Exists(strFileName))

     acDocMgr.Open(strFileName, false);

else

     acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " + strFileName + " does not exist.");

}

 

0 Likes
Accepted solutions (1)
5,172 Views
15 Replies
Replies (15)
Message 2 of 16

norman.yuan
Mentor
Mentor

If you want to open PDF document/file in the similar way as AutoCAD API does, you need to use an application, which provides similar API method and can be automated from AutoCAD (since you want to open it from AutoCAD). So, is there such an application? Probably. However, in your case, it can be done in a lot easier way: simply use System.Diagnostics.Process.Start() with either pfd file name or a ProcessStartInfo argument supplied to start a default PDF application in the computer, as long as the computer has a PDF application installed, most likely, Adobe PDF Reader.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 16

Anonymous
Not applicable

Hi thanks for your response. I'm not sure what you mean. I know there is a command -PDFIMPORT but there is a dialog box which prevents SendACommandToAutoCAD from working. This code can also be found on website but PDFIMPORT command isn't working for me in this setting.

 

P.S. I am using Visual Studio 2013 and AutoCAD 2017

0 Likes
Message 4 of 16

norman.yuan
Mentor
Mentor

Well, what you asked is "a way to open a pdf...".

 

Now you are saying it is "PDFIMPORT" command, which is importing data into AutoCAD as AutoCAD entity, as your previous post asked. 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 16

Anonymous
Not applicable

I meant opening it as a drawing. Otherwise, like you said Adobe works great for just opening a PDF. Do you know if its possible to do what I want?

0 Likes
Message 6 of 16

Anonymous
Not applicable

Or do you suggest any other way to do it?

0 Likes
Message 7 of 16

norman.yuan
Mentor
Mentor

Regarding to your previous post, want to run command "PDFIMPORT", but have difficult to bypass the open file dialog box (which is NOT OPENING PDF itself, it only service the purpose to get a valid file name). If your current question is still on that topic, here is what you can try in code: set system variable "FILEDIA" to 0 prior to send command "PDFIMPORT"

 

Application.SetSystemVariable("FILEDIA", 0);

command="-PDFIMPORT f '" + userFile + "' 1 ......"

 

You may want to set "FILEDIA" back after the command completed (in CommandEnded event handler, maybe?).

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 8 of 16

Anonymous
Not applicable

Hi, Thank you again. This got me passed the dialog, but the weird thing is the command took the whole string at once and did not take the rest of the parameters for the whole string. Basically, the initial command was ._-PDFIMPORT f userfile .... then it asked for me to enter the file manually.

 

Also what is the default value for FILEDIA?

0 Likes
Message 9 of 16

Anonymous
Not applicable

Anyone know how to solve this issue?

 

I got the default value for FILEDIA I put it in wrong place in program so it wasn't working as expected but that is fixed.

0 Likes
Message 10 of 16

norman.yuan
Mentor
Mentor
Accepted solution

Well, after figuring out what system variable "FILEDIA" means, it is just a matter of try-error-try needed to figure out how to feed all the inputs required at command line.

 

Since you are using Acad2017 (Acad2015 or later), executing Acad command with Editor.Command() method would be much easier than Document.SendStringToExecute() in this case (a command with many user inputs). 

 

Here is the Editor.Command() version of code, which works with my Acad2018:

        [CommandMethod("PdfIn")]
        public static void ImportPdf()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;
            try
{
//Save original FILEDIA value and then set it to 0 int fileDia = Convert.ToInt32(CadApp.GetSystemVariable("FILEDIA")); CadApp.SetSystemVariable("FILEDIA", 0); ed.Command("-PDFIMPORT", "f", "D:/Temp/TestPdf.pdf", 1, "10.0,10.0", 1.0, 0.0); } catch(System.Exception ex) { CadApp.ShowAlertDialog(ex.Message); } finally {
//Restore changed FILEDIA value back to original CadApp.SetSystemVariable("FILEDIA", fileDia); } }

However, Editor.Command() can only be called in Document Context. If you want to do it in Application context (such as doing it from button click of a modeless window, you cannot call Editor.Command(), so, a Document.SendStringToExecution(0 version would still needed as below, which also works OK with my Acad2018:

        [CommandMethod("PdfIn", CommandFlags.Session)]
        public static void ImportPdf()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;
            //Save origial FILEDIA value
            int fileDia = Convert.ToInt32(CadApp.GetSystemVariable("FILEDIA"));
//Set FILEDIA value to 0 dwg.SendStringToExecute("(setvar \"FILEDIA\" 0)\r", true, false, false);
//Start command -PDFIMPORT with pdf file name supplied var cmd = "-PDFIMPORT f \"D:\\Temp\\TestPdf.pdf\"\r"; dwg.SendStringToExecute(cmd, true, false, false);
// supply page input dwg.SendStringToExecute("1\r", true, false, false);
// supply insertion location input dwg.SendStringToExecute("10.0,10.0\r", true, false, false);
// supply scale input dwg.SendStringToExecute("1.0\r", true, false, false);
// supply rotation input dwg.SendStringToExecute("30.0\r", true, false, false);
// restore FILEDIA back to original dwg.SendStringToExecute($"(setvar \"FILEDIA\" {fileDia})\r", true, false, false); }

Notice that I added CommandFlags.Session to make this command being executed in Application context, thus I must use Document.SendStringToExecution(), rather than Editor.Command().

 

By the way, this forum is meant for AutoCAD VBA/COM API, while your post is about AutoCAD .NET API, which should be posted in .NET forum.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 11 of 16

Anonymous
Not applicable

CadApp.SetSystemVariable("FILEDIA", fileDia);

 

For this line, I'm getting an error "The name 'fileDia' does not exist in the current context even though it is declared and spelled the same in the try{}.

 

Also, I tried just putting the dialog to 1 instead of fileDia and ran the command. I tried the Editor.Command() version and it did not import the PDF I entered. I changed the filepath appropriately. What do you mean by

 

I did not know there were different forums, sorry.

0 Likes
Message 12 of 16

norman.yuan
Mentor
Mentor

Ah, this line:

 

int fileDia = Convert.ToInt32(CadApp.GetSystemVariable("FILEDIA"));

 

should be move before

 

try

{

 

}

catch{}

finally{}

 

Attached video clip below shows how the code being executed line by line in debugging mode and the result.

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 13 of 16

Anonymous
Not applicable

When I run this code, then do Netload as usual, the PDFIN command doesn't show up in AutoCAD for some reason.

Capture1.PNG

0 Likes
Message 14 of 16

Anonymous
Not applicable

How does the second way work for you? For me this line gives me 14 errors:

dwg.SendStringToExecute($"(setvar \"FILEDIA\" {fileDia})\r", true, false, false);

I found away around the "command not showing up" in my previous reply.

 

I want to try the second way. I'm assuming its a really small thing that's giving so many errors. Is there a reference or another library I need to include?

Capture.PNGCapture1.PNG

0 Likes
Message 15 of 16

norman.yuan
Mentor
Mentor

Code like

var stringValue = $"xxxxx{varName}xxxx";

is C# 6.0 feature (.NET 4.6 or later)

 

Obviously, you are targeting .NET4.5. SO, simply change that line to

 

dwg.SendStringToExecute("(setvar \"FILEDIA\" " + fileDia.ToString() + ")\r", true, false, false)

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 16 of 16

Anonymous
Not applicable

Hi yall,

 

This was probably solved. Saw that @norman.yuan  had some good insight on tackling this.

 

I'm attaching a Code I developed along with a custom FileDialog Class in order to select the pdf of interest via a file dialog.

I'll have to credit @Ed__Jobe for posting the custom FileDialog Class workaround here: VBA Custom FileDialog Class Workaround .

 

My code lets you select the multipage pdf, input the first # of sheets to pdf import ,starting from sheet1, into the active modelspace, input the scale, and wallah! There's a "ReadMe.txt" file for instructions.

The imports will be neatly organized systemically with a nominal degree of seperation (see my screenshot).

 

*What if you have multiple single sheet pdfs?

  • Assuming they are all single sheets, for the Input prompt "first # of sheets" will have to be set to 1 since you're just importing the first sheet on this pdf
  • Update the following FileDialog object In my main "ACAD_PDF" code at this line and set to True: objFile.MultiSelect = True 'Select one file (False) vs multi files (True)
  • The following variable will absorb each file name in one long single string, separated by blanks: strFileName 
  • You will have to parse the string. I'd recommend allocating the strings into a collection or array that you can then loop through around the "Loop Through PDF" section of the "ACAD_PDF" code.

Enjoy!

OJ3D

 

 

0 Likes