Unable to generate PDF Preview.

Posted by: mayur.purandare on 26 May 2021, 1:42 am EST

  • Posted 26 May 2021, 1:42 am EST

    Hello,
    Facing issue in PDF Generation and PDF Preview while executing following code.

    C1PdfDocument c1PdfDocument = GetC1PDFDocument();
    C1PrintDocument c1PrintDocument = GetC1PrintDocument();
    RenderTable renderTable = GetRenderTable(); // some data is added (contains images)
    c1PrintDocument.Body.Children.Add(renderTable);
    c1PrintDocument.Generate();
    foreach (C1Page page in c1PrintDocument.Pages)
    {
    c1PdfDocument.NewPage();
    img = page.AsMetafile(); //Exception
    c1PdfDocument.DrawImage(img, c1PdfDocument.PageRectangle);
    }
    c1PdfDocument.Save(SavePath);


    Component used: C1.WPF.C1Report.4.dll [Version: 4.0.20203.457]

    Stacktrace is in the attachment.
  • Replied 26 May 2021, 1:49 am EST

    Adding the Stacktrace in the comments:
    ************************************************************** Exception Stack **************************************************************


    Parameter is not valid. at System.Drawing.Graphics.set_PixelOffsetMode(PixelOffsetMode value)
    at C1.C1Preview.GraphicsUtils.DrawImage(Graphics graphics, Image image, RectangleF drawRect, RectangleF clipRect, IImageAlign align, IMetafilePainter metafilePainter, Boolean useClassicDrawMethod)
    at #w3i.#VOj.Draw(Image #gjc, ImageAlign #wrc, Graphics #0p, Rectangle #Mi, Rectangle #TGj, Boolean #TOj, Boolean #qgc, Boolean #UOj)
    at C1.C1Preview.RenderGraphicsFragment.InternalDraw(Graphics graphics, DrawParams drawParams)
    at C1.C1Preview.RenderFragment.DrawContent(Graphics graphics, DrawParams dp)
    at C1.C1Preview.RenderFragment.#1Lj(Graphics #0p, DrawParams #Dr)
    at C1.C1Preview.RenderFragment.Draw(Graphics graphics, Rectangle boundsPx, Rectangle parentContentBoundsPx, Boolean renderChildren, PageDrawParams pdp)
    at C1.C1Preview.RenderTableFragment.#6Gj(Object #hEj, RenderFragment #qKf, Rectangle #7Gj, Rectangle #8Gj, Boolean #9Gj, Boolean #aHj, Boolean #bHj)
    at C1.C1Preview.RenderTableFragment.#eHj(#A4k #fHj, Object #hEj, #w4k #ZKf, Rectangle #gHj, Single #5lb, Single #6lb, Boolean #9Gj, Boolean #bHj)
    at C1.C1Preview.RenderTableFragment.#hHj(Single #5lb, Single #6lb, DrawParams #iHj, #x4k #jHj, #A4k #kHj, #z4k #lHj, #y4k #CGj, Object #mHj)
    at C1.C1Preview.RenderTableFragment.InternalDraw(Graphics graphics, DrawParams drawParams)
    at C1.C1Preview.RenderFragment.DrawContent(Graphics graphics, DrawParams dp)
    at C1.C1Preview.RenderFragment.#1Lj(Graphics #0p, DrawParams #Dr)
    at C1.C1Preview.RenderFragment.Draw(Graphics graphics, Rectangle boundsPx, Rectangle parentContentBoundsPx, Boolean renderChildren, PageDrawParams pdp)
    at C1.C1Preview.C1Page.Draw(Graphics graphics, PageDrawParams pdp)
    at C1.C1Preview.C1Page.#jKj(Graphics #jbj, PageDrawParams #S8i)
    at C1.C1Preview.C1Page.AsMetafile(Graphics referenceGraphics, EmfType emfType, Boolean whiten, Boolean renderInputControls, SmoothingMode smoothingMode)
    at C1.C1Preview.C1Page.AsMetafile(EmfType emfType, Boolean whiten)
    at C1.C1Preview.C1Page.AsMetafile()

    ************************************************************** Exception Stack **************************************************************
  • Marked as Answer

    Replied 30 May 2021, 6:39 pm EST

    Hi,
    I found, you are passing metafile as a parameter of DrawImage method. However, it takes as WriteableBitmap.
    Please check below link:
    http://helpcentral.componentone.com/nethelp/C1PDFWPF/C1.Silverlight.Pdf.5~C1.Silverlight.Pdf.C1PdfDocument~DrawImage(WriteableBitmap,Rect).html

    You can use below code for better understanding:
    private void Button_Click(object sender, RoutedEventArgs e)
    {
    C1PdfDocument c1PdfDocument = new C1PdfDocument(PaperKind.A4);
    C1PrintDocument c1PrintDocument1 = GetPrintDocumentWithTable();
    c1PrintDocument1.Generate();
    foreach (C1Page page in c1PrintDocument1.Pages)
    {
    if (page.Index > 0)
    {
    c1PdfDocument.NewPage();
    }
    Metafile img = page.AsMetafile(); //Exception
    Stream stream = new MemoryStream();
    img.Save(stream, ImageFormat.Bmp);
    var bitmap = new BitmapImage();
    bitmap.BeginInit();
    bitmap.StreamSource = stream;
    bitmap.EndInit();
    bitmap.Freeze();
    var wb = new WriteableBitmap(bitmap);
    c1PdfDocument.DrawImage(wb, c1PdfDocument.PageRectangle);
    }
    string filename = "test" + DateTime.Now.Ticks + ".pdf";
    c1PdfDocument.Save(filename);
    Process.Start(filename);
    }

    C1PrintDocument GetPrintDocumentWithTable()
    {
    C1PrintDocument c1PrintDocument1 = new C1PrintDocument();
    // Make a table.
    C1.C1Preview.RenderTable table = new C1.C1Preview.RenderTable(c1PrintDocument1);
    table.Style.GridLines.All = new C1.C1Preview.LineDef(Colors.DarkGray);
    // Add 3 rows.
    const int r = 3;

    // Add 3 columns.
    const int c = 3;

    for (int row = 0; row < r; ++row)
    {
    for (int col = 0; col < c; ++col)
    {
    if (!(row == 1 && col == 1) && !(row == 1 && col == 2))
    {
    C1.C1Preview.RenderText celltext = new C1.C1Preview.RenderText(c1PrintDocument1);
    celltext.Text = string.Format("Cell ({0}, {1})", row, col);

    // Add cells with text.
    table.Cells[row, col].RenderObject = celltext;
    }
    }
    }
    c1PrintDocument1.Body.Children.Add(table);
    return c1PrintDocument1;
    }



    Hope, this will solve your issue. If this does not solve your issue then please share a small sample application with us, so that we can assist you accordingly.

    Thanks,
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels