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

wps文档加水印后,段落格式会被修改为段落后加10磅,导致页面都往下一页移动,格式错乱. #293

Closed 18571532202 closed 3 years ago

18571532202 commented 3 years ago

原始文档: image

加水印后: image

AlexNosk commented 3 years ago

@18571532202 请附加您的输入和输出文档以及代码以重现该问题。

18571532202 commented 3 years ago

原始文档: 合同登记2020_07_23[10_17_40_21]1515151744(1).docx 加水印后文档: 合同登记2020_07_23[10_17_40_21]1515151744(1)29095237.docx 实现代码:

string strNewFileName = label3.Text;  // 原始文档地址
Aspose.Words.Document doc = new Aspose.Words.Document(strNewFileName);
string ImgPath = "zjsy.jpg"; // 水印
InsertWatermarkImageToWord(doc, ImgPath); //加水印
doc.Save(strNewFileName); //保存文档

--加水印方法: // 实例化一个水印图片

Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.Shape(doc, ShapeType.Image );

            watermark.ImageData.SetImage (imgPath );
            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;

            // 初始化一个段落,将水印追加到段落中
            Aspose.Words.Paragraph watermarkPara = new Aspose.Words.Paragraph(doc);
            watermarkPara.AppendChild(watermark);

            DocumentBuilder builder = new DocumentBuilder(doc);
            Aspose.Words.Section currentSection = builder.CurrentSection;
            Aspose.Words.PageSetup pageSetup = currentSection.PageSetup;
            pageSetup.HeaderDistance = 20;
            builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
            builder.InsertImage(imgPath, RelativeHorizontalPosition.Page, 100, RelativeVerticalPosition.Page, 400, 70, 21, WrapType.None);

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

--循环体方法

 private static void InsertWatermarkImgIntoHeader(Aspose.Words.Paragraph watermarkPara, Aspose.Words.Section sect, HeaderFooterType headerType)
        {

            Aspose.Words.HeaderFooter header = sect.HeadersFooters[headerType];

            if (header != null)
            {
                //复制水印,并追加到文档头部分
                header.AppendChild(watermarkPara.Clone(true));
            }
        }
AlexNosk commented 3 years ago

@18571532202 如我所见,您正在使用旧版本的Aspose.Words(16.7.0)。 我使用最新的21.1.0版本的Aspose.Words测试了该方案,并且输出文档看起来正确。 同样在最新版本的Aspose.Words中,您可以使用内置方法来插入水印。 https://docs.aspose.com/words/net/working-with-watermark/ 我也纠正了您插入水印的代码

private static void InsertWatermarkImageToWord(Document doc, string strImgPath)
{
    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;

    // Insert watermark into the document's headers.
    foreach (Section sect in doc.Sections)
    {
        InsertWatermarkImgIntoHeader(watermark, sect, HeaderFooterType.HeaderFirst);
        InsertWatermarkImgIntoHeader(watermark, sect, HeaderFooterType.HeaderPrimary);
        InsertWatermarkImgIntoHeader(watermark, sect, HeaderFooterType.HeaderEven);
    }
}

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

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

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

var pdfDoc = new Aspose.Words.Document(fileStream);

                        pdfDoc.Save(resultFilePath, SaveFormat.Pdf);
                        txtResultFilePath.Text = resultFilePath;

我这边做了一个简单的例子,用了最新的版本,直接Word转PDF 还是会段落后加10磅

AlexNosk commented 3 years ago

我无法使用最新版本的Aspose.Words重现此问题。 请参阅我身边生成的随附PDF文档。 请在此处附加您的输入和输出文档,以进行进一步调查。 out.pdf

18571532202 commented 3 years ago

您可以看一下 我的原始Word文档表格是在第一页的,您这个输出的PDF文档是换页了。

AlexNosk commented 3 years ago

我没有看到这个问题。 我这边的两个文档输入DOCX和输出PDF看起来都一样。 您可以在此处附上文档进行测试吗?

AlexNosk commented 3 years ago

@18571532202 您是否仍需要此问题的帮助,或者可以将其关闭?

AlexNosk commented 3 years ago

@18571532202 I am closing the issue since no additional feedback and the original problem is not reproducible. Please feel free to create another issue in case of any problems.