Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Help 

C# + Crystal report - PDF Exporter code VS from visual studio preview

The PDF File output is different look (style, font, spacing, etc) when exported from C# code VS from visual studio preview tab
the code commonly and widely use as follow:

Code
ReportDocument cryRpt = new ReportDocument();
...
...
etc

is there any way to export crystal report into PDF but keep everything same as like exporting from preview tab visual studio?
(Apr 12, 2019 08:15 PM)n31ght0 Wrote:  The PDF File output is different look (style, font, spacing, etc) when exported from C# code VS from visual studio preview tab
the code commonly and widely use as follow:

Code
ReportDocument cryRpt = new ReportDocument();
...
...
etc

is there any way to export crystal report into PDF but keep everything same as like exporting from preview tab visual studio?

Of course there is a way for that.
Code
YourCrystalReport report = new YourCrystalReport();
report.SetDataSource(query);
report.ExportToStream(ExportFormatType.PortableDocFormat);
report.SaveAs("reportname.pdf");

or if you want to send a mail with report
Code
mail.Attachments.Add(new Attachment(report.ExportToStream(ExportFormatType.PortableDocFormat), "reportname.pdf"));
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
(Apr 13, 2019 01:36 AM)itcorp Wrote:  Of course there is a way for that.
Code
YourCrystalReport report = new YourCrystalReport();
report.SetDataSource(query);
report.ExportToStream(ExportFormatType.PortableDocFormat);
report.SaveAs("reportname.pdf");

or if you want to send a mail with report
Code
mail.Attachments.Add(new Attachment(report.ExportToStream(ExportFormatType.PortableDocFormat), "reportname.pdf"));

i do but it doesn't work for me, the exported PDF file still giving different looks

[Image: OMBzagL.png]

i try to googling some say because of windows block font access for security reason. is that true?
actually is it possible achieve this results? i'm really dizzy why it looks fine and good when i exported from visual studio but different from code 😭
(Apr 13, 2019 07:12 PM)n31ght0 Wrote:  i do but it doesn't work for me, the exported PDF file still giving different looks

[Image: OMBzagL.png]

i try to googling some say because of windows block font access for security reason. is that true?
actually is it possible achieve this results? i'm really dizzy why it looks fine and good when i exported from visual studio but different from code  😭

It's possible to achieve the result as I told you earlier,the previous example I gave you I personally use in my desktop apps. But I made a very simple example for you in web forms witch works perfectly in visual studio or hosted in local iis.
All the best.
Exported example of pdf

Source code of example


Code
public partial class Default : System.Web.UI.Page
    {
        public class User
        {
            public string Username { get; set; }
            public int Popularity { get; set; }
        }
        private List<User> usersCollections;
        private ReportDocument document;
        private Reports.HFReport report;
        protected void Page_Load(object sender, EventArgs e)
        {
            usersCollections = new List<User>()
                {
                    new User(){ Username="n31ght0",Popularity=6},
                    new User(){Username="itcorp",Popularity=6}
                };

        }

        protected void btnExportOneMethod_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            Response.ClearHeaders();

            report = new Reports.HFReport();

            HFDataSet dt = new HFDataSet();
            DataRow row;

            if (usersCollections != null)
            {
                foreach (var user in usersCollections)
                {
                    Console.WriteLine(user.Username);
                    row = dt.HFUsers.NewRow();
                    row["Username"] = user.Username;
                    row["Popularity"] = user.Popularity;
                    dt.HFUsers.Rows.Add(row);
                }

                report.SetDataSource(dt);
                report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "myHFReportMethodOne");
                report.Dispose();
            }
        }

        protected void btnExportSecondMethod_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            Response.ClearHeaders();

            document = new ReportDocument();

            HFDataSet dt = new HFDataSet();
            DataRow row;

            if (usersCollections != null)
            {
                foreach (var user in usersCollections)
                {
                    Console.WriteLine(user.Username);
                    row = dt.HFUsers.NewRow();
                    row["Username"] = user.Username;
                    row["Popularity"] = user.Popularity;
                    dt.HFUsers.Rows.Add(row);
                }

                document.Load(Server.MapPath("~/Reports/HFReport.rpt"));
                document.SetDataSource(dt);
                document.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "myHFReportSecondMethod");
                document.Dispose();
            }
        }
    }
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
Hi Man, Thank you so much! Very appreciate for your help.
i just try to change rpt report on your project. i add stylish text (which is trajan pro font) and it's fail to load with the following error message.
[Image: RpNU2Su.png]

here's the updated rpt file
http://www.mediafire.com/file/i35bqgar1r...t.zip/file


(Apr 14, 2019 06:36 AM)itcorp Wrote:  It's possible to achieve the result as I told you earlier,the previous example I gave you I personally use in my desktop apps. But I made a very simple example for you in web forms witch works perfectly in visual studio or hosted in local iis.
All the best.
Exported example of pdf

Source code of example


Code
public partial class Default : System.Web.UI.Page
   {
       public class User
       {
           public string Username { get; set; }
           public int Popularity { get; set; }
       }
       private List<User> usersCollections;
       private ReportDocument document;
       private Reports.HFReport report;
       protected void Page_Load(object sender, EventArgs e)
       {
           usersCollections = new List<User>()
               {
                   new User(){ Username="n31ght0",Popularity=6},
                   new User(){Username="itcorp",Popularity=6}
               };

       }

       protected void btnExportOneMethod_Click(object sender, EventArgs e)
       {
           Response.ClearContent();
           Response.ClearHeaders();

           report = new Reports.HFReport();

           HFDataSet dt = new HFDataSet();
           DataRow row;

           if (usersCollections != null)
           {
               foreach (var user in usersCollections)
               {
                   Console.WriteLine(user.Username);
                   row = dt.HFUsers.NewRow();
                   row["Username"] = user.Username;
                   row["Popularity"] = user.Popularity;
                   dt.HFUsers.Rows.Add(row);
               }

               report.SetDataSource(dt);
               report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "myHFReportMethodOne");
               report.Dispose();
           }
       }

       protected void btnExportSecondMethod_Click(object sender, EventArgs e)
       {
           Response.ClearContent();
           Response.ClearHeaders();

           document = new ReportDocument();

           HFDataSet dt = new HFDataSet();
           DataRow row;

           if (usersCollections != null)
           {
               foreach (var user in usersCollections)
               {
                   Console.WriteLine(user.Username);
                   row = dt.HFUsers.NewRow();
                   row["Username"] = user.Username;
                   row["Popularity"] = user.Popularity;
                   dt.HFUsers.Rows.Add(row);
               }

               document.Load(Server.MapPath("~/Reports/HFReport.rpt"));
               document.SetDataSource(dt);
               document.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "myHFReportSecondMethod");
               document.Dispose();
           }
       }
   }
(Apr 18, 2019 08:03 PM)n31ght0 Wrote:  Hi Man, Thank you so much! Very appreciate for your help.
i just try to change rpt report on your project. i add stylish text (which is trajan pro font) and it's fail to load with the following error message.
[Image: RpNU2Su.png]

here's the updated rpt file
http://www.mediafire.com/file/i35bqgar1r...t.zip/file

I think the problem that you have is tied to a font you do not have TrueType (.ttf) font and you probably only have .otf and you need to have both versions.You will solve the problem by downloading it .ttf version of Trajan Pro.

https://www.wfonts.com/font/trajan-pro

All the best.
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58


Possibly Related Threads...
Thread / Author Replies Views Last Post
by code_g33kMar 5, 2026 08:31 PM 6 210 Mar 5, 2026 08:31 PM
Last Post: sudowoodoking
by NightmareFeb 6, 2024 03:38 PM 9 329 Feb 6, 2024 03:38 PM
Last Post: Ph4n70M Phr34K
[Source] by sourceJul 15, 2023 10:25 PM 4 208 Jul 15, 2023 10:25 PM
Last Post: Nightmare