Vb.net “获取异常”;“文件已打开”;生成pdf时
我正在尝试使用iTextSharp库将tiff文件转换为pdf,并获得此异常。Vb.net “获取异常”;“文件已打开”;生成pdf时,vb.net,pdf,itextsharp,tiff,Vb.net,Pdf,Itextsharp,Tiff,我正在尝试使用iTextSharp库将tiff文件转换为pdf,并获得此异常。 “文档已打开” 这是我的密码: Dim saveFileDialog1 As New SaveFileDialog() saveFileDialog1.Filter = "Pdf files (*.pdf)|*.pdf|All files (*.*)|*.*" saveFileDialog1.FilterIndex = 2 saveFileDialog1.RestoreDirectory = True If
“文档已打开”
这是我的密码:
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "Pdf files (*.pdf)|*.pdf|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
' creation of the document with a certain size and certain margins
Dim document As New iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0)
' load the tiff image and count the total pages
Dim bm As New System.Drawing.Bitmap(OpenFileDialog1.FileName)
Dim total As Integer = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)
document.Open()
' creation of the different writers
Dim writer As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(document, New System.IO.FileStream(saveFileDialog1.FileName, System.IO.FileMode.Create))
Dim cb As iTextSharp.text.pdf.PdfContentByte = writer.DirectContent
For k As Integer = 0 To total - 1
bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k)
Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Bmp)
' scale the image to fit in the page
img.ScalePercent(72.0F / img.DpiX * 100)
img.SetAbsolutePosition(0, 0)
cb.AddImage(img)
document.NewPage()
Next k
document.Close()
有人能告诉我我做错了什么和哪里
编辑:
当我尝试添加尺寸标注时:
img.ScaleToFit(595, 842)
img.SetAbsolutePosition(0, 0)
正如我在中所解释的,在使用iText(Sharp)时,您应该分5步创建PDF。创建编写器是第2步,打开文档是第3步。您已经切换了这两个步骤,这就是导致错误的原因
您需要将行document.Open()
向下移动几行。在创建writer
之后和创建cb
实例之前。错误具体发生在哪里?从它的外观来看,我几乎打赌:Dim writer As iTextSharp.text.pdf.PdfWriter=iTextSharp.text.pdf.PdfWriter.GetInstance(document,New System.IO.FileStream(saveFileDialog1.FileName,System.IO.FileMode.Create))是的。你能告诉我为什么以及我现在需要做什么吗?\从表面上看,你遵循了这个例子;如果我错了,请纠正我。还是这一个?不,你在Jst之前说的那个,现在我正在研究它,幸运的是得到了你的答案。这有很多问题,还有一个疑问是,当添加tiff文件时,它会缩放图像,因为我需要适应图像。我尝试过使用img.ScalePercent(24.0F),但图像是在中心生成的。你能告诉我如何修改它吗?你正在创建A4页面,并且正在添加未知大小的图像。如果我是你,我会调整图像大小,使其适合A4页面:img.ScaleToFit(595842)代码>值595和842是A4页面的宽度和高度。ScaleToFit()
方法将保留图像的纵横比。如果没有必要,可以使用img.ScaleAbsolute(595842)代码>(但很明显,这会以不同的纵横比扭曲图像)。不要忘记img.SetAbsolutePosition(0,0)
在底部添加图像!当我尝试添加您所说的维度时,我得到的结果如上所示。为什么我会在图像的顶部有一个空白?最后但并非最不重要的是,我可以在我的商业项目中包括这个dll,还是我们需要付费?(1)你的图像的纵横比是多少?如果高度远小于宽度,则得到的结果是正常的。(2) iTextSharp在AGPL下发布。如果创建商业项目,则需要许可证。填写销售表格了解更多信息:这是一个1066*1155像素的tiff文件