简体   繁体   English

如何在XAML生成的XPS中嵌入JPEG图像?

[英]How to embed JPEG images in XAML generated XPS?

I'm generating an XPS file on the fly using the XpsDocument class. 我正在使用XpsDocument类动态生成XPS文件。 In my XAML template I embed a JPEG image in an Image container. 在我的XAML模板中,我将JPEG图像嵌入到Image容器中。 However, the embedded images in the resulting XPS are always PNG images - resulting in a very large files for certain types of images. 但是,生成的XPS中嵌入的图像始终是PNG图像 -对于某些类型的图像,会导致文件很大。

It seems the document writer interprets the rendered images as bitmaps and then saves them as PNG . 似乎文档编写者将渲染的图像解释为位图,然后将其另存为PNG

Here's the code that produces the XPS : 这是产生XPS的代码:

void ConvertToXps(IEnumerable<FixedDocument> fixedDocuments, Stream outputStream)
{
    var package = Package.Open(outputStream, FileMode.Create);
    var xpsDoc = new XpsDocument(package, CompressionOption.Normal);
    var xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);

    // XPS documents are built using fixed document sequences.
    var fixedDocSeq = new FixedDocumentSequence();

    // A4 = 210 x 297 mm = 8.267 x 11.692 inches = 793.632 * 1122.432 dots
    fixedDocSeq.DocumentPaginator.PageSize = new Size(793.632, 1122.432);

    foreach (var fixedDocument in fixedDocuments)
    {
        var docRef = new DocumentReference();
        docRef.BeginInit();
        docRef.SetDocument(fixedDocument);
        docRef.EndInit();
        ((IAddChild)fixedDocSeq).AddChild(docRef);
    }

    // Write out our fixed document to XPS.
    xpsWriter.Write(fixedDocSeq.DocumentPaginator);

    xpsDoc.Close();
    package.Close();
}

Q: How can I force my XAML rendered images to be saved as JPEG in the generated XPS ? 问:如何强制将XAML渲染的图像保存为JPEG在生成的XPS

I think you have to change the way you create your XPS document. 我认为您必须更改创建XPS文档的方式。

var package = Package.Open(outputStream, FileMode.Create);
var xpsDoc = new XpsDocument(package, CompressionOption.Normal);
var xpsWriter = xpsDoc.AddFixedDocumentSequence();

var fixedDocSeq = xpsDoc.GetFixedDocumentSequence();

// A4 = 210 x 297 mm = 8.267 x 11.692 inches = 793.632 * 1122.432 dots
fixedDocSeq.DocumentPaginator.PageSize = new Size(793.632, 1122.432);

foreach (var fixedDocument in fixedDocuments)
{
    var docWriter = xpsWriter.AddFixedDocument();

    var pageWriter = docWriter.AddFixedPage();

    var image = pageWriter.AddImage(XpsImageType.JpegImageType);

    Stream imageStream = image.GetStream();

    //Write your image to stream

    //Write the rest of your document based on the fixedDocument object
}

The key here is getting the IXpsFixedPageWriter with the docWriter.AddFixedPage(); 这里的关键是使用docWriter.AddFixedPage();获取IXpsFixedPageWriter docWriter.AddFixedPage(); . This allows you to recreate your document, adding the images where you want them to be. 这使您可以重新创建文档,将图像添加到想要的位置。

Not sure you can edit the already created FixedDocument unfortunately. 不确定您是否可以编辑已经创建的FixedDocument

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM