Solved

Damaged pdf when converting Excel workbook using PDFCreator

Posted on 2008-06-19
7
684 Views
Last Modified: 2012-05-05
I am trying to create *.pdf files from an Excel workbook using the following C# statement:

wb.PrintOut(1, wb.Sheets.Count, 1, false, "PDFCreator", true, false, FileName);

where wb is a Workbook using Microsoft.Office.Interop.Excel.

This results in a "damaged" pdf file - see attached file for error message.
What would be the solution?
Question.JPG
0
Comment
Question by:MTecho
  • 4
  • 3
7 Comments
 
LVL 45

Expert Comment

by:patrickab
ID: 21829413
MTecho,

If you use PDFCreator as a stand-alone application, does it create a valid working pdf file? Once you've established that then you can move on to other possibilities.

Patrick
0
 

Author Comment

by:MTecho
ID: 21837090
Thanks, Patrick.

Yes, it does.
0
 
LVL 45

Expert Comment

by:patrickab
ID: 21837112
MTecho,

In this statement:

wb.PrintOut(1, wb.Sheets.Count, 1, false, "PDFCreator", true, false, FileName);

how are you declaring and defining each element? Please give your actual code for

===
wb
Filename
===

Thanks

Patrick
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:MTecho
ID: 21837157
           string ReportID = "A1b";
            Microsoft.Office.Interop.Excel.Application xlApp = null;
...
                        xlApp = new Microsoft.Office.Interop.Excel.Application();
                        Workbook wb = xlApp.Workbooks.Open(Reports.Properties.Settings.Default.XLT_LOCATIONA1b, 3, false, 5, "", "", true, XlPlatform.xlWindows, null, null, false, null, false, false, XlCorruptLoad.xlNormalLoad);
                        Worksheet objsheet1 = (Worksheet)xlApp.Worksheets["All Arrivals"];
                        PivotTable Pv = (PivotTable)objsheet1.PivotTables("PivotTable1");
                        if (Pv.RefreshTable())
                        {
...
                                    wb.PrintOut(1, wb.Sheets.Count, 1, false, "PDFCreator", true, false, Reports.Properties.Settings.Default.WEB_FOLDER + (char)92 + ReportID + ".pdf");
...
                        }
==============================================
Reports.Properties.Settings.Default.XLT_LOCATIONA1b is a string setting, set by OpenFileDialog to the path for an Excel template file (*.xlt). The template file is loaded and the pivot table is refreshed
Reports.Properties.Settings.Default.WEB_FOLDER  is a string setting, set by OpenFolderDialog

Thanks

Mark
0
 
LVL 45

Accepted Solution

by:
patrickab earned 50 total points
ID: 21837454
Mark,

I do not pretend know anything about MS Interop, however it does look to be a very complicated way to do something that is really very simple. I'm not in the habit of producing pdf files from Excel so I had a look on the net and found Sonic PDF Creator 2.0. I installed it and then recorded an Excel macro to create a pdf file. All of that took a matter of minutes. The result was this short macro:

Sub print2pdf()
    Application.ActivePrinter = "Sonic PDF on Ne02:"
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
        "Sonic PDF on Ne02:", Collate:=True
End Sub

I obtained Sonic PDF Creator 2.0 from:

http://www.investintech.com/downloads/sonic/download.htm

I realise you are using C#, but perhaps it might be much easier to do what you want to do from within Excel and using VBA instead.

Patrick
0
 

Author Closing Comment

by:MTecho
ID: 31469055
Thanks for your help Patrick.
The solution I would really like would enable my application to process multiple Excel files and not require any macro in the spreadsheets.
I have found some VB code that does this, but generates an error when processing the second file - have not solved it yet.
0
 
LVL 45

Expert Comment

by:patrickab
ID: 21923541
MTecho - Thanks for the points. - Patrick
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question