0

Im building an asp.net application and im using crystal report to generate pdf reports, my main issue was in the .rpt report that i need to change the existing picture with another URL path, i set it as fix path . So i reach to point where i changed the picture , but i faced a stopper which is i can't change the width of a new picture to be suitable and fit with all reports width

can you help me

i will be grateful for any suggestion

best regards

i try this code to replace the Picture ?

private void ReplaceHeaderAndFooter(ReportDocument report)
{
var clientDoc = report.ReportClientDocument.ReportDefController;

            const string HeaderPath = "Reports\\Images\\headerSIS.png";
            const string FooterPath = "Reports\\Images\\footerSIS.png";
            string newHeaderImagePath = Path.GetFullPath(HeaderPath);
            string newFooterImagePath = Path.GetFullPath(FooterPath);
    
            //Determine which section to add the picture field to 
            var headerSection = clientDoc.ReportDefinition.ReportHeaderArea.Sections[0];
          
    
            var allRptObjKindPicture = clientDoc.ReportObjectController.GetReportObjectsByKind(CrystalDecisions.ReportAppServer.ReportDefModel.CrReportObjectKindEnum.crReportObjectKindPicture);
    
            int oldWidth = 0;
            int oldHight = 0;
    
            if (allRptObjKindPicture.Count > 0)
            {
                if (headerSection.ReportObjects.Count > 0)
                {
                    string rptObjPictureHeaderName = String.Empty;
                    for (int i = 0; i < headerSection.ReportObjects.Count; i++)
                    {
                        if (headerSection.ReportObjects[i].Kind == CrystalDecisions.ReportAppServer.ReportDefModel.CrReportObjectKindEnum.crReportObjectKindPicture)
                        {
                            rptObjPictureHeaderName = headerSection.ReportObjects[i].Name;
                            oldWidth  = headerSection.ReportObjects[i].Width;
                            oldHight  = headerSection.ReportObjects[i].Height;
                        }
                    }
                    if (!String.IsNullOrEmpty(rptObjPictureHeaderName))
                    {
                        clientDoc.ReportObjectController.Remove(headerSection.ReportObjects[rptObjPictureHeaderName]);
                        clientDoc.ReportObjectController.ImportPicture(newHeaderImagePath, headerSection, 0, 0);
                    }
                }
    
                var allRptObjKindPictureAfrter = clientDoc.ReportObjectController.GetReportObjectsByKind(CrystalDecisions.ReportAppServer.ReportDefModel.CrReportObjectKindEnum.crReportObjectKindPicture);
                if (headerSection.ReportObjects.Count > 0)
                {
                    string rptObjPictureHeaderName = String.Empty;
                    for (int i = 0; i < headerSection.ReportObjects.Count; i++)
                    {
                        if (headerSection.ReportObjects[i].Kind == CrystalDecisions.ReportAppServer.ReportDefModel.CrReportObjectKindEnum.crReportObjectKindPicture)
                        {
                            CrystalDecisions.ReportAppServer.ReportDefModel.ISCRPictureObject    newPictureObj  = (CrystalDecisions.ReportAppServer.ReportDefModel.ISCRPictureObject)headerSection.ReportObjects[i];
                            newPictureObj.Width = w; 
                            newPictureObj.Height = h;
                        }
                    }
                }

I expect that the width of the new picture be equal to the old picture width and when i generate the report the header picture with the same old picture width .

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • You have a list like 3, 4, 5, 6. When you remove the second item the third item becomes the second. So with you pictures when you remove and item and then add the picture is going into the wrong location. The solution is to change the for loop to go from end to beginning. Use : for (int i = headerSection.ReportObjects.Count - 1; i >= 0; i--) – jdweng Jan 14 '23 at 10:46
  • actually, it removes the right picture and adds it into the right place , but the problem is the new picture have different width , so it can't be the same as the old picture . – Ahmad_Lutfi Jan 14 '23 at 11:29
  • Are you sure? After deleting, what is the header section number? : ImportPicture(newHeaderImagePath, headerSection, 0, 0); After you delete the third section you are adding the new picture after the original fourth section, not after the second which is what you want. – jdweng Jan 14 '23 at 12:27
  • yes im sure that the position of the picture is the same one which i deleted – Ahmad_Lutfi Jan 14 '23 at 17:01
  • 1
    You do not understand what I'm saying. If you have indexes 0,1,2. You delete index zero. You are now adding the deleted picture after section 1 (what was index 1 is now index 0) where the delete picture was before index 1. – jdweng Jan 14 '23 at 19:38

0 Answers0