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);
}
}