I'm facing a somehow strange issue. I'm adding some information to the left border of a page. These include the current page and the total page number.
Everything works fine when saving the document as a Word file but if I save the document as a pdf the total page number is missing:
If I open the saved Word document and save it to PDF from Office it works fine. But when I open this saved Word document with Spire.Doc and save it to PDF it the total page number is missing as well as the preceding '/', which was there when I saved the document directly to pdf without the intermediate Word document.
I used the latest hotfix 8.1.10.4046.
The following code snippets use C#.
I'm adding the current page and total field information with this code:
- Code: Select all
// Current Page field
var currentPage = content.AppendField(Guid.NewGuid().ToString(), Spire.Doc.FieldType.FieldNumPages);
currentPage.Code = @"PAGE \\* Arabic \\* MERGEFORMAT";
currentPage.ApplyCharacterFormat(characterFormat);
content.AppendFieldMark(FieldMarkType.FieldSeparator).ApplyCharacterFormat(characterFormat);
// Uncommented because the total page is not displayed if the document is saved to pdf.
content.AppendText($"{identifierLabel.OfLabel}").ApplyCharacterFormat(characterFormat);
// Total Page field
var totalPages = content.AppendField(Guid.NewGuid().ToString(), Spire.Doc.FieldType.FieldNumPages);
totalPages.Code = @"NUMPAGES \\* Arabic \\* MERGEFORMAT";
totalPages.ApplyCharacterFormat(characterFormat);
content.AppendFieldMark(FieldMarkType.FieldEnd).ApplyCharacterFormat(characterFormat);
And save the document to word or pdf with the following code.
- Code: Select all
protected void CommitChanges(bool convertToPdf)
{
WordDocument.UpdateTableOfContents();
WordDocument.AcceptChanges();
WordDocument.GetPageCount();
WordDocument.IsUpdateFields = true;
_workingStream = new MemoryStream();
if (convertToPdf)
{
WordDocument.SaveToStream(_workingStream, new ToPdfParameterList()
{
IsEmbeddedAllFonts = true,
PdfConformanceLevel = Spire.Pdf.PdfConformanceLevel.Pdf_A1B,
});
}
else
{
WordDocument.SaveToStream(_workingStream, WordDocument.DetectedFormatType);
}
_workingStream.Seek(0, SeekOrigin.Begin);
}
public string SaveAsTempFile(bool convertToPdf)
{
CommitChanges(convertToPdf);
string tempDocumentFilePath;
tempDocumentFilePath = Path.GetTempFileName().Replace(".tmp", convertToPdf ? ".pdf" : ".docx");
using (var fileStream = System.IO.File.Create(tempDocumentFilePath))
{
_workingStream.Seek(0, SeekOrigin.Begin);
_workingStream.CopyTo(fileStream);
}
return tempDocumentFilePath;
}
Should I call some other method in the "CommitChanges" or set an additional parameter for ToPdfParameterList or is this a minor issue within Spire.Doc?