Word to Pdf misalignment problem

Hello,

I’m trying to convert a simple document but some text are misaligned (Aspose.Words.24.10.0).
Please see attached files.

Can you help?

TestDocx.docx (11,0 Ko)

TestPdf.pdf (37,2 Ko)

@dev1CHRH Unfortunately, I cannot reproduce the problem on my side. Here is the output produced on my side using the following simple code:

Document doc = new Document(@"C:\Temp\in.docx");
doc.Save(@"C:\Temp\out.pdf");

out.pdf (35.9 KB)

The problem might occurs because different version of Calibri font is used on your side.

You are right. The problem only occurs when saving to .PDF just after replacing text.

Document dw = new Document(@"C:\Temp\PeppolFactTemplate.docx");
dw.Range.Replace("£NameSup£", "My Supplier Company N.V.");
dw.Range.Replace("£AdrSup£", "De Grote Meir 22" + Environment.NewLine + "2000 ANTWERPEN");
dw.Range.Replace("£NameCust£", "My Customer Company S.A.");
dw.Range.Replace("£AdrCust£", "Boulevard  Sint Michel 53" + Environment.NewLine + "1000 BRUXELLES");
dw.Range.Replace("£DteFact£", "09/04/2018");
dw.Range.Replace("£DueDte£", "09/05/2018");
dw.Save(@"C:\Temp\out.pdf");

PeppolFactTemplate.docx (16,2 Ko)

@dev1CHRH The problem occurs because you are using Environment.NewLine, it is not normally used in MS Word documents. If you need a line break, you should use either ControlChar.LineBreak:

doc.Range.Replace("£AdrCust£", "Boulevard  Sint Michel 53" + ControlChar.LineBreak + "1000 BRUXELLES");

Or in case of using Range.Replace method, you can use "&l" metacharacter:

doc.Range.Replace("£AdrCust£", "Boulevard  Sint Michel 53" + "&l" + "1000 BRUXELLES");

Also, find/replace is not the most efficient way to fill the document with data. I would suggest you to consider using either Mail Merge or LINQ Reporting Engine.

Thank you again for your answer. But it is very strange that just saving/reloading the .docx with replaced texts resolves the problem.

Document dw = new Document(@"C:\Temp\PeppolFactTemplate.docx");
dw.Range.Replace("£NameSup£", "My Supplier Company N.V.");
dw.Range.Replace("£AdrSup£", "De Grote Meir 22" + Environment.NewLine + "2000 ANTWERPEN");
dw.Range.Replace("£NameCust£", "My Customer Company S.A.");
dw.Range.Replace("£AdrCust£", "Boulevard  Sint Michel 53" + Environment.NewLine + "1000 BRUXELLES");
dw.Range.Replace("£DteFact£", "09/04/2018");
dw.Range.Replace("£DueDte£", "09/05/2018");
dw.Save(@"C:\Temp\TestPdf.pdf");      // NOT WORKING

// Just save/reload new .docx and resave in .pdf
dw.Save(@"C:\Temp\TestPdf.docx");
Document dw2 = new Document(@"C:\Temp\TestPdf.docx");
dw2.Save(@"C:\Temp\TestPdf2.pdf");    // WORKING

@dev1CHRH It resolves the problem because upon reading DOCX back to Aspose.Words model Environment.NewLine is corrected and is not read as is. You cannot use \r\n in normal text in MS Word documents.

Thank you for all. When using ControlChar.ParagraphBreak instead of Environment.NewLine, it is working well. I’ll never use Environment.NewLine again :wink:

I’ll try Mail Merge or LINQ Reporting Engine.

Have a good day.

@dev1CHRH Using ControlChar.ParagraphBreak is also incorrect in your case. If you need a paragraph break you should use &p metacharacter when find/replace method is used.

OK, works even better. Thanks a lot.

1 Like