Custom Page Size without associated PageMediaSizeName not Working with XpsDocumentWriter
- I have an application that is using the XpsDocumentWriter to write FixedDocuments that it generates to a PDF print driver installed on the machine. A PrintTicket is created for the document and passed to the Write method on the XpsDocumentWriter. For documents that are a custom page size and have no PageMediaSizeName associated with them, the PageMediaSize set on the PrintTicket is created with just the page height and width set. When these documents with a custom page size are written to a PDF print driver installed on the machine, the resulting PDF page size is not correct, however if the document with a custom page size is written to an XPS file, the XPS displays in the proper custom page size. Is there anyway to get the custom page size to properly be passed/set on the resulting PDF?
Here is an example of the code:
//Create XpsDocumentWriter to write to PDF printer installed on machine
PrintServer printServer = new PrintServer(@"\\" + System.Environment.MachineName);
PrintQueue printQueue = new PrintQueue(printServer, "PDF printer Name");
XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(printQueue);
//Get FixedDocument from custom method.
FixedDocument fixedDoc = GetFixedDocument();
//Create PrintTicket for document and set its page size to the proper custom page size
PrintTicket printTicket = new PrintTicket();
//Set the page size to a custom 3.5" x 2.5" page
printTicket.PageMediaSize = new PageMediaSize(3.5 * 96.0, 2.5 * 96.0);
//Write out the FixedDocument
docWriter.Write(fixedDoc, printTicket);
全部回复
I determined how to get this to work, at least in my scenario, and I tested it against two different PDF print drivers . The code change does not seem right or logical but it works. In summary, if a PageMediaSizeName that is NOT supported by the printer is passed/set on the PageMediaSize for the PrintTicket, and the PrintTicket is merged with the UserPrintTicket of the PrintQueue, then the resulting PDF has the correct custom page size. Setting the PageMediaSizeName to Unknown did not work, however the resulting ValidatedPrintTicket does show the PageMediaSizeName as Unknown. The code below should be enhanced to insure the PageMediaSizeName is not supported by the PrintQueue, but it demonstrates the fix.
Code sample:
PrintServer printServer = new PrintServer(@"\\" + System.Environment.MachineName);
PrintQueue printQueue = new PrintQueue(printServer, "PDF printer Name");
XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(printQueue);
//Get FixedDocument from custom method.
FixedDocument fixedDoc = GetFixedDocument();
//Create PrintTicket for document and set its page size to the proper custom page size
PrintTicket printTicket = new PrintTicket();
//Set the page size to a custom 3.5" x 2.5" page passing PageMediaSizeName not supported by print driver
printTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.JapanLPhoto, 3.5 * 96.0, 2.5 * 96.0);
ValidationResult result = printQueue.MergeAndValidatePrintTicket(printQueue.UserPrintTicket, printTicket);
printQueue.UserPrintTicket = result.ValidatedPrintTicket;
//Write out the FixedDocument
docWriter.Write(fixedDoc, result.ValidatedPrintTicket);

