以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- pdfsharp文件操作pdf,怎么实现目录下的pdf批量合并成一个呢?怎么实现给新生成的pdf添加水印?怎么实现给新生成的pdf从指定的页数后面开始添加数字作为编码? (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=189241) |
-- 作者:cnsjroom -- 发布时间:2023/11/19 3:48:00 -- pdfsharp文件操作pdf,怎么实现目录下的pdf批量合并成一个呢?怎么实现给新生成的pdf添加水印?怎么实现给新生成的pdf从指定的页数后面开始添加数字作为编码? 参照论坛帖子学习pdf文件的合并与页码获取等操作:http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=151835&authorid=0&page=0&star=1 遇到如下问题,经摸索,得以解决,特备注备用。 以下内容是专门发给有点蓝浏览 当前遇见几个问题: 1、为什么以下红色部分代码直接命令窗口运行或直接写成内部函数运行都会报错:::【修正错误后,遍历实现所有pdf页数的汇总统计】 Dim ymsum As Integer = 0 For Each File As String In FileSys.GetFiles("C:\\Users\\Administrator\\Desktop") \'遍历指定目录下的文件 If FileSys.GetName(File).Contains(".pdf") Then \'找到名称中含pdf的文件 Dim wenjian As String = "C:\\Users\\Administrator\\Desktop\\pdf文件操作\\Test2.pdf" Dim Test As pdfSharp.pdf.PdfDocument = pdfSharp.pdf.IO.PdfReader.Open(wenjian, pdfSharp.pdf.IO.PdfDocumentOpenMode.Import) \'读取Test1.pdf MessageBox.show(Test.PageCount) ymsum = ymsum + Test.PageCount \'统计总的页数 End If Next Output.Show(ymsum) \'输出指定目录下的pdf文件的所有页码数 提示: --------------------------- 编译错误:局部变量不能与包含它的函数同名。 错误代码:Dim Test As pdfSharp.pdf.PdfDocument = pdfSharp.pdf.IO.PdfReader.Open(file, pdfSharp.pdf.IO.PdfDocumentOpenMode.Import) \'读取Test1.pdf --------------------------- 解决办法: Dim ymsum As Integer = 0 For Each File As String In FileSys.GetFiles("C:\\Users\\Administrator\\Desktop") \'遍历指定目录下的文件 If FileSys.GetName(File).Contains(".pdf") Then \'找到名称中含pdf的文件 Dim wenjian As String = File Dim mytest As pdfSharp.pdf.PdfDocument = pdfSharp.pdf.IO.PdfReader.Open(wenjian, pdfSharp.pdf.IO.PdfDocumentOpenMode.Import) \'读取Test1.pdf Output.show(mytest.PageCount) ymsum = ymsum + mytest.PageCount \'统计总的页数 End If Next Output.Show(ymsum) \'输出指定目录下的pdf文件的所有页码数 2、如何实现目录下的所有pdf合并呢? 红色部分代码怎么修正呢? Dim fl As String = ProjectPath & "Test4.pdf" For Each File As String In FileSys.GetFiles("C:\\Users\\Administrator\\Desktop") \'遍历指定目录下的文件 If FileSys.GetName(File).Contains(".pdf") Then \'找到名称中含pdf的文件 Dim Test1 As pdfSharp.pdf.PdfDocument = pdfSharp.pdf.IO.PdfReader.Open(file1, pdfSharp.pdf.IO.PdfDocumentOpenMode.Import) \'读取Test1.pdf Dim Test2 As pdfSharp.pdf.PdfDocument = pdfSharp.pdf.IO.PdfReader.Open(file2, pdfSharp.pdf.IO.PdfDocumentOpenMode.Import) \'读取Test2.pdf For i As Integer = 0 To Test1.PageCount - 1 Dim page As pdfSharp.pdf.PdfPage = Test1.Pages(i) \'从Test1获取页面\' Test2.AddPage(page) \'添加页面到Test2 Next Test2.Save(fl) \'另存为Test4.pdf Dim Proc As New Process \'定义一个新的Process Proc.File = fl \'指定要打开的文件 Proc.Start() End If Next 解决办法: Dim destinationPdf As String = ProjectPath & "AllPDFsMerged.pdf" Dim pdfDocument As New pdfSharp.pdf.PdfDocument() For Each file As String In FileSys.GetFiles("C:\\Users\\Administrator\\Desktop") If file.EndsWith(".pdf") Then \'找到名称以.pdf结尾的文件 Using document As pdfSharp.pdf.PdfDocument = pdfSharp.pdf.IO.PdfReader.Open(file, pdfSharp.pdf.IO.PdfDocumentOpenMode.Import) For Each page As pdfSharp.pdf.PdfPage In document.Pages \'遍历源文件的每一页 pdfDocument.AddPage(page) \'将该页添加到目标文件 Next End Using End If Next pdfDocument.Save(destinationPdf) \'将目标PDF保存到指定路径 3、假定pdf总页数为100页 ,“写页码” 按钮中的代码 怎么修正实现从pdf指定的页数 比如从第三页开始从1开始绘制页码直到97呢? Dim filename As String = e.Form.Controls("TextBox1").text Dim fl As String = ProjectPath & "Test7.pdf" \'Dim watermark As String = "Test Watermark" \'定义水印 Dim watermark As String Dim font As pdfSharp.Drawing.XFont = New pdfSharp.Drawing.XFont("Verdana", 30, pdfSharp.Drawing.XFontStyle.Bold) \'定义水印字体 Dim format As pdfSharp.Drawing.XStringFormat = New pdfSharp.Drawing.XStringFormat() format.Alignment = pdfSharp.Drawing.XStringAlignment.Near format.LineAlignment = pdfSharp.Drawing.XLineAlignment.Near Dim gfx As pdfSharp.Drawing.XGraphics Dim size As pdfSharp.Drawing.XSize Dim brush As pdfSharp.Drawing.XBrush Dim pen As pdfSharp.Drawing.XPen Dim path As pdfSharp.Drawing.XGraphicsPath Dim Test As pdfSharp.pdf.PdfDocument = pdfSharp.pdf.IO.PdfReader.Open(filename, pdfSharp.pdf.IO.PdfDocumentOpenMode.Modify) \'读取Test1.pdf For idx As Integer = 0 To Test.PageCount - 1 Dim page As pdfSharp.pdf.PdfPage = Test.Pages(idx) \'获取页面\' Dim width As Double = page.Width Dim height As Double = page.Height \'-------------------------------------------------------------------------------------------------------\'\' \' 第 1 种: 将水印绘制为文本字符串. \'获取用于在现有内容下绘制的图形对象 gfx = pdfSharp.Drawing.XGraphics.FromPdfPage(page, pdfSharp.Drawing.XGraphicsPdfPageOptions.Prepend) \'获取文本的大小(以磅为单位) watermark = idx + 1 size = gfx.MeasureString(watermark, font) \'创建暗红色笔刷 brush = New pdfSharp.Drawing.XSolidBrush(pdfSharp.Drawing.XColor.FromArgb(128, 255, 0, 0)) \'描绘字符串 gfx.DrawString(watermark, font, brush, New pdfSharp.Drawing.XPoint((width - size.Width) / 1.1, 20), format) 假定pdf总页数为100页 怎么修正实现从pdf指定的页数 比如从第三页开始从1开始绘制页码直到97呢? \' gfx.DrawString(watermark, font, brush, New pdfSharp.Drawing.XPoint((width - size.Width) / 2, (height - size.Height) / 2), format) Next Test.Save(fl) \'另存为Test7.pdf Dim Proc As New Process \'定义一个新的Process Proc.File = fl \'指定要打开的文件 Proc.Start() 解决办法: Dim destinationPdf As String = ProjectPath & "AllPDFsMerged.pdf" Dim fl As String = ProjectPath & "Test7.pdf" \'Dim watermark As String = "Test Watermark" \'定义水印 Dim watermark As String Dim font As pdfSharp.Drawing.XFont = New pdfSharp.Drawing.XFont("Verdana", 30, pdfSharp.Drawing.XFontStyle.Bold) \'定义水印字体 Dim format As pdfSharp.Drawing.XStringFormat = New pdfSharp.Drawing.XStringFormat() format.Alignment = pdfSharp.Drawing.XStringAlignment.Near format.LineAlignment = pdfSharp.Drawing.XLineAlignment.Near Dim gfx As pdfSharp.Drawing.XGraphics Dim size As pdfSharp.Drawing.XSize Dim brush As pdfSharp.Drawing.XBrush Dim pen As pdfSharp.Drawing.XPen Dim path As pdfSharp.Drawing.XGraphicsPath Dim my As pdfSharp.pdf.PdfDocument = pdfSharp.pdf.IO.PdfReader.Open(destinationPdf, pdfSharp.pdf.IO.PdfDocumentOpenMode.Modify) \'读取Test1.pdf For idx As Integer = 0 To my.PageCount - 1 Dim page As pdfSharp.pdf.PdfPage = my.Pages(idx) \'获取页面\' Dim width As Double = page.Width Dim height As Double = page.Height \'-------------------------------------------------------------------------------------------------------\'\' \' 第 1 种: 将水印绘制为文本字符串. \'获取用于在现有内容下绘制的图形对象 gfx = pdfSharp.Drawing.XGraphics.FromPdfPage(page, pdfSharp.Drawing.XGraphicsPdfPageOptions.Prepend) \'获取文本的大小(以磅为单位) If idx >= 2 Then watermark = idx + 1 - 2 size = gfx.MeasureString(watermark, font) \'创建暗红色笔刷 brush = New pdfSharp.Drawing.XSolidBrush(pdfSharp.Drawing.XColor.FromArgb(128, 255, 0, 0)) \'描绘字符串 gfx.DrawString(watermark, font, brush, New pdfSharp.Drawing.XPoint((width - size.Width) / 1.1, 20), format) \' gfx.DrawString(watermark, font, brush, New pdfSharp.Drawing.XPoint((width - size.Width) / 2, (height - size.Height) / 2), format) End If Next my.Save(fl) \'另存为Test7.pdf Dim Proc As New Process \'定义一个新的Process Proc.File = fl \'指定要打开的文件 Proc.Start() [此贴子已经被作者于2023/11/20 0:40:30编辑过]
|
-- 作者:有点蓝 -- 发布时间:2023/11/19 19:44:00 -- 第三方组件请自行研究组件的开发文档。 |
-- 作者:puma -- 发布时间:2023/11/19 21:33:00 -- |
-- 作者:puma -- 发布时间:2023/11/19 21:36:00 -- 还用.net 2.0 如果是win11或者win 10 需要启用.net 3.5? 吧 |
-- 作者:有点蓝 -- 发布时间:2023/11/19 21:41:00 -- 要找.net framework 4.0及以下的报表,Foxtable才能使用 |