Word 转 PDF 文字换行错误

您好,

我有付费购买 NET - Aspose.Words,
我将资料汇出成 word 档,显示是正常的,
但转成 PDF 档 ,却发现显示与 word 画面不一样,
文字会在奇怪的地方换行,
如下图的蓝色箭头处

以上问题再麻烦您协助,谢谢。

附上汇出的原始档案
PDF.pdf (89.7 KB)
WORD.docx (18.3 KB)

原始码:
WordToPdfSample.zip (1.9 MB)

public async Task<IActionResult> Word(string reviewId)
{
    var doc = await GetReportDocAsync();

    MemoryStream stream = new MemoryStream();
    doc.Save(stream, SaveFormat.Docx);

    var dataTime = DateTime.Now.ToString("yyyyMMddhhmmss");

    return File(stream.ToArray(), "application/vnd.ms-word", string.Format($"{dataTime}.docx"));
}

public async Task<IActionResult> Pdf(string reviewId)
{
    var doc = await GetReportDocAsync();

    MemoryStream stream = new MemoryStream();
    doc.Save(stream, SaveFormat.Pdf);

    var dataTime = DateTime.Now.ToString("yyyyMMddhhmmss");

    return File(stream.ToArray(), "application/vnd.ms-word", string.Format($"{dataTime}.pdf"));
}

private async Task<Document> GetReportDocAsync()
{
    var docA = new Document();
    var builder = new DocumentBuilder(docA);

    var font = builder.Font;

    font.Name = "標楷體";
    font.Size = 12;
    builder.Bold = true;

    builder.PageSetup.PaperSize = PaperSize.A4;
    builder.MoveToDocumentStart();
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

    builder.Font.Size = 16;

    builder.Writeln("Demo");

    builder.StartTable();

    builder.InsertCell();
    builder.CellFormat.Width = 100;
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.Write("title");

    builder.CellFormat.Shading.ClearFormatting();
    builder.InsertCell();
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.Write("content");
    builder.CellFormat.Width = 400;
    builder.Bold = false;
    builder.EndRow();

    builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;

    builder.InsertCell();
    builder.CellFormat.Width = 100;
    builder.Write("發表會直播");

    builder.CellFormat.Shading.ClearFormatting();
    builder.InsertCell();
    builder.Write("Apple 宣布在台灣時間 9 月 13 日凌晨 1 點舉行蘋果秋季發表會,於表1-外國測試名單清冊中,今年 iPhone 15 發表會活動同樣也採用線上形式舉行,僅在體驗階段開放給現場試用。本篇瘋先生就替大家整理 5 個最佳觀看 2023 蘋果秋季發表會直播/轉播方法,如果想要看到原汁原味的蘋果秋季發表會直播畫面,趕緊透過這篇了解。");
    builder.CellFormat.Width = 400;
    builder.Bold = false;
    builder.EndRow();


    builder.CellFormat.Borders[BorderType.Right].LineStyle = LineStyle.None;
    builder.CellFormat.Borders[BorderType.Left].LineStyle = LineStyle.None;
    builder.CellFormat.Borders[BorderType.Bottom].LineStyle = LineStyle.None;

    builder.InsertCell();
    builder.CellFormat.Width = 100;
    builder.CellFormat.HorizontalMerge = CellMerge.First;

    builder.InsertCell();
    builder.CellFormat.Width = 400;
    builder.CellFormat.HorizontalMerge = CellMerge.Previous;

    builder.EndRow();

    builder.EndTable();

    builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.InsertField("PAGE", null);

    return docA;
}

@vitawang MS Word 中的文档布局与 Aspose.Words 中的文档布局相同,取决于默认编辑语言。 在您的情况下,应将其设置为中文才能获得正确的渲染结果。 由于无法为从头开始创建的文档指定它,因此您应该像这样保存并重新加载文档:

// Save document as DOCX and reload it with EditingLanguage.ChinesePRC
using (MemoryStream tmp = new MemoryStream())
{
    docA.Save(tmp, SaveFormat.Docx);
    tmp.Position = 0;

    LoadOptions opt = new LoadOptions();
    opt.LanguagePreferences.DefaultEditingLanguage = EditingLanguage.ChinesePRC;
    docA = new Document(tmp, opt);

    docA.Save(@"C:\Temp\out.pdf");
}

out.pdf (55.6 KB)

原來是這樣,修改後就可以顯示了,謝謝您的協助!

1 Like