You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从FixedDocument生成的XPS文档提取DocumentPage时图像丢失的问题求助

FixedDocument生成的XPS文档提取DocumentPage时图像丢失的问题求助

各位大佬好,我最近碰到一个棘手的问题,想请教下大家有没有解决思路。

我写了一段代码,把FixedDocument保存为XPS文件的过程是正常的——fd.pages.child里的文本和图像都能正确渲染显示。但当我从生成的XPS文档中获取FixedDocumentSequence,再遍历提取DocumentPage的时候,页面里只剩下文本,图像完全加载不出来,变成了空路径。

我已经尝试过更换多种渲染格式,也改成异步渲染模式了,但图像依旧无法正常显示。有没有大佬知道这是什么原因,该怎么修复这个问题?

附上我的代码:

Private Sub SaveFixDoc(ByVal fd As FixedDocument, name As String)

Dim filename As String = My.Settings.Path2Labels & "\" & name & ".xps"

Try

Dim xpsd As XpsDocument = New XpsDocument(filename, FileAccess.ReadWrite)

Dim xpsdw As XpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsd)

xpsdw.Write(fd) ' Write the FixedDocument as a document.

' ** All the way to here the fd.pages.child have my text and my images and they are rendered ok

Dim sequence As FixedDocumentSequence = xpsd.GetFixedDocumentSequence()

For pageCount As Integer = 0 To sequence.DocumentPaginator.PageCount - 1

Dim page As DocumentPage = sequence.DocumentPaginator.GetPage(pageCount)

'  **but in here the page is only taking the text and completely ignoring the images they just appear as empty paths **

Dim toBitmap As New RenderTargetBitmap(4 * (CInt(page.Size.Width)), 4 * (CInt(page.Size.Height)), 384, 384, System.Windows.Media.PixelFormats.Pbgra32)

toBitmap.Render(page.Visual)

Dim bmpEncoder As JpegBitmapEncoder = New JpegBitmapEncoder()

bmpEncoder.Frames.Add(BitmapFrame.Create(toBitmap))

Dim fStream As New FileStream(My.Settings.Path2Labels & "\" & name & ".jpeg", FileMode.Create, FileAccess.Write)

bmpEncoder.Save(fStream)

fStream.Close()

Next

xpsd.Close()

Catch ex As Exception

Dim st As New StackTrace(True)

st = New StackTrace(ex, True)

Utilities.WriteToApplicationLog(New LogMessage() With {

.MessageType = "Error",

.MessageDetails = ex.ToString})

Mediator.NotifyColleagues("MessageBox", New MessageBoxObject() With {

.message =

My.Resources.mbInstrError & " SaveSingleFixedContentDocument " & " " &

st.GetFrame(0).GetFileLineNumber().ToString,

.caption = My.Resources.mbExcelError,

.button = MessageBoxButton.OK,

.image = MessageBoxImage.Error})

End Try

End Sub

状态说明:

  • fd.page.child:包含完整的文本和图像,渲染状态正常
  • page.visual:仅保留文本内容,图像变为空路径

备注:内容来源于stack exchange,提问作者2al2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 13:14:06