![](/img/trans.png)
[英]How to improve the quality of JPEG images that are generated from PDFs using Ghostscript?
[英]How to embed JPEG images in XAML generated XPS?
我正在使用XpsDocument类动态生成XPS
文件。 在我的XAML
模板中,我将JPEG
图像嵌入到Image容器中。 但是,生成的XPS
中嵌入的图像始终是PNG
图像 -对于某些类型的图像,会导致文件很大。
似乎文档编写者将渲染的图像解释为位图,然后将其另存为PNG
。
这是产生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();
}
问:如何强制将XAML
渲染的图像保存为JPEG
在生成的XPS
?
我认为您必须更改创建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
}
这里的关键是使用docWriter.AddFixedPage();
获取IXpsFixedPageWriter
docWriter.AddFixedPage();
。 这使您可以重新创建文档,将图像添加到想要的位置。
不确定您是否可以编辑已经创建的FixedDocument
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.