PDF-XChange Drivers API (only) V4/V5
This Forum is for the use of Software Developers requiring help and assistance for Tracker Software's PDF-XChange Printer Drivers SDK (only) - VERSION 4 & 5 - Please use the PDF-Tools SDK Forum for Library DLL assistance.
I want to print my excel file to PDF with customized paper size in Excel Macro using VBA. Here's my code which can print the file but the paper size seems not changed. Can anyone help me? Any reply will be appreciated. Thanks a lot.
Public PDFPFactory As New PXCComLib.CPXCControlEx
Public PDFPrinter As PXCComLib.CPXCPrinter
Public Sub PDFPrintInit()
'Initialize printer
Set PDFPrinter = PDFPFactory.Printer("", "PDF-XChange 4.0 Sample", "<YOUR REG CODE>", "<YOUR DEV CODE>")
pname = PDFPrinter.Name
PDFPrinter.SetAsDefaultPrinter
'Config printer
PDFPrinter.ResetDefaults
With PDFPrinter
'Set paper size
.Option("Paper.SheetSizeIndex") = -2
.Option("Paper.SheetWidth") = 1200
.Option("paper.SheetHeight") = 300
.Option("Save.Path") = ""
.Option("Save.File") = "MyTest.pdf"
.Option("Save.SaveType") = "Save"
.Option("Save.ShowSaveDialog") = "No"
.Option("Save.WhenExists") = "Overwrite"
.Option("Compression.Text") = "Yes"
.Option("Compression.ASCII") = "No"
.Option("Compression.Color.Enabled") = "Yes"
.Option("Compression.Color.Method") = "Auto"
.Option("Compression.Indexed.Enabled") = "Yes"
.Option("Compression.Indexed.Method") = "Auto"
.Option("Compression.Mono.Enabled") = "Yes"
.Option("Compression.Mono.Method") = "Auto"
.Option("Save.RunApp") = "Yes"
.Option("Saver.ShowProgress") = "No"
End With
End Sub
Public Sub PDFClose()
PDFPrinter.RestoreDefaultPrinter
Set PDFPrinter = Nothing
Set PDFFactory = Nothing
End Sub
Public Sub PDFPrint()
' Select used cells
Worksheets("Sheet1").Activate
PDFPrintInit
ActiveSheet.UsedRange.Select
Selection.PrintOut
PDFClose
End Sub
How to set the paper size in excel? If I print the file in excel manually, choose the printer "PDF-XChange 4.0", then in the properties window, set the page size to custom, the file will be printed to PDF in the excat customized size. How can I implement this in my code?
This is really important to my project. Any help will be appreciated.
I tried to set PageSetup.PaperSize = xlPaperUser behind the code which sets the values of Paper.SheetWidth and Paper.SheetHeight of PDFPrinter. But it still didn't work out. I got this error message:
"unable to set the papersize property of the pagesetup class" on the line "PageSetup.PaperSize = xlPaperUser".
I am wondering that in SDK all the properties of Paper section actually are corresponding to the properties of layout section on the paper setting page when config the printer manually.
When setting the printer properties manually, I tried 2 different ways:
Test 1. If I just change the Sheet Size to "custom", and set Size to, e.g., 1200 X 300, while keep the Paper Size as "Standard: A4", then the printout will still be at size of A4.
Test 2. On the contrary, if I change the Paper Size to "Custom: 1200 X 300 mm", and leave the Sheet Size as "Auto", then I will get a printout at the size of 1200 X 300.
So I'm thinking that in my code, if I want to print a file in a customized size, it might not be enough only to set Paper.SheetSizeIndex = -2, Paper.SheetWidth = 1200 and Paper.SheetHeight = 300, becuase it looks like what I did in Test 1.
Therefore, are there any other properties, like Paper.PaperWidth or Paper.PaperHeight, for us to do it? If not, how can I implement what I did in Test 2 programmatically?
Sorry for so many questions and thank you for your help.
Thanks. I tried the code and it didn't work, The sheetsize of the printout did change to what I set in code, 1200 X 300. But the paper size is still A4. Here are my code and the printout pdf.
By the way, I had to comment this line since otherwise it would throw out errors like "Run-time error '1004': Unable to set the PaperSize property of the PageSetup class".
fbao wrote:Thanks. I tried the code and it didn't work, The sheetsize of the printout did change to what I set in code, 1200 X 300. But the paper size is still A4. Here are my code and the printout pdf.
As I see by attached PDF's your code works. What's wrong now ?
Tracker Software (Project Director)
When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
Now my problem is that I can't print the excel as large as the sheet size. In the printout pdf, you can see the sheet size is pretty large, while the excel sheet is so small. My goal is to print excel files into pdf which is wide enough so that all the columns/fields can be put in one page. However, in my printout pdf, the excel sheet is still vertically devided into several pages.
For example, there is a spread sheet which has 20 columns and 200 rows. To print it by default, an A4 paper may only contain 10 columns and 100 rows, so the printout pdf has 4 pages totally. If my user wants to look at the contents of column 10 and 11 to find out the relation between them, he has to find them seperately on page 1 and 3 and make sure he's looking at the same row.
What I need to do is to customize the size of the print paper so that one page can contain 20 columns, and maybe also 200 rows which doesn't matter though, so my user can view all the fields of a row on the same page.
I found that if I print the spread sheet manually like this:
1. In excel 2007, click print, choose "PDF-XChange 4.0", click "properties".
2. In the properties window, in "Paper Setting" ->"Paper size" section, choose "Standard A4".
3. In "Paper Setting"->"Layout" section, set "Sheet Size" to "Custom", then "Size" to "1200 X 300".
Then the printout is exactly what I attached previously.
If I chage step 2 above to the following:
2. In the properties window, in "Paper Setting" ->"Paper size" section, choose "Custom" and set "1200 X 300".
Then do step 3 the same as above, the printout is indeed what I want.
Now in my code, it seems that I can only do step 3 which is to set the sheet size to custom, but can't do step 2 which is to set paper size to custom. How can I do this in my code?
With PDFPrinter
' Set Paper size
.Option("DevMode.PapSize") = 256 ' means custom paper size
.Option("DevMode.PapWidth") = 1200
.Option("DevMode.PapHeight") = 300
End With
Tracker Software (Project Director)
When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
I tried the PDFPrinter initializing part of my code, then in Excel 2007, opened the properties window of the printer "PDF-XChange 4.0 Sample" which was just created by my code, and found that only the sheetsize was changed to custom while the paper size still remained as "Standard A4". This meant that the two lines I added to my code could't change the paper size.
Cannot reproduce the trouble: have inserted you code into Excel file, put break point into PDFPrint method after PDFPrintInit call, and now I see printers settings:
Tracker Software (Project Director)
When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
Would you please try again using the attached excel file and code? I tested the code on the attached file with the break point just after PDFPrintInit call, and got the printer paper size as "Standard A4".
Thank you very much. Hope the problem can be solved before the holiday, .
To be honest, I cannot reproduce the problem even with your file. On my side, when I set breakpoint on mentioned line, I see paper and sheet size as custom with dimensions of 14000x300 as was specified.
Are you sure you are using the latest build of the driver ?
Tracker Software (Project Director)
When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
Thank you for your reply, Ivan. I'm sorry to bother you just before the holiday.
Yes, I'm sure I used the latest version, because I downloaded the driver and installed it on our server on Dec 23, tested my code there and got the same result. I don't know why you got different thing, is there any configuration in the system printer or Excel?
I tested the code on my laptop which is installed WinXP Professional SP3 and our server which is installed Windows Server 2003 R2 Standard Edition SP2, and the same version of Office. The excel version is:
Microsoft Office Excel 2007(12.0.6514.5000) SP2 MSO(12.0.6425.1000) Part of Microsoft Office Enterprise 2007