Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed May 21, 2014 11:14 am

Hello,

I am trying to print a report where I have dynamically add a footer containing the field "FieldTYpe.FieldNumPages". I've found that the value is not populated/rendered during the print. However, if I save the file to disk rather than printing the number of pages is rendered correctly.

I am assuming this is occurring because the document that I've added the footer too does not know the number of pages when I try to print. Is there a way to allow this too occur without me having to save the document to disk then reloading into the Spire component?

Shaun

Code: Select all

 private void renderPrint(Document doc, PatientReportDetails patientDetails)
        {
            try
            {
                int i = doc.Sections.Count - 1;
                Section lastSection = doc.Sections[0];

                //Add Footer
                HeaderFooter footer = lastSection.HeadersFooters.Footer;
                Paragraph FParagraph = footer.AddParagraph();

                 //insert page number
                TextRange FormattedNHS = FParagraph.AppendText(patientDetails.getFormattedNHS() + "\t");
                TextRange FormattedMRN = FParagraph.AppendText(patientDetails.getFormattedMRN() + "\t");
                TextRange FormattedPatientName = FParagraph.AppendText(patientDetails.getFormattedPatientName() + "\t");
                TextRange FormattedDOB = FParagraph.AppendText(patientDetails.getFormattedDOB() + '\n');


                TextRange FPageInfo = FParagraph.AppendText("Page: ");
                TextRange FPageInfo1 = FParagraph.AppendField("page number", FieldType.FieldPage);
                TextRange FPageInfo2 = FParagraph.AppendText(" of ");   
             
                TextRange FPageInfo3 = FParagraph.AppendField("number of pages", FieldType.FieldNumPages);
                TextRange FPageInfo4 = FParagraph.AppendText("\t Printed " );
                TextRange FPrintedDetails = FParagraph.AppendText(DateTime.Now.ToLocalTime().ToString());

                FPageInfo.CharacterFormat.FontName = "Calibri";
                FPageInfo.CharacterFormat.FontSize = 8;
                FPageInfo.CharacterFormat.Bold = true;
                FPageInfo.CharacterFormat.TextColor = System.Drawing.Color.Black;
   
                FPageInfo1.CharacterFormat.FontName = "Calibri";
                FPageInfo1.CharacterFormat.FontSize = 8;
                FPageInfo1.CharacterFormat.Bold = true;
                FPageInfo1.CharacterFormat.TextColor = System.Drawing.Color.Black;
   
                FPageInfo2.CharacterFormat.FontName = "Calibri";
                FPageInfo2.CharacterFormat.FontSize = 8;
                FPageInfo2.CharacterFormat.Bold = true;
                FPageInfo2.CharacterFormat.TextColor = System.Drawing.Color.Black;
           
                FPageInfo3.CharacterFormat.FontName = "Calibri";
                FPageInfo3.CharacterFormat.FontSize = 8;
                FPageInfo3.CharacterFormat.Bold = true;
                FPageInfo3.CharacterFormat.TextColor = System.Drawing.Color.Black;

                FPageInfo4.CharacterFormat.FontName = "Calibri";
                FPageInfo4.CharacterFormat.FontSize = 8;
                FPageInfo4.CharacterFormat.Bold = true;
                FPageInfo4.CharacterFormat.TextColor = System.Drawing.Color.Black;

                FormattedNHS.CharacterFormat.FontName = "Calibri";
                FormattedNHS.CharacterFormat.FontSize = 8;
                FormattedNHS.CharacterFormat.Bold = true;
                FormattedNHS.CharacterFormat.TextColor = System.Drawing.Color.Black;

                FormattedPatientName.CharacterFormat.FontName = "Calibri";
                FormattedPatientName.CharacterFormat.FontSize = 8;
                FormattedPatientName.CharacterFormat.Bold = true;
                FormattedPatientName.CharacterFormat.TextColor = System.Drawing.Color.Black;

                FormattedMRN.CharacterFormat.FontName = "Calibri";
                FormattedMRN.CharacterFormat.FontSize = 8;
                FormattedMRN.CharacterFormat.Bold = true;
                FormattedMRN.CharacterFormat.TextColor = System.Drawing.Color.Black;

                FormattedDOB.CharacterFormat.FontName = "Calibri";
                FormattedDOB.CharacterFormat.FontSize = 8;
                FormattedDOB.CharacterFormat.Bold = true;
                FormattedDOB.CharacterFormat.TextColor = System.Drawing.Color.Black;

                //Set Footer Paragrah Format
                FParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
                FParagraph.Format.Borders.BorderType = Spire.Doc.Documents.BorderStyle.Thick;
                FParagraph.Format.Borders.Top.Space = 0.15f;
                FParagraph.Format.Borders.Color = System.Drawing.Color.Black;
                FParagraph.Format.Borders.Space = 4.0f;
                using (PrintDialog dialog = new PrintDialog())
                {         
                    doc.PrintDialog = dialog;
                    PrintDocument printDoc = doc.PrintDocument;
                    printDoc.PrintController = new StandardPrintController();
                    printDoc.Print();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {

            }
        }

shaun
 
Posts: 16
Joined: Fri Mar 14, 2014 2:58 pm

Thu May 22, 2014 3:34 am

Hello,

Thanks for your feedback.
There is a following method for your reference, and with table, you can set the position of text.
Code: Select all
private static void renderPrint(Document doc, PatientReportFooter footerInfo)
        {
            try
            {
                int i = doc.Sections.Count - 1;
                Section lastSection = doc.Sections[i];

                //Add Footer
                HeaderFooter footer = lastSection.HeadersFooters.Footer;
                Table table = footer.AddTable(true);
                table.ResetCells(3, 2);

                Paragraph FParagraph = table.Rows[0].Cells[0].AddParagraph();
                //FParagraph.Format.LeftIndent = 10;
                TextRange FormattedNHS = FParagraph.AppendText("NHS No:");
                TextRange FNHSDetails = FParagraph.AppendText(footerInfo.getFormattedNHS());

                FParagraph = table.Rows[0].Cells[1].AddParagraph();
                TextRange FormattedPatientName = FParagraph.AppendText("Patient Name: ");
                TextRange FPatientDetails = FParagraph.AppendText(footerInfo.getFormattedPatientName());

                FParagraph = table.Rows[1].Cells[0].AddParagraph();
                TextRange FPageInfo = FParagraph.AppendText("Page: ");
                TextRange FPageInfo1 = FParagraph.AppendField("page number", FieldType.FieldPage);
                TextRange FPageInfo2 = FParagraph.AppendText(" of ");
                TextRange FPageInfo3 = FParagraph.AppendField("number of pages", FieldType.FieldNumPages);

                FParagraph = table.Rows[1].Cells[1].AddParagraph();
                TextRange FormattedMRN = FParagraph.AppendText("MRN: ");
                TextRange FMRNDetails = FParagraph.AppendText
                    (footerInfo.getFormattedMRN());

                FParagraph = table.Rows[2].Cells[0].AddParagraph();
                TextRange FPageInfo4 = FParagraph.AppendText("Printed ");
                TextRange FPrintedDetails = FParagraph.AppendText(DateTime.Now.ToLocalTime().ToString());

                FParagraph = table.Rows[2].Cells[1].AddParagraph();
                TextRange FormattedDOB = FParagraph.AppendText("DOB: ");
                TextRange FDOBDetails = FParagraph.AppendText
                    (footerInfo.getFormattedDOB());

                FPageInfo.CharacterFormat.FontName = "Calibri";
                FPageInfo.CharacterFormat.FontSize = 8;
                FPageInfo.CharacterFormat.Bold = true;
                FPageInfo.CharacterFormat.TextColor = System.Drawing.Color.Black;


                FPageInfo4.CharacterFormat.FontName = "Calibri";
                FPageInfo4.CharacterFormat.FontSize = 8;
                FPageInfo4.CharacterFormat.Bold = true;
                FPageInfo4.CharacterFormat.TextColor = System.Drawing.Color.Black;

                FormattedNHS.CharacterFormat.FontName = "Calibri";
                FormattedNHS.CharacterFormat.FontSize = 8;
                FormattedNHS.CharacterFormat.Bold = true;
                FormattedNHS.CharacterFormat.TextColor = System.Drawing.Color.Black;

                FormattedPatientName.CharacterFormat.FontName = "Calibri";
                FormattedPatientName.CharacterFormat.FontSize = 8;
                FormattedPatientName.CharacterFormat.Bold = true;
                FormattedPatientName.CharacterFormat.TextColor = System.Drawing.Color.Black;

                FormattedMRN.CharacterFormat.FontName = "Calibri";
                FormattedMRN.CharacterFormat.FontSize = 8;
                FormattedMRN.CharacterFormat.Bold = true;
                FormattedMRN.CharacterFormat.TextColor = System.Drawing.Color.Black;

                FormattedDOB.CharacterFormat.FontName = "Calibri";
                FormattedDOB.CharacterFormat.FontSize = 8;
                FormattedDOB.CharacterFormat.Bold = true;
                FormattedDOB.CharacterFormat.TextColor = System.Drawing.Color.Black;

                using (PrintDialog dialog = new PrintDialog())
                {
                    doc.PrintDialog = dialog;
                    PrintDocument printDoc = doc.PrintDocument;
                    printDoc.PrintController = new StandardPrintController();
                    printDoc.Print();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {

            }
        }

If there are any questions, welcome to get it back to us.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Return to Spire.Doc