我有一个简单的程序,可以读取XPS文件并创建重复的页面。
如您所见,操作非常简单。但是我发现,如果创建的页面过多,则会抛出异常“ System.NotSupportedException。无法识别URI前缀”。。
我创建了一个测试程序,该程序增加了while循环中的页面数量,如您所见,我将打印用于存储xps文件的每个临时内存流的URI(仅用于检查冲突或进行调试)我发现每次计数器达到 11 时,都会在此处引发异常。
翻译:System.NotSupportedException。无法识别URI前缀
有趣的是,这个问题是可重复的,我对发生的事情一无所知:\任何建议将不胜感激,谢谢!
class Program
{
[STAThread]
static void Main(string[] args)
{
var xpsFile = File.ReadAllBytes(@"Test.xps");
Stopwatch sw1 = new Stopwatch();
int i = 5;
while (true)
{
sw1.Start();
var output = createDuplicatePages(xpsFile, i++);
sw1.Stop();
File.WriteAllBytes(@"output.xps", output);
Console.WriteLine($"Trial {i} Completed. {sw1.ElapsedMilliseconds}");
Console.ReadLine();
}
}
static byte[] createDuplicatePages(byte[] originalXPS, int numberOfCopies)
{
byte[] ret = originalXPS;
try
{
using (MemoryStream outputStream = new MemoryStream())
{
Package container = Package.Open(outputStream, FileMode.Create);
XpsDocument xpsDoc = new XpsDocument(container);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
SerializerWriterCollator vxpsd = writer.CreateVisualsCollator();
vxpsd.BeginBatchWrite();
for (int nthCopy = 0; nthCopy < numberOfCopies; nthCopy++)
{
MemoryStream inputStream = new MemoryStream(originalXPS);
string memoryStreamUri = "memorystream://" + Path.GetFileName(Guid.NewGuid().ToString() + ".xps");
Uri packageUri = new Uri(memoryStreamUri);
Console.WriteLine(packageUri.AbsoluteUri);
Package oldPackage = Package.Open(inputStream);
PackageStore.AddPackage(packageUri, oldPackage);
using (XpsDocument xpsOld = new XpsDocument(oldPackage, CompressionOption.Normal, memoryStreamUri))
{
FixedDocumentSequence seqOld = xpsOld.GetFixedDocumentSequence();
foreach (DocumentReference r in seqOld.References)
{
FixedDocument d = r.GetDocument(false);
foreach (PageContent pc in d.Pages)
{
FixedPage fixedPage = pc.GetPageRoot(false);
double width = fixedPage.Width;
double height = fixedPage.Height;
System.Windows.Size sz = new System.Windows.Size(width, height);
fixedPage.Width = width;
fixedPage.Height = height;
fixedPage.Measure(sz);
fixedPage.Arrange(new System.Windows.Rect(new System.Windows.Point(), sz));
ContainerVisual newPage = new ContainerVisual();
newPage.Children.Add(fixedPage);
vxpsd.Write(newPage);
}
}
PackageStore.RemovePackage(packageUri);
}
oldPackage.Close();
}
vxpsd.EndBatchWrite();
container.Close();
ret = outputStream.ToArray();
}
return ret;
}
catch (Exception e)
{
return originalXPS;
}
}
}
Test.xps:
https://drive.google.com/file/d/1kXrcKeDgAeSsIGqQwx-UpRhLdx8eW0PV/view?usp=sharing
------ 10/9/2018更新-----
我偶然发现以下修改确实解决了问题,但是为什么呢?不知道:\
vxpsd.BeginBatchWrite();
for (int nthCopy = 0; nthCopy < numberOfCopies; nthCopy++)
{
MemoryStream inputStream = new MemoryStream(originalXPS);
string memoryStreamUri = "memorystream://" + Path.GetFileName(Guid.NewGuid().ToString() + ".xps");
收件人
vxpsd.BeginBatchWrite();
string memoryStreamUri = "memorystream://" + Path.GetFileName(Guid.NewGuid().ToString() + ".xps");
for (int nthCopy = 0; nthCopy < numberOfCopies; nthCopy++)
{
MemoryStream inputStream = new MemoryStream(originalXPS);