2

I have a pdf sharp document which I'm generating and saving it to a single file works well. If I try and save to several files from the same pdf document I get the following error message:

Test Error : UnitTests.PdfFocusCalResultOutputterTester.Test_MakePDF
System.InvalidOperationException : The dictionary already has a stream.
at PdfSharp.Pdf.PdfDictionary.CreateStream(Byte[] value)
at PdfSharp.Pdf.Advanced.PdfToUnicodeMap.PrepareForSave()
at PdfSharp.Pdf.Advanced.PdfType0Font.PrepareForSave()
at PdfSharp.Pdf.Advanced.PdfFontTable.PrepareForSave()
at PdfSharp.Pdf.PdfDocument.PrepareForSave()
at PdfSharp.Pdf.PdfDocument.DoSave(PdfWriter writer)
at PdfSharp.Pdf.PdfDocument.Save(Stream stream, Boolean closeStream)
at ResultOutputter.PdfFocusCalResultOutputter.RenderToFile(String filename) in c:\projects\testing\pdffocuscalresultoutputter.cpp:line 802

I'm simply calling PdfSharp::Pdf::PdfDocument::Save on my instance of a PdfDocument as follows:

System::IO::FileStream^ s = gcnew System::IO::FileStream("firstpdf.pdf", System::IO::FileMode::Create);
m_document->Save(s, false);
s->Flush();
s->Close();

System::IO::FileStream^ s2 = gcnew System::IO::FileStream("secondpdf.pdf", System::IO::FileMode::Create);
m_document->Save(s2, false);
s2->Flush();
s2->Close();
Jon Cage
  • 36,366
  • 38
  • 137
  • 215

1 Answers1

3
  1. It's a bug in PDFsharp, your code should work.
  2. Your code is not efficient. Save once to a stream, then write that stream into two or more files.
  • So if I want to re-use a pdf I've generated I have to re-create it? ...or does changing the pdf clear the stream? – Jon Cage May 08 '12 at 12:57
  • If anyone else needs to do this, you can use a memory stream: http://stackoverflow.com/questions/1073277/pdfsharp-save-to-memorystream – Jon Cage May 08 '12 at 13:14
  • Open-modify-save does work. Open-modify-save-modify-save does not work. Open-modify-save-open-modify-save will work, so if you want to save an intermediate step to a file, save to a stream, copy the stream to a file, then open the stream and make further modifications. I'm sorry for the inconvenience, but I was told it's rather complicated to fix the "save bug". – I liked the old Stack Overflow May 08 '12 at 13:16
  • I guess I'll have to grin and bear it for the time being. Thanks for the clarification though... – Jon Cage May 08 '12 at 14:45