How to convert from PDF to XPS? - c#

I use the following method to print a XPS file with adobe acrobat:
private void GenerateXPS(String filename)
{
Process proc = new Process();
proc.StartInfo.FileName = "AcroRd32.exe";
proc.StartInfo.Arguments = "/t " + filename + ".pdf" + " " + "\"Microsoft XPS Document Writer\"";
proc.Start();
}
But the problem is, the Microsoft XPS Document writer needs a filename where to store the document. Now I'm asked to enter this filename by Adobe Acrobat, but I want to pass this filename as well in the arguments. Or if this is not possible just use the same filename. Is this possible?

If it isn't mandatory that you use acrobat, you might try printing the file directly to the Microsoft XPS Document writer. You can do this without the UI popping up. (see Feng Yuan's blog post for more details)

Related

Print to PDF with Microsoft Print to PDF printer

I try to print a ms project file to pdf without any additional action needed.
I have tried several solutions without success.
PrintDocument doc = new PrintDocument()
{
//DocumentName = safeDir + fileName,
PrinterSettings = new PrinterSettings()
{
// set the printer to 'Microsoft Print to PDF'
PrinterName = "Microsoft Print to PDF",
// tell the object this document will print to file
PrintToFile = true,
// set the filename to whatever you like (full path)
PrintFileName = safeDir + fileName,
}
};
doc.Print();
If i try this approach like showed here, I get an empty pdf file.
Manually printing to PDF works fine.
Any suggestions to solve this problem?
My Spidey Senses tells me this is most likely caused by commas in the file name
This is a known bug when printing to PDF. Use a different driver or
don't put a comma in the file name
Note : Its not only from code, its in general in certain situations.
Disclaimer : this is a complete and utter guess
Check these other links out
Bug in "Print to PDF" and "Print to XPS" in Windows 10? comma in filename results in zero-byte file
Microsoft Print to PDF not working
Found a bug in Microsoft Print to PDF
Microsoft Print to PDF in Windows 10

Unhandled exception in Adobe Reader while passing a search parameter in C#

I'm writing a WinForms application and there's a method to search text strings into an array of pdf files. When the given text is found, the method should open the proper file, highlighting said text.
I followed the specifics found in the Parameters for Opening PDF Files document, and this is the part of the code where I call the reader:
string arg = "/A search=\"" + parametri.testoDaRicercare + "\" \"" + file + "\"";
Process.Start("AcroRd32.exe", arg);
where parametri.testoDaRicercare is the string with the given text and file is the path to the pdf file.
Now, it works flawlessly with Adobe Reader XI, while I am given this error using Adobe Reader DC:
Unhandled exception in 0x61D6796F (AcroRd32.exe) in AcroRd32.exe:
0xC0000005: access violation while reading the path 0x000002C0. An Exception occurred.
By the way I don't know whether it's a simple Adobe+Win10 issue or whatever, because if I set the parameters to open the pdf at a certain page it works fine on both machines.
string arg = "/A \"page=" + pagina + "\" \"" + file + "\"";
Process.Start("AcroRd32.exe", arg);

OpenFileDialog to view pdf file c#

I just want to open a pdf file and not to use it. If the user wants to be able to print via a glider. I want to just pressing a button will open the file if Acrobat Reader to view
To open the PDF, try using the following code with the PDF's filename as the command.
string command=#"c:\Users\User\Desktop\hello.pdf";
var process = new System.Diagnostics.Process
{
StartInfo =
new System.Diagnostics.ProcessStartInfo(command)
};
process.Start();
Visual Basic CODE:
Dim FilePath As String = "<YourFilePath>" & "<YourFileName>" & ".pdf"
Dim Process As System.Diagnostics.Process = New System.Diagnostics.Process
Process.StartInfo.FileName = FilePath
Process.Start()
Every time the relative pdf file will show on a separate window
Obviously you MUST HAVE Adobe Reader INSTALLED on the CLIENT PC
It works pefectly. Used in Visual Studio 2010.

PDF to XPS Converting via Microsoft XPS Document Writer

Printing pdf document with Microsoft XPS Document Writer:
string filename = "C:\\1.pdf";
Process process = new Process();
process.StartInfo.Verb = "PrintTo";
process.StartInfo.FileName = #"C:\Program Files\Adobe\Reader 9.0\Reader\acrord32.exe";
process.StartInfo.Arguments =
"/t \"C:\\1.pdf\" \"Microsoft XPS Document Writer\" \"xps\" XPSPort:";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardOutput.ReadToEnd();
process.WaitForExit();
The only problem is Save Dialog, which requests file name (*.xps) where to save result. Everbody advices DOCINFO to solve this problem, but I didn't find any example of using.
I need programatically print PDF File via Microsoft XPS Document Writer with default output file name. How should I use DOCINFO in this situation?
Can you help me?
You can't reliably print by spawning Acrobat Reader unless you give it a desktop session and there will be a user there, because it sometimes pops up dialogs that need user attention.
Also it violates Adobe's licence if used unattended.
You can, however print using Ghostscript.
There is a C# interface to Ghostscript called Ghostscript.Net that I've used successfully on some very large projects. Both Ghostscript and Ghostcript.Net are free & open source.

Open a pdf file programmatically at a named destination

I would like to open a PDF file at named destination using WinForms (C#). Here is my code:
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "Acrobat.exe";
myProcess.StartInfo.Arguments = "/A \"nameddest=Test2=OpenActions\" C:\\example.pdf";
myProcess.Start();
It always opens the file at page 1 even having the destination Test2 at page # 10. It basically ignores the destination parameter. However if I use another parameter like the page number it works fine. For example:
myProcess.StartInfo.Arguments = "/A \"page=5=OpenActions\" C:\\example.pdf";
will always open the PDF document at page 5.
Thanks in advance for your help
I use the following code:
string strNamedDestination = "MyNamedDestination"; // Must be defined in PDF file.
string strFilePath = "MyFilePath.pdf";
string strParams = " /n /A \"pagemode=bookmarks&nameddest=" + strNamedDestination + "\" \"" + strFilePath + "\"";
Process.Start("AcroRd32.exe", strParams);
Note the "/n" inside the params. It makes Adobe to always open a new document. Otherwise, if the document was already opened, it doesn't move it to the right Named Destination. It depends on the behaviour you want for your application.
Regarding the Adobe documentation when opening a PDF document from a command shell, you can pass the parameters to the open command using the /A switch using the following syntax:
myProcess.StartInfo.Arguments = "/A \"nameddest=Test2=OpenActions\" C:\\example.pdf";
If I omit the OpenActions parameter everything works fine like:
myProcess.StartInfo.Arguments = "/A \"nameddest=Test2\" C:\\example.pdf";
I'm not sure why the OpenActions breaks opening the file but with omitting it works fine.
I have a csv with 5 columns.
Column1 contains PDF names and Column5 pagenumbers.
The executable displays the csv.
When I doubleclick on a line in the csv the following code is executed :
ListViewItem item = lvwItems.SelectedItems[0];
Process myProcess = new Process();
myProcess.StartInfo.FileName = "Acrobat.exe";
myProcess.StartInfo.Arguments = "/A page=" + item.SubItems[4].Text + " " + item.Text;
myProcess.Start();
This opens the selected PDF which name is in item.Text on the page which pagenumber is in item.SubItems[4].Text
Have you set up the destinations? You need to be have the standard or professional versions of Adobe Acrobat in order to do this:
http://kb2.adobe.com/cps/317/317300.html
Adobe Reader has a few bugs regarding opening to named destinations. Take a look at http://xenon.arcticus.com/open-pdf-named-destination-dde-c-c for some information and workarounds.

Categories