Câu hỏiIn ấn BlockUIContainer để XpsDocument/FixedDocument
- Làm thế nào để bạn in một FlowDocument có
BlockUIContainer
? - Làm cách nào để có thể đo lường/Cập nhật/Sắp xếp trên FlowDocument?
nền
Tôi đã một tạo FlowDocument
với đoạn văn bản với một vài yếu tố Rectangle
điền DrawingBrushes
từ một cuốn từ điển tài nguyên và BlockUIContainer
với các điều khiển tùy chỉnh. tài liệu
Các render một cách chính xác khi quan sát trong bất kỳ FlowDocument * khiển TUY NHIÊN khi tài liệu được chuyển đổi sang một FixedDocument/XpsDocument, không ai trong số các Rectangle
hoặc BlockUIContainer
yếu tố render.
Tôi gần như chắc chắn là do điều khiển có không được đo/sắp xếp, tuy nhiên không thể tìm ra cách ép buộc điều đó xảy ra trước khi nó được chuyển đổi thành XpsDocument.
tôi đã bước đi trên
LogicalTree
đệ quy và thực hiện những điều sau đây,UIElement element = (UIElement)d; element.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); element.Arrange(new Rect(element.DesiredSize)); element.UpdateLayout();
nơi
d
là mộtDependencyObject
. Tôi có thể thấy rằng điều này đặt các thuộc tínhActualWidth
vàActualHeight
khi được chỉ định trong trình gỡ lỗi.Tôi đã cố gắng buộc
Dispatcher
để hiển thị như được đề xuất bởi Will ♦.
Mã dùng để in các XpsDocument
public class XpsDocumentConverter
{
public static XpsDocumentReference CreateXpsDocument(FlowDocument document)
{
// Need to clone the document so that the paginator can work
FlowDocument clonedDocument = DocumentHelper.Clone<FlowDocument>(document);
Uri uri = new Uri(String.Format("pack://temp_{0}.xps/", Guid.NewGuid().ToString("N")));
MemoryStream ms = new MemoryStream();
Package pkg = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
PackageStore.AddPackage(uri, pkg);
XpsDocument xpsDocument = new XpsDocument(pkg, CompressionOption.Normal, uri.AbsoluteUri);
XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDocument), false);
DocumentPaginator paginator = new FixedDocumentPaginator(clonedDocument, A4PageDefinition.Default);
rsm.SaveAsXaml(paginator);
return new XpsDocumentReference(ms, xpsDocument);
}
}
Như bạn có thể thấy tôi cũng đang sử dụng một tùy chỉnh DocumentPaginator
tên 'FixedDocumentPaginator'; tuy nhiên tôi sẽ không đăng mã đó vì tôi nghi ngờ vấn đề này là do thời điểm bắt đầu phân trang tài liệu trong GetPage(int pageNumber)
mọi thứ đã được chuyển đổi thành Visual
và đã quá muộn để bố cục.
Sửa
Hmm. Khi tôi đánh máy này, một ý nghĩ chỉ xảy ra với tôi rằng tài liệu nhân bản được sao chép có thể chưa hoàn thành được Measure/Arrange/UpdateLayout
.
Câu hỏi: Làm cách nào để có thể đo lường/Cập nhật/sắp xếp trên FlowDocument?
Có thể một hack mà tôi có thể làm việc sẽ hiển thị tài liệu nhân bản trong một trong các FlowDocumentViewers (có lẽ ngoài màn hình).
Một giải pháp khả thi mà tôi chỉ học được về và đã không cố gắng sẽ được gọi: ContextLayoutManager.From(Dispatcher.CurrentDispatcher).UpdateLayout();
ContextLayoutManager
đi cây hợp lý cho bạn và cập nhật trình bày.
Mã được sử dụng để nhân bản tài liệu
public static FlowDocument Clone(FlowDocument originalDocument)
{
FlowDocument clonedDocument = new FlowDocument();
TextRange sourceDocument = new TextRange(originalDocument.ContentStart, originalDocument.ContentEnd);
TextRange clonedDocumentRange = new TextRange(clonedDocument.ContentStart, clonedDocument.ContentEnd);
try
{
using (MemoryStream ms = new MemoryStream())
{
sourceDocument.Save(ms, DataFormats.XamlPackage);
clonedDocumentRange.Load(ms, DataFormats.XamlPackage);
}
clonedDocument.ColumnWidth = originalDocument.ColumnWidth;
clonedDocument.PageWidth = originalDocument.PageWidth;
clonedDocument.PageHeight = originalDocument.PageHeight;
clonedDocument.PagePadding = originalDocument.PagePadding;
clonedDocument.LineStackingStrategy = clonedDocument.LineStackingStrategy;
return clonedDocument;
}
catch (Exception)
{
}
return null;
}
Cảm ơn rất nhiều vì điều này, chỉ thông qua địa ngục này, bạn đã giúp rất nhiều =) – JMK
Điều gì đang xảy ra trong 'XpsDocumentReference'? Tôi tiếp tục cố gắng in từ một 'PrintDialog' bằng cách kéo' DocumentPaginator' từ tài liệu XPS qua 'GetFixedDocumentSequence(). DocumentPaginator'. Tôi nhận được một ngoại lệ phân tích Xaml kết quả từ một ngoại lệ InvalidURI. Rõ ràng URI tài liệu cố định bị xáo trộn bằng cách nào đó. –
@AustinMullins xem xét câu trả lời của tôi tại http://stackoverflow.com/questions/9647401/documentviewer-to-richtextbox-binding-error Nó cho thấy việc triển khai 'XpsDocumentReference'. Đã một vài năm kể từ khi tôi nhìn vào XPS. Rất vui được xem lại và trợ giúp.Nó sẽ là một cơ hội tốt để tạo ra một thư viện nguồn mở. – Dennis