When reading in a rtf file that's in Letter format the Spire.Doc component treats it as A4. The consequence of this is that some of the border text is missing from the printed report. The file we are reading in is called fail_rtf.rtf
I have tried changing the format of the spire document to Letter but this only affects the saved document and not the in memory document.
The code I have included reads in the fail_rtf.rtf file (Letter format), prints the file using spire.Doc and saves the file using spire.Doc as c:\printed_doc.rtf . If you now look at the printed document, the border is missing. Opening the file printed_doc.rtf in word and looking at the pagelayout you will see the format as A4 and the printpreview removes the border as with the printed document.
I tried to address this issue by forcing the Spire.Doc to treat the fail_rtf.rtf as Letter format using
lastSection.PageSetup.PageSize = PageSize.Letter;
The printed document is still being treated as A4 with the border missing but the saved file printed_asletter.rtf has been saved in letter format and the print preview in word is correct with no border missing.
If it was the case that all are documents were letter, I would code around this issue. However, this is not the case and is a issue.
Please see the test code below. It reads in the test file and prints normally in memory and saves the file. It then forces the document in memory to be letter format then saves it.
- Code: Select all
private void runTest()
{
string tempfilename = @"c:\fail_rtf.rtf";
string text = System.IO.File.ReadAllText(tempfilename, Encoding.GetEncoding(1252));
try
{
using (Document doc = new Document(tempfilename, FileFormat.Rtf))
{
renderPrint(doc, new PatientReportDetails("nhsnumber","mrn", "dob", "shouldbeletter", "forename", "gppractice" , 12345));
}
using (Document doc = new Document(tempfilename, FileFormat.Rtf))
{
renderPrintForceLetter(doc, new PatientReportDetails("nhsnumber", "mrn", "dob", "force letter", "forename", "gppractice", 12345));
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
// System.IO.File.Delete(tempfilename);
}
}
private void renderPrint(Document doc, PatientReportDetails patientDetails)
{
try
{
int i = doc.Sections.Count - 1;
Section lastSection = doc.Sections[0];
lastSection.PageSetup.FooterDistance = 10;
//Add Footer
HeaderFooter footer = lastSection.HeadersFooters.Footer;
Table table = footer.AddTable(true);
table.ResetCells(2, 1);
Paragraph FParagraph = table.Rows[0].Cells[0].AddParagraph();
FParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
FParagraph.Format.Borders.BorderType = Spire.Doc.Documents.BorderStyle.None;
FParagraph.Format.Borders.Top.Space = 0.15f;
FParagraph.Format.Borders.Color = System.Drawing.Color.Black;
FParagraph.Format.Borders.Space = 4.0f;
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());
//insert page number
FParagraph = table.Rows[1].Cells[0].AddParagraph();
TextRange FormattedUniqueId = FParagraph.AppendText(patientDetails.getFormattedUniqueID() + "\t");
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;
FPrintedDetails.CharacterFormat.FontName = "Calibri";
FPrintedDetails.CharacterFormat.FontSize = 8;
FPrintedDetails.CharacterFormat.Bold = true;
FPrintedDetails.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;
FormattedUniqueId.CharacterFormat.FontName = "Calibri";
FormattedUniqueId.CharacterFormat.FontSize = 8;
FormattedUniqueId.CharacterFormat.Bold = true;
FormattedUniqueId.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.None;
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();
}
doc.SaveToFile(@"c:\printed_doc.rtf");
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}
private void renderPrintForceLetter(Document doc, PatientReportDetails patientDetails)
{
try
{
int i = doc.Sections.Count - 1;
Section lastSection = doc.Sections[0];
lastSection.PageSetup.FooterDistance = 10;
//Add Footer
HeaderFooter footer = lastSection.HeadersFooters.Footer;
Table table = footer.AddTable(true);
table.ResetCells(2, 1);
Paragraph FParagraph = table.Rows[0].Cells[0].AddParagraph();
FParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
FParagraph.Format.Borders.BorderType = Spire.Doc.Documents.BorderStyle.None;
FParagraph.Format.Borders.Top.Space = 0.15f;
FParagraph.Format.Borders.Color = System.Drawing.Color.Black;
FParagraph.Format.Borders.Space = 4.0f;
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());
//insert page number
FParagraph = table.Rows[1].Cells[0].AddParagraph();
TextRange FormattedUniqueId = FParagraph.AppendText(patientDetails.getFormattedUniqueID() + "\t");
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;
FPrintedDetails.CharacterFormat.FontName = "Calibri";
FPrintedDetails.CharacterFormat.FontSize = 8;
FPrintedDetails.CharacterFormat.Bold = true;
FPrintedDetails.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;
FormattedUniqueId.CharacterFormat.FontName = "Calibri";
FormattedUniqueId.CharacterFormat.FontSize = 8;
FormattedUniqueId.CharacterFormat.Bold = true;
FormattedUniqueId.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.None;
FParagraph.Format.Borders.Top.Space = 0.15f;
FParagraph.Format.Borders.Color = System.Drawing.Color.Black;
FParagraph.Format.Borders.Space = 4.0f;
lastSection.PageSetup.PageSize = PageSize.Letter;
using (PrintDialog dialog = new PrintDialog())
{
doc.PrintDialog = dialog;
PrintDocument printDoc = doc.PrintDocument;
printDoc.PrintController = new StandardPrintController();
printDoc.Print();
}
doc.SaveToFile(@"c:\printed_asletter.rtf");
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}