[Solved] Converting an XPS Document to a PDF

If you are a C/C++/C# developer and have any questions about using our products from your application here is the place to post them.
Post Reply
kpyancey
Posts: 7
Joined: Mon Dec 31 2007

[Solved] Converting an XPS Document to a PDF

Post by kpyancey » Wed Jan 02 2008

Sorry for the double-post. I guess I posted this to the wrong forum originally.

I'm evaluating the PDF Creator library for a project I'm working on. I can't reveal too much detail about the project itself, but the software I am writing essentially extracts data from a database based upon criteria specified in a request and outputs a report in one of several possible formats. I already have the code to generate the report as an XPS document, but I also need to support PDF. To do this, I was hoping to use PDF Creator to import the XPS Document, and then save it to a stream as a PDF (note that I'm using the non-gui version of the API).

Here is the code I have right now:

Code: Select all

public void Format(ReportData reportData, System.IO.Stream outputStream) {
            /* Snip: build a FlowDocument containing the data specified in reportData */
            Stream xpsStream = new MemoryStream();
            using(Package package = ZipPackage.Open(xpsStream, FileMode.Create)) {
                using(XpsDocument xpsDoc = new XpsDocument(package)) {
                    XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
                    writer.Document.ColumnWidth = 400;
                    xpsWriter.Write(((IDocumentPaginatorSource)flowDocument).DocumentPaginator);
                }
            }
            xpsStream.Flush();  //probably not necessary, but just in case
            xpsStream.Seek(0, SeekOrigin.Begin);
            acPDFCreatorLib.Initialize();
            acPDFCreatorLib.SetLicenseKey("Evaluation Version Developer Pro", /*Snip: License Key Here*/);
            IacDocument pdfDocument = new IacDocument();
            Stream pdfStream;
            //Make sure the stream passed in supports seeking.  Otherwise,
            //we'll have to cache it into a MemoryStream and write to the
            //output stream afterwards.
            if(!outputStream.CanSeek) {
                pdfStream = new MemoryStream();
            } else {
                pdfStream = outputStream;
            }
            pdfDocument.Open(xpsStream, String.Empty);
            pdfDocument.Save(pdfStream, IacFileSaveOption.acFileSaveView);
            //If we wrote the PDF to a temporary stream, write the contents
            //of that stream to the output stream.
            if(pdfStream != outputStream) {
                pdfStream.Flush();
                outputStream.Write(((MemoryStream)pdfStream).GetBuffer(), 0, (int)((MemoryStream)pdfStream).Length);
            }
        }
This code creates the XPS document correctly. The XPS Document varies in size from 5 pages to 50 pages. However, when I save it as a PDF, I get a 1 page document that has numerous overlapping red square outlines arranged diagonally. I have no idea why this would be. I've checked the code several times, and everything seems to work until I call pdfDocument.Open. After that call, according to the debugger, the number of pages in the pdfDocument are 1. So, I know at that point something has gone wrong. Can anyone give me an idea of what I'm doing wrong? :?

Joan
Amyuni Team
Posts: 2799
Joined: Wed Sep 11 2002
Contact:

Post by Joan » Wed Jan 23 2008

For reference:

The answer to this post is at: http://www.amyuni.com/forum/viewtopic.php?p=7090#7090
Custom Brand the Amyuni PDF Printer Driver http://www.amyuni.com/en/developer/branding/index.html

Amyuni PDF Converter tested for true PDF performance. View results - http://www.amyuni.com/benchmark

Post Reply