aspose-words / Aspose.Words-for-.NET

Aspose.Words for .NET examples, plugins and showcases
https://products.aspose.com/words/net
MIT License
503 stars 187 forks source link

插入水印会在文档页眉结尾处添加换行,导致文档内容错乱 #292

Closed fengli0 closed 3 years ago

fengli0 commented 3 years ago

插入水印会在文档页眉结尾处添加换行,导致文档内容错位,请问有好的解决方案吗?

image 12 专业工程分包合同(35616家合桩基).docx

InsertWatermarkCode.txt 水印图片

AlexNosk commented 3 years ago

@fengli0 请附上您的文件和用于插入水印的代码。 我们将检查并提供更多信息。

fengli0 commented 3 years ago

@fengli0 请附上您的文件和用于插入水印的代码。 我们将检查并提供更多信息。

你好,我已经上传了文件和插入水印的代码,麻烦帮忙看看,谢谢

AlexNosk commented 3 years ago

@fengli0 感谢您提供其他信息。 如我所见,您的文档已经包含水印。 您是否需要两个水印? 另外,在代码中,您可以通过循环所有标头来手动插入水印。 在最新版本的Aspose.Words中,有一个用于插入水印的内置方法。 https://docs.aspose.com/words/net/working-with-watermark/ 但是在这种情况下,Aspose.Words将删除旧的水印并插入新的水印。 如果同时需要两个水印,则可以使用您的方法,但是将水印图像插入页眉中,而不是插入页脚中。

fengli0 commented 3 years ago

@AlexNosk 我们现在的需求就是需要可以插入多个水印。您说的“但是将水印图像插入页眉中,而不是插入页脚中。”是下图所示的代码吗? 134 我是参考你们提供的Dome 实现的 image 。我对aspose不了解,您能详细说下怎么插入页眉中吗?

AlexNosk commented 3 years ago

@fengli0 我修改了您的代码,以避免插入多余的段落。 希望这可以帮助。

    Private Sub InsertWatermarkImageToWord(ByVal doc As Aspose.Words.Document, ByVal strImgPath As String, ByVal ContractCode As String)
        doc.AcceptAllRevisions()
        Dim bookmark As Aspose.Words.Bookmark = doc.Range.Bookmarks("合同编号")
        If Not bookmark Is Nothing Then
            Dim name As String = bookmark.Name
            Dim text As String = bookmark.Text
            bookmark.Name = "合同编号"
            bookmark.Text = ContractCode
        End If

        Dim watermark As Aspose.Words.Drawing.Shape = New Aspose.Words.Drawing.Shape(doc, ShapeType.Image)
        watermark.ImageData.SetImage(strImgPath)
        watermark.Left = 100
        watermark.Top = 400
        watermark.Width = 70
        watermark.Height = 21
        watermark.WrapType = WrapType.None
        watermark.BehindText = True
        watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page
        watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page

        '循环每页,将水印插入到文档每页中
        For Each sect As Aspose.Words.Section In doc.Sections
            InsertWatermarkImgIntoHeader(watermark, sect, HeaderFooterType.HeaderFirst)
            InsertWatermarkImgIntoHeader(watermark, sect, HeaderFooterType.HeaderPrimary)
            InsertWatermarkImgIntoHeader(watermark, sect, HeaderFooterType.HeaderEven)
        Next

    End Sub

    Private Sub InsertWatermarkImgIntoHeader(ByVal watermark As spose.Words.Drawing.Shape , ByVal sect As Aspose.Words.Section, ByVal headerType As HeaderFooterType)
        Dim header As Aspose.Words.HeaderFooter = sect.HeadersFooters(headerType)
        If header IsNot Nothing Then

            If header.Paragraphs.Count = 0 Then
                   header.AppendChild(New Paragraph(sect.Document))
            End If

            header.FirstParagraph.AppendChild(watermark.Clone(True))
        End If
    End Sub
private void InsertWatermarkImageToWord(Document doc, string strImgPath, string ContractCode)
{
    doc.AcceptAllRevisions();

    Bookmark bookmark = doc.Range.Bookmarks["合同编号"];

    if (bookmark != null)
    {
        string name = bookmark.Name;
        string text = bookmark.Text;
        bookmark.Name = "合同编号";
        bookmark.Text = ContractCode;
    }

    Shape watermark = new Shape(doc, ShapeType.Image);
    watermark.ImageData.SetImage(strImgPath);
    watermark.Left = 100;
    watermark.Top = 400;
    watermark.Width = 70;
    watermark.Height = 21;
    watermark.WrapType = WrapType.None;
    watermark.BehindText = true;
    watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;

    // 循环每页,将水印插入到文档每页中
    foreach (Section sect in doc.Sections)
    {
        InsertWatermarkImgIntoHeader(watermark, sect, HeaderFooterType.HeaderFirst);
        InsertWatermarkImgIntoHeader(watermark, sect, HeaderFooterType.HeaderPrimary);
        InsertWatermarkImgIntoHeader(watermark, sect, HeaderFooterType.HeaderEven);
    }
}

private void InsertWatermarkImgIntoHeader(Shape watermark, Section sect, HeaderFooterType headerType)
{
    HeaderFooter header = sect.HeadersFooters[headerType];
    if (header != null)
    {
        if (header.Paragraphs.Count == 0)
            header.AppendChild(new Paragraph(sect.Document));

        header.FirstParagraph.AppendChild(watermark.Clone(true));
    }
}
fengli0 commented 3 years ago

@AlexNosk 不知道是不是版本的问题(我这边的版本是19.10.0.0),我这边用您的代码测试,发现无法插入水印。还需要加下面的代码才行(见下图) image 但是这个只会在第一页插入水印,其他页不会插入水印(见附件) 12 专业工程分包合同(35616家合桩基)19180610.docx

AlexNosk commented 3 years ago

@fengli0 突出显示的代码实际上不是必需的,因为它重复了水印的插入。 另外,我还修改了InsertWatermarkImgIntoHeader方法,以使其创建头(如果不存在)。 我已使用您先前随附的原始文档对代码进行了测试,并且所有页面上都存在水印。 我使用最新的21.1版本的Aspose.Words进行了测试

private static void InsertWatermarkImgIntoHeader(Shape watermark, Section sect, HeaderFooterType headerType)
{
    HeaderFooter header = sect.HeadersFooters[headerType];
    if (header == null)
        sect.HeadersFooters.Add(new HeaderFooter(sect.Document, headerType));

    if (header.Paragraphs.Count == 0)
        header.AppendChild(new Paragraph(sect.Document));

    header.FirstParagraph.AppendChild(watermark.Clone(true));
}
fengli0 commented 3 years ago

@AlexNosk 非常感谢!我之前说的只会在第一页插入水印的问题,是因为 watermark.ImageData.SetImage(strImgPath); 这个方法导致的,我改成watermark.ImageData.SetImage(Image.FromFile(strImgPath));后就可以了

AlexNosk commented 3 years ago

@fengli0 设法实现所需的东西是件好事。 问题可以解决吗?

fengli0 commented 3 years ago

@AlexNosk 已经解决了,非常感谢!