Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
接下来我们将进入关于“图像处理”的介绍,在Aspose.Words中学会如何在Word文档的所有或特定页面上添加相同或不同的条形码。
>>Aspose.Words for .NET更新至最新版 点击下载体验
欢迎下载|体验更多Aspose文档管理产品或 加入Aspose技术交流群(761297826)
在文档的每一页上插入条形码
没有直接的方法可以在文档的所有页面上添加条形码,但是可以使用DocumentBuilder.MoveToSection、DocumentBuilder.MoveToHeaderFooter和DocumentBuilder.InsertImage方法移动到任何部分或页眉/页脚,并插入条形码图像。以下代码示例在文档的每一页上插入条形码图像。
-
- // The path to the documents directory.
- string dataDir = RunExamples.GetDataDir_WorkingWithImages();
- // Create a blank documenet.
- Document doc = new Document();
- DocumentBuilder builder = new DocumentBuilder(doc);
-
- // The number of pages the document should have.
- int numPages = 4;
- // The document starts with one section, insert the barcode into this existing section.
- InsertBarcodeIntoFooter(builder, doc.FirstSection, 1, HeaderFooterType.FooterPrimary);
-
- for (int i = 1; i < numPages; i++)
- {
- // Clone the first section and add it into the end of the document.
- Section cloneSection = (Section)doc.FirstSection.Clone(false);
- cloneSection.PageSetup.SectionStart = SectionStart.NewPage;
- doc.AppendChild(cloneSection);
-
- // Insert the barcode and other information into the footer of the section.
- InsertBarcodeIntoFooter(builder, cloneSection, i, HeaderFooterType.FooterPrimary);
- }
-
- dataDir = dataDir + "Document_out.docx";
- // Save the document as a PDF to disk. You can also save this directly to a stream.
- doc.Save(dataDir);
- 12月即将走过!Aspose.Total10000元直降活动即将落下帷幕!赶紧联系慧都客服1分钟了解全部资讯!
-
- private static void InsertBarcodeIntoFooter(DocumentBuilder builder, Section section, int pageId, HeaderFooterType footerType)
- {
- // Move to the footer type in the specific section.
- builder.MoveToSection(section.Document.IndexOf(section));
- builder.MoveToHeaderFooter(footerType);
-
- // Insert the barcode, then move to the next line and insert the ID along with the page number.
- // Use pageId if you need to insert a different barcode on each page. 0 = First page, 1 = Second page etc.
- builder.InsertImage(System.Drawing.Image.FromFile( RunExamples.GetDataDir_WorkingWithImages() + "Barcode1.png"));
- builder.Writeln();
- builder.Write("1234567890");
- builder.InsertField("PAGE");
-
- // Create a right aligned tab at the right margin.
- double tabPos = section.PageSetup.PageWidth - section.PageSetup.RightMargin - section.PageSetup.LeftMargin;
- builder.CurrentParagraph.ParagraphFormat.TabStops.Add(new TabStop(tabPos, TabAlignment.Right, TabLeader.None));
-
- // Move to the right hand side of the page and insert the page and page total.
- builder.Write(ControlChar.Tab);
- builder.InsertField("PAGE");
- builder.Write(" of ");
- builder.InsertField("NUMPAGES");
- }
-