This is a migrated thread and some comments may be shown as answers.

Export to Pdf: PdfFormatProvider not working

6 Answers 327 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
ajin prasad
Top achievements
Rank 1
ajin prasad asked on 09 Sep 2013, 04:01 PM
Hi All,

I am using PdfFormatProvider to export PDF document. It throws the error "Index was outside the bounds of the array." in the line provider2.Export(bigbook, output); 

The same code works fine with 
XamlFormatProvider
TxtFormatProvider
HtmlFormatProvider

please see the below code for implementation

  void ExportPDF2_Click(object sender, RoutedEventArgs e)
        {
           this.Cursor = Cursors.Wait;

          

            RadDocument theContent = LoadContent();

            RadDocument bigbook = new RadDocument();
            Section section1 = new Section();
            Paragraph paragraph1 = new Paragraph();

            Span span1 = new Span(TVM.TOC3);
            paragraph1.Inlines.Add(span1);
            section1.Blocks.Add(paragraph1);
            bigbook.Sections.Add(section1);

            
            foreach (Section section in theContent.Sections)
            {
                Section copySection = section.CreateDeepCopy() as Section;
                theContent.Sections.Remove(section);
                bigbook.Sections.Add(copySection);
            }

            this.Cursor = Cursors.Arrow;

            //-----------------------------------------------

            PdfFormatProvider provider2 = new PdfFormatProvider();
           
        
            

            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = ".pdf";
            saveDialog.Filter = "PDF Document|*.pdf";
            bool? dialogResult = saveDialog.ShowDialog();
            if (dialogResult == true)
            {
                using (System.IO.Stream output = saveDialog.OpenFile())
                {
                    

                    provider2.Export(bigbook, output); //Error at this line
                    MessageBox.Show("Saved Successfully!");
                }
            }
        }

This code works fine with XamlFormatProvider, TxtFormatProvider, HtmlFormatProvider. Issue is only with PDFFormatProvider

Can anyone help me in this?

6 Answers, 1 is accepted

Sort by
0
Vishal
Top achievements
Rank 1
answered on 11 Sep 2013, 09:49 AM
public static bool ConvertXamlToPDF(RadDocument document, string pdfPath)
        {
            Log.Info(System.Reflection.MethodBase.GetCurrentMethod().Name + " entering.");
            try
            {
                PdfFormatProvider provider = new PdfFormatProvider();
                var bytes = provider.Export(document);//Same Exception of "Index was outside the bounds of the array."

                if (File.Exists(pdfPath))
                {
                    File.SetAttributes(pdfPath, FileAttributes.Normal);
                }

                var fs = new FileStream(pdfPath, FileMode.Create);
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();
            }
            catch (Exception ex)
            {
                Log.Error(System.Reflection.MethodBase.GetCurrentMethod().Name + " leaving with exception : " + ex.Message);
                return (false);
            }
            Log.Info(System.Reflection.MethodBase.GetCurrentMethod().Name + " leaving.");
            return (true);
        }
0
Accepted
Petya
Telerik team
answered on 11 Sep 2013, 03:24 PM
Hello,

Thank you for contacting us!

I tried replicating the issue you are observing, but to no avail. Could you please additional details about your setup and the content of the document you are loading? Also, please verify that you are using 2013.2 724 version of RadControls for Silverlight and all required assembly references are added to your project.

Generally, I'd like to point out that it is not recommended to use the CreateDeepCopy() method of document elements and the current approach you are using to merge documents might not always produce the expected result. Instead, I'd suggest you use the InsertFragment() method of RadDocumentEditor. You can revise the code of your application like this:
this.Cursor = Cursors.Wait;
 
RadDocument theContent = LoadContent();
 
RadDocument bigbook = new RadDocument()
    {
        LayoutMode = DocumentLayoutMode.Paged
    };
RadDocumentEditor documentEditor = new RadDocumentEditor(bigbook);
 
documentEditor.Insert(TVM.TOC3);
 
documentEditor.InsertSectionBreak();
documentEditor.InsertFragment(new DocumentFragment(theContent));
 
this.Cursor = Cursors.Arrow;

Additionally, please note that in Silverlight SaveFileDialog can only be shown if user-initiated. With your current setup if the loading of the document in the LoadContent() method or the logic executed after that takes longer time a security exception might occur. That said, I'd suggest initializing the dialog first and executing the other logic after that.

I hope these tips are helpful! If you are still experiencing problems please get back to us with additional details about your setup and the document you are trying to export. We would appreciate if you can provide us with such document exported to XAML format, so that we can troubleshoot the issue on our end.

I'm looking forward to your reply.

Regards,
Petya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Vishal
Top achievements
Rank 1
answered on 12 Sep 2013, 04:26 AM
Hi Petya,
I'm also facing same problem. It was working well when i was using older versions of dlls, but not not working well from 2013 q2 dlls.

stack trace is

   at Telerik.Windows.Documents.FormatProviders.Pdf.RadPdf.PdfContentsWriter.CalculateKernings(String text, PdfType0Font font) in c:\TB\221\WPF_Scrum\Release_WPF\Sources\Development\Documents\FormatProviders\Pdf\RadPdf\PdfContentsWriter.cs:line 117
   at Telerik.Windows.Documents.FormatProviders.Pdf.RadPdf.PdfContentsWriter.BuildDrawingString(String text, PdfType0Font font) in c:\TB\221\WPF_Scrum\Release_WPF\Sources\Development\Documents\FormatProviders\Pdf\RadPdf\PdfContentsWriter.cs:line 100
   at Telerik.Windows.Documents.FormatProviders.Pdf.RadPdf.PdfContentsWriter.DrawString(Color color, Point position, PdfType0Font type0Font, Double fontSize, String text) in c:\TB\221\WPF_Scrum\Release_WPF\Sources\Development\Documents\FormatProviders\Pdf\RadPdf\PdfContentsWriter.cs:line 65
   at Telerik.Windows.Documents.FormatProviders.Pdf.PdfDocumentExporter.ExportSpan(SpanLayoutBox spanBox, PdfContentsWriter writer) in c:\TB\221\WPF_Scrum\Release_WPF\Sources\Development\Documents\FormatProviders\Pdf\PdfDocumentExporter.cs:line 204
   at Telerik.Windows.Documents.FormatProviders.Pdf.PdfDocumentExporter.ExportSectionLayoutBox(SectionLayoutBox sectionBox, PdfContentsWriter writer) in c:\TB\221\WPF_Scrum\Release_WPF\Sources\Development\Documents\FormatProviders\Pdf\PdfDocumentExporter.cs:line 409
   at Telerik.Windows.Documents.FormatProviders.Pdf.PdfDocumentExporter.ExportPage(SectionLayoutBox sectionBox, PdfContentsWriter writer) in c:\TB\221\WPF_Scrum\Release_WPF\Sources\Development\Documents\FormatProviders\Pdf\PdfDocumentExporter.cs:line 530
   at Telerik.Windows.Documents.FormatProviders.Pdf.PdfDocumentExporter.<>c__DisplayClass1.<Export>b__0(PdfContentsWriter contentsWriter) in c:\TB\221\WPF_Scrum\Release_WPF\Sources\Development\Documents\FormatProviders\Pdf\PdfDocumentExporter.cs:line 135
   at Telerik.Windows.Documents.FormatProviders.Pdf.RadPdf.PdfContents.WriteContents(PdfWriter writer) in c:\TB\221\WPF_Scrum\Release_WPF\Sources\Development\Documents\FormatProviders\Pdf\RadPdf\PdfContents.cs:line 25
   at Telerik.Windows.Documents.FormatProviders.Pdf.RadPdf.PdfStream.WriteToCore(PdfWriter writer) in c:\TB\221\WPF_Scrum\Release_WPF\Sources\Development\Documents\FormatProviders\Pdf\RadPdf\PdfStream.cs:line 57
   at Telerik.Windows.Documents.FormatProviders.Pdf.RadPdf.PdfObject.WriteTo(PdfWriter writer) in c:\TB\221\WPF_Scrum\Release_WPF\Sources\Development\Documents\FormatProviders\Pdf\RadPdf\PdfObject.cs:line 42
   at Telerik.Windows.Documents.FormatProviders.Pdf.RadPdf.PdfWriter.WritePdfObject(PdfObject obj) in c:\TB\221\WPF_Scrum\Release_WPF\Sources\Development\Documents\FormatProviders\Pdf\RadPdf\PdfWriter.cs:line 56
   at Telerik.Windows.Documents.FormatProviders.Pdf.PdfDocumentExporter.Export() in c:\TB\221\WPF_Scrum\Release_WPF\Sources\Development\Documents\FormatProviders\Pdf\PdfDocumentExporter.cs:line 147
   at Telerik.Windows.Documents.FormatProviders.Pdf.PdfFormatProvider.Export(RadDocument document, Stream output) in c:\TB\221\WPF_Scrum\Release_WPF\Sources\Development\Documents\FormatProviders\Pdf\PdfFormatProvider.cs:line 62
   at Telerik.Windows.Documents.FormatProviders.DocumentFormatProviderBase.Export(RadDocument document) in c:\TB\221\WPF_Scrum\Release_WPF\Sources\Development\Documents\Core\FormatProviders\DocumentFormatProviderBase.cs:line 47

same exception of Index was outside the bounds of the array for both overload methods of PdfFormatProvider export.
Need help!!!
0
ajin prasad
Top achievements
Rank 1
answered on 12 Sep 2013, 05:25 PM
Hi.

Fixed the issue as per your suggestion. the following line helped to resolve the issue

documentEditor.InsertSectionBreak();
documentEditor.InsertFragment(new DocumentFragment(theContent));

Thanks for the help
0
Vishal
Top achievements
Rank 1
answered on 13 Sep 2013, 02:52 AM
When this fix will be available?
Is it available for WPF also?
0
Petya
Telerik team
answered on 13 Sep 2013, 12:43 PM
Hello,

@ajin prasad I am glad to hear the issue was resolved. Do not hesitate to get back to us if you have other questions or comments.

@Vishal We are unaware of any issues in PdfFormaProvider. The problem ajin prasad was experiencing is most likely related to the way the document was created rather that the export itself.

That said we would appreciate if you open a support ticket and send us a runnable example demonstrating the issue you are observing.

We are looking forward to hearing from you.

Regards,
Petya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
RichTextBox
Asked by
ajin prasad
Top achievements
Rank 1
Answers by
Vishal
Top achievements
Rank 1
Petya
Telerik team
ajin prasad
Top achievements
Rank 1
Share this question
or