Importa RTF con immagini in xbap
Domanda
Devo importare un documento RTF in un FlowDocument per ulteriori analisi .. Ma ho un problema molto strano:
public string ConvertRTF(byte[] bytes)
{
if (bytes == null)
{
throw new ArgumentNullException();
}
FlowDocument document = new FlowDocument();
// open the file for reading
using (MemoryStream stream = new MemoryStream(bytes, true))
{
// create a TextRange around the entire document
TextRange documentTextRange = new TextRange(document.ContentStart, document.ContentEnd);
if (documentTextRange.CanLoad(DataFormats.Rtf))
documentTextRange.Load(stream, DataFormats.Rtf);
}
return XamlWriter.Save(document);
}
Ho testato questo metodo in tre diversi progetti:
- App Wpf stand-alone : non mi dà alcun problema, ma purtroppo non posso usare questo tipo di applicazione.
- App console : spesso funziona, ma di tanto in tanto si rompe sui documenti con l'immagine e non riesco a trovare il dito quando si rompe e perché ... L'errore che sto ricevendo è sul metodo Load di TextRange: " Struttura non riconosciuta nel formato dati "Rich Text Format". Nome del parametro: stream "
- Applicazione Xbap : non supera nemmeno il metodo CanLoad ... :( Quindi mi dà il nome di Jack come risultato ...
La cosa strana è che quando lo collaudo con l'app console funziona senza errori nella seguente costruzione:
[STAThread]
static void Main(string[] args)
{
OpenFileDialog dialog = new OpenFileDialog
{
Filter = "import files (*.rtf)|*.rtf"
};
if (dialog.ShowDialog() != DialogResult.OK)
return;
byte[] data;
using (Stream filestream = dialog.OpenFile())
{
int offset = 0;
data = new byte[filestream.Length];
int remaining = data.Length;
while (remaining > 0)
{
int read = filestream.Read(data, offset, remaining);
if (read <= 0)
throw new EndOfStreamException
(String.Format("End of stream reached with {0} bytes left to read", remaining));
remaining -= read;
offset += read;
}
}
FlowDocument document = new FlowDocument();
using (MemoryStream stream = new MemoryStream(data))
{
// create a TextRange around the entire document
TextRange documentTextRange = new TextRange(document.ContentStart, document.ContentEnd);
documentTextRange.Load(stream, DataFormats.Rtf);
}
Console.WriteLine("test ok");
}
Il che mi rende semplicemente all'oscuro, perché è esattamente quello che sto facendo, ma poi sono diviso in due fasi ... prima recuperare i bit, quindi utilizzare il memorystream per trasformarlo in un RTF ... :(
Può essere che in qualche modo ci sia un conflitto in qualche versione di dll? Stiamo usando 3.5 SP1 per il nostro progetto ...
Qualcuno può aiutarmi a trovare una soluzione per una delle ultime due possibilità sopra menzionate?
Grazie
Soluzione 2
Apparentemente non può essere fatto.
Quello che abbiamo finito è l'invio di RTF al server che ha più privilegi e rispedisce il risultato al client. Brutto, ma funziona.
Altri suggerimenti
Probabilmente hai problemi con il livello di attendibilità. Le impostazioni predefinite delle applicazioni Internet Xbap sono parzialmente attendibili. È possibile utilizzare il certificato per consentire la piena fiducia con le applicazioni Internet xpab.