代码之家  ›  专栏  ›  技术社区  ›  Sabs

使用Aspose文字将页码替换为条形码

  •  1
  • Sabs  · 技术社区  · 7 年前

    这可能是一个愚蠢的问题,但我无法找到答案,一天后,我向整个社区寻求帮助。。。

    所以我真的需要找到编号容器,读取其中的值并替换它。一旦我有了创建条形码和插入它很容易。

    有人能帮忙吗?

    当前的方法(很抱歉,它很混乱,但我一直在尝试新的东西):

    internal static void SetFooters(ref Document doc)
        {
            doc.FirstSection.HeadersFooters.LinkToPrevious(false);
            var builder = new DocumentBuilder(doc);
            builder.MoveToDocumentStart();
            Section currentSection = builder.CurrentSection;
            PageSetup pageSetup = currentSection.PageSetup;
            int totalPages = doc.PageCount;
            int j = 1;
            foreach (Section sect in doc.Sections)
            {
                //Loop through all headers/footers
                foreach (HeaderFooter hf in sect.HeadersFooters)
                {
                    if (
                    hf.HeaderFooterType == HeaderFooterType.FooterPrimary || hf.HeaderFooterType == HeaderFooterType.FooterEven || hf.HeaderFooterType == HeaderFooterType.FooterFirst)
                    {
                        builder.MoveToHeaderFooter(hf.HeaderFooterType);
                        Field page = builder.InsertField("PAGE");
                        builder.Document.UpdatePageLayout();
                        try
                        {
                            page.Update();
                        }
                        catch { }
    
                        int pageNumber = j;
                        if (int.TryParse(page.Result, out pageNumber))
                        { j++; }
                        // Remove PAGE field.
                        page.Remove();
                        builder.Write(string.Format("{0}/{1}", pageNumber, totalPages));
                    }
                }
            }
        }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Tahir Manzoor    7 年前

    HeaderFooter是节级节点,只能是节的子级。页眉/页脚内的页面字段返回最新更新的值,该值对于一个节的所有页面都是相同的。

    在您的情况下,我建议您在每页的顶部/底部插入文本框,并在其中插入所需的内容。下面的代码示例在文档的每一页上插入文本框,并在其中插入页面字段和一些文本。希望这对你有帮助。

    public static void InsertTextBoxAtEachPage()
    {
        string filePathIn = MyDir + @"input.docx";
        string filePathOut = MyDir + @"output.docx";
    
        Document doc = new Document(filePathIn);
    
        DocumentBuilder builder = new DocumentBuilder(doc);
        LayoutCollector collector = new LayoutCollector(doc);
    
        int pageIndex = 1;
        foreach (Section section in doc.Sections)
        {
            NodeCollection paragraphs = section.Body.GetChildNodes(NodeType.Paragraph, true);
            foreach (Paragraph para in paragraphs)
            {
                if (collector.GetStartPageIndex(para) == pageIndex)
                {
                    builder.MoveToParagraph(paragraphs.IndexOf(para), 0);
                    builder.StartBookmark("BM_Page" + pageIndex);
                    builder.EndBookmark("BM_Page" + pageIndex);
                    pageIndex++;
                }
            }
        }
    
        collector = new LayoutCollector(doc);
        LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
    
        const int PageRelativeY = 0;
        const int PageRelativeX = 0;
    
        foreach (Bookmark bookmark in doc.Range.Bookmarks)
        {
            if (bookmark.Name.StartsWith("BM_"))
            {
                Paragraph para = (Paragraph)bookmark.BookmarkStart.ParentNode;
    
                Shape textbox = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextBox);
    
                textbox.Top = PageRelativeY;
                textbox.Left = PageRelativeX;
    
                int currentPageNumber = collector.GetStartPageIndex(para);
    
                string barcodeString = string.Format("page {0} of {1}", currentPageNumber, doc.PageCount);
                string barcodeEncodedString = "some barcode string";
    
                Paragraph paragraph = new Paragraph(doc);
                ParagraphFormat paragraphFormat = paragraph.ParagraphFormat;
                paragraphFormat.Alignment = ParagraphAlignment.Center;
                Aspose.Words.Style paragraphStyle = paragraphFormat.Style;
                Aspose.Words.Font font = paragraphStyle.Font;
                font.Name = "Tahoma";
                font.Size = 12;
                paragraph.AppendChild(new Run(doc, barcodeEncodedString));
                textbox.AppendChild(paragraph);
    
                paragraph = new Paragraph(doc);
                paragraphFormat = paragraph.ParagraphFormat;
                paragraphFormat.Alignment = ParagraphAlignment.Center;
    
                paragraphStyle = paragraphFormat.Style;
                font = paragraphStyle.Font;
                font.Name = "Arial";
                font.Size = 10;
                paragraph.AppendChild(new Run(doc, barcodeString));
                textbox.AppendChild(paragraph);
    
                //Set the width height according to your requirements
                textbox.Width = doc.FirstSection.PageSetup.PageWidth;
                textbox.Height = 50;
                textbox.BehindText = false;
    
                para.AppendChild(textbox);
    
                textbox.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page;
                textbox.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page;
    
                bool isInCell = bookmark.BookmarkStart.GetAncestor(NodeType.Cell) != null;
                if (isInCell)
                {
                    var renderObject = collector.GetEntity(bookmark.BookmarkStart);
                    layoutEnumerator.Current = renderObject;
    
                    layoutEnumerator.MoveParent(LayoutEntityType.Cell);
                    RectangleF location = layoutEnumerator.Rectangle;
    
                    textbox.Top = PageRelativeY - location.Y;
                    textbox.Left = PageRelativeX - location.X;
                }
            }
        }
    
        doc.Save(filePathOut, SaveFormat.Docx);
    }
    

    我与Aspose合作,担任开发人员传道者。