ОК, так что я обнаружил, что это было наиболее удобно. Я использую код по этой ссылке https://social.msdn.microsoft.com/Forums/vstudio/en-US/536919f0-f9c5-4431-939d-5e76657c9a5d/save-canvas-element-to-xamlxps-file?forum=wpf И теперь я конвертирую его в pdf, используя библиотеку syncfusion (которую получила моя компания) https://www.syncfusion.com/kb/9077/how-to-convert-xps-to-pdf-in-c-vb-net, но я думаю, что это тоже можно сделать с помощью pdfSharp.
PDF можно просматривать в программе просмотра, оно хорошего качества, а при наличии текста его можно выбрать.
private async Task PrintingStreamToViewer(Canvas canvas, string pageName)//, PrintDialog prnt)
{
using (MemoryStream ms = new MemoryStream())
{
string basename = Path.GetFileName($"temp{DateTime.Now.Second}.xps");
string directory = Path.GetDirectoryName(Path.GetTempPath());
var uri = new Uri($"{directory}\\{basename}", UriKind.Absolute);
Export(uri, canvas);
XPSToPdfConverter converter = new XPSToPdfConverter();
//Convert the XPS to PDF
PdfDocument document = converter.Convert(uri.AbsolutePath);
var pdfPath = new Uri($"{directory}\\{pageName}{DateTime.Now.Ticks}.pdf", UriKind.Absolute);
//Save the document
document.Save(pdfPath.AbsolutePath);
//Close the document
document.Close(true);
var pdfViewModel = PresentationInstances.Container.Resolve<PdfViewerControlViewModel>();
try
{
pdfViewModel.SetPdfFile(pdfPath, (int)canvas.Width, (int)canvas.Height);
}
catch (Exception e)
{
PresHelper.WriteToDebug(e);
}
await _starMainPage.DialogService.ShowContentDialogAsync<IPdfViewerControl>(pdfViewModel);
}
}
Метод экспорта по приведенной выше ссылке:
public void Export(Uri path, FrameworkElement surface)
{
if (path == null) return;
// Save current canvas transorm
Transform transform = surface.LayoutTransform;
// Temporarily reset the layout transform before saving
surface.LayoutTransform = null;
// Get the size of the canvas
Size size = new Size(surface.Width, surface.Height);
// Measure and arrange elements
surface.Measure(size);
surface.Arrange(new Rect(size));
// Open new package
Package package = Package.Open(path.LocalPath, FileMode.Create);
// Create new xps document based on the package opened
XpsDocument doc = new XpsDocument(package);
// Create an instance of XpsDocumentWriter for the document
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
// Write the canvas (as Visual) to the document
writer.Write(surface);
// Close document
doc.Close();
// Close package
package.Close();
// Restore previously saved layout
surface.LayoutTransform = transform;
}