【问题标题】:using Aspose Words to replace page numbers with barcodes使用 Aspose Words 将页码替换为条码
【发布时间】:2017-07-09 18:32:38
【问题描述】:

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

我正在使用 Aspose for Word(C# 或 .Net),并尝试将生成的页码替换为我自己创建的条形码图像。我目前可以使用字体来做到这一点,但我发现它们对我的条形码阅读器不太可靠,因此需要能够从页码读取值并将其替换为我自己创建的图像。

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

谁能帮忙?

目前的方法(对不起,它很乱,但我一直在尝试新事物):

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));
                }
            }
        }
    }

【问题讨论】:

    标签: c# aspose aspose.words


    【解决方案1】:

    HeaderFooter 是节级节点,只能是 Section 的子节点。页眉/页脚内的页面字段返回最新更新的值,并且对于一个部分的所有页面来说,它将是相同的值。

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

    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 合作,担任开发人员传道者。

    【讨论】:

    • 嗨 Tahir,我目前拥有的是一种方法,它只是尝试遍历页脚部分并写入当前页码和总页数。添加条形码位很容易,它实际上可以获取当前页码,这似乎让我在追逐我的尾巴!这是下面的代码:...好吧,这里发布的时间似乎太长了,我将编辑我的问题
    • 我已根据您的要求更新了我的答案。希望这对您有所帮助。我与 Aspose 合作,担任开发人员传道者。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多