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 Jan 29, 2020 7:28 am

Hi,

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:
MissingTotalPage_PDF.png


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.
MissingTotalPage_intermediateWord_PDF.png


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?

Dkrueger
 
Posts: 2
Joined: Mon May 14, 2018 1:35 pm

Wed Jan 29, 2020 8:32 am

Hello,

Thanks for your post.
I simulated and tested your case, but didn't reproduce your issue. To help us further investigate it, please share us with your full testing code as well as your input file (if any). Or you can provide a runnable application which shows your problem. You also can send them to us via email (support@e-iceblue.com). Thanks in advance.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Wed Jan 29, 2020 9:13 am

Hello,

thank you very much for the fast reply.

I send you a mail with the C# project.

Best regards,
Daniel

Dkrueger
 
Posts: 2
Joined: Mon May 14, 2018 1:35 pm

Wed Jan 29, 2020 12:10 pm

Hello,

Ok, I am looking forward to your further reply.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc