如何将图像插入第一个页脚?

问题描述 投票:0回答:1

我创建了页脚,插入文本没有任何问题。

        static void Main(string[] args)
    {
        CreateFooter(@"D:\1.docx");

     }

    static void CreateFooter(String documentPath)
    {
      
    using (WordprocessingDocument document = WordprocessingDocument.Open(documentPath, true))
        {
       
            MainDocumentPart mainDocumentPart = document.MainDocumentPart;
            mainDocumentPart.DeleteParts(mainDocumentPart.FooterParts);
     
            FooterPart footerPart = mainDocumentPart.AddNewPart<FooterPart>();

            string footerPartId = mainDocumentPart.GetIdOfPart(footerPart);

            GenerateFooter(footerPart);

            var sections = mainDocumentPart.Document.Body.Elements<SectionProperties>().FirstOrDefault();

                sections.RemoveAllChildren<FooterReference>();
                sections.PrependChild<FooterReference>(new FooterReference() { Id = footerPartId });

        }    
    }




    static void GenerateFooter(FooterPart part)
    {
        Footer footer1 = new Footer() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
        footer1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
        footer1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
        footer1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
        footer1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
        footer1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
        footer1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
        footer1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
        footer1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
        footer1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
        footer1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
        footer1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
        footer1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
        footer1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
        footer1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
        footer1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");

        Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" };

        ParagraphProperties paragraphProperties1 = new ParagraphProperties();
        ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Footers" };
        using (FileStream stream = File.OpenRead(@"C:\Users\Амир\Downloads\Подпись.png"))
        {
            var img = Bitmap.FromStream(stream);

            paragraphProperties1.Append(paragraphStyleId1);

            Run run1 = new Run();
            Text text1 = new Text();
            text1.Text = "Footessr";

            run1.Append(text1);

            paragraph1.Append(paragraphProperties1);
            paragraph1.Append(run1);

            footer1.Append(paragraph1);

            part.Footer = footer1;
        }
    }

但是有几个问题,但是有几个问题我无法解决: 1 问题是我不知道如何插入图像。 2 如何确保页脚仅添加到一页? 3.如何确保页脚添加到右下角

c# asp.net openxml
1个回答
0
投票

2 如何确保页脚仅添加到一页?

您可以将页脚引用类型添加为

HeaderFooterValues.First
并将
TitlePage
属性添加到部分属性。

foreach (var section in sections)
 {
     // Delete existing references to  footers                   
     section.RemoveAllChildren<FooterReference>();
     section.PrependChild<TitlePage>(new TitlePage());
     // Create the new  footer reference node
     section.PrependChild<FooterReference>(new FooterReference() { Id = footerPartId, Type = HeaderFooterValues.First });
 }

3.如何确保页脚添加到右下角

您可以设置 Justification 类:此元素指定应用于本段落中文本的段落对齐方式。

   Justification Right = new Justification() { Val = JustificationValues.Right };      
   paragraphProperties1.Append(Right);

1 问题是我不知道如何插入图像。

插入图像的代码有很多。详细请参考以下代码。 (文档和图片的路径需要更改)

static void Main(string[] args)
{
    CreateFooter(@"C:\1.docx");

}

static void CreateFooter(String documentPath)
{

    // Replace header in target document with header of source document.
    using (WordprocessingDocument document = WordprocessingDocument.Open(documentPath, true))
    {
        string imagePath = @"C:\Capture.PNG";
        setHeaderPicture(imagePath);
        // Get the main document part
        MainDocumentPart mainDocumentPart = document.MainDocumentPart;

        // Delete the existing footer parts             
        mainDocumentPart.DeleteParts(mainDocumentPart.FooterParts);
        // Create a new header and footer part

        FooterPart footerPart = mainDocumentPart.AddNewPart<FooterPart>();

        // Get Id of thefooter parts

        string footerPartId = mainDocumentPart.GetIdOfPart(footerPart);



        GenerateFooterPartContent(footerPart);

        ImagePart image = footerPart.AddNewPart<ImagePart>("image/PNG", "rId999");
        GenerateImagePart1Content(image);
        // Get SectionProperties and Replace FooterRefernce with new Id
        IEnumerable<DocumentFormat.OpenXml.Wordprocessing.SectionProperties> sections = mainDocumentPart.Document.Body.Elements<DocumentFormat.OpenXml.Wordprocessing.SectionProperties>();

        foreach (var section in sections)
        {
            // Delete existing references to  footers                   
            section.RemoveAllChildren<FooterReference>();
            section.PrependChild<TitlePage>(new TitlePage());
            // Create the new  footer reference node

            section.PrependChild<FooterReference>(new FooterReference() { Id = footerPartId, Type = HeaderFooterValues.First });
        }
    }
}



static void GenerateFooterPartContent(FooterPart part)
{
    Footer footer1 = new Footer() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
    footer1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
    footer1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
    footer1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
    footer1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
    footer1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
    footer1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
    footer1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
    footer1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
    footer1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
    footer1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
    footer1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
    footer1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
    footer1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
    footer1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
    footer1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");

    Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" };

    ParagraphProperties paragraphProperties1 = new ParagraphProperties();

    ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Footers" };
    Justification Right = new Justification() { Val = JustificationValues.Right };
    paragraphProperties1.Append(Right);
    paragraphProperties1.Append(paragraphStyleId1);
    Run run1 = new Run();
    Text text1 = new Text();
    text1.Text = "Footessr";
    Picture picture1 = new Picture();

    Shape shape1 = new Shape() { Id = "WordPictureWatermark75517470", Style = "width:20px;height:20px" };
    ImageData imageData1 = new ImageData() { RelationshipId = "rId999" };
    shape1.Append(imageData1);
    picture1.Append(shape1);
    run1.Append(picture1);

    run1.Append(text1);

    paragraph1.Append(paragraphProperties1);
    paragraph1.Append(run1);

    footer1.Append(paragraph1);

    part.Footer = footer1;

}
static void GenerateImagePart1Content(ImagePart imagePart1)
{
    System.IO.Stream data = GetBinaryDataStream(imagePart1Data);
    imagePart1.FeedData(data);
    data.Close();
}
static System.IO.Stream GetBinaryDataStream(string base64String)
{
    return new System.IO.MemoryStream(System.Convert.FromBase64String(base64String));
}
static string imagePart1Data = "";
static void setHeaderPicture(string file)
{
    FileStream inFile;
    byte[] byteArray;
    try
    {
        inFile = new FileStream(file, FileMode.Open, FileAccess.Read);
        byteArray = new byte[inFile.Length];
        long byteRead = inFile.Read(byteArray, 0, (int)inFile.Length);
        inFile.Close();
        imagePart1Data = Convert.ToBase64String(byteArray, 0, byteArray.Length);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.