请教一个itextsharp的问题

jhonsonzhang 2010-11-29 09:06:14
dim pdfpath as string="c:\src1.pdf"
dim decPath as string="c:\test1.pdf"
Dim rd As New PdfReader(PdfPath)
Dim doc As New Document(PageSize.A4)
Dim wr As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(decPath, FileMode.Create))
doc.Open()
Dim cb As PdfContentByte = wr.DirectContent
doc.NewPage()

Dim pha As New Phrase("aaaaaaa")
doc.Add(pha)

这是加的第一页的内容,是纵向的。

现在开始加第二页
doc.NewPage()
cb.AddTemplate(wr.GetImportedPage(rd, 1), 0, 0)

但这个第二页的页面是横向的,怎么办才能使这个页面也横向呢?
rd.Close()
doc.Close()
...全文
139 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangweizhao 2011-03-30
  • 打赏
  • 举报
回复
这个问题可以这样解决,其实和合并pdf文件的原理是一样的。当你pdfreader到一个文件,并从里面取出一页,如果发现这页的宽度大于高度,则可以当成是横向页面来处理。这时候你需要新建一个临时的document,并且用PageSize.A4.rotate()这种方式。该文件全部合并到这个临时文件里面,下面去操作这个临时文件里面的页面就行了。
我的代码如下:纵向转横向的还没写!!!

Private Sub MergePdfFiles(ByVal strSrcFilePath As String, ByVal strSrcPdfFiles As String(), ByVal strDestFilePath As String, ByVal strDestPdfFile As String)

Dim intIdx As Integer = 0
Try
If Not strSrcFilePath.EndsWith("\") Then
strSrcFilePath = strSrcFilePath + "\"
End If
If Not strDestFilePath.EndsWith("\") Then
strDestFilePath = strDestFilePath + "\"
End If

'先將橫向的pdf文件轉換成縱向的pdf文件
For intIdx = 0 To strSrcPdfFiles.Length - 1
ChangePdfDirection(strSrcFilePath, strSrcPdfFiles(intIdx), 1)
Next

Dim strDestFullName As String = strDestFilePath + strDestPdfFile

Dim reader As PdfReader
Dim doc As New Document(PageSize.A4, 5, 5, 0, 0)
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(strDestFullName, FileMode.Create))
doc.Open()
Dim cb As PdfContentByte = writer.DirectContent
Dim newpage As PdfImportedPage

For intIdx = 0 To strSrcPdfFiles.Length - 1
Dim strSrcFullName As String = strSrcFilePath + strSrcPdfFiles(intIdx)
reader = New PdfReader(strSrcFullName.Trim())
Dim intPageCnt As Integer = reader.NumberOfPages
For intPageIdx As Integer = 1 To intPageCnt
doc.NewPage()
newpage = writer.GetImportedPage(reader, intPageIdx)

Dim truePageHeight As Double = doc.PageSize.Height - doc.TopMargin - doc.BottomMargin
Dim truePageWidth As Double = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin
cb.AddTemplate(newpage, truePageWidth / newpage.Width, 0, 0, truePageHeight / newpage.Height, doc.LeftMargin, doc.TopMargin)
Next
Next
doc.Close()
doc.Dispose()
Catch ex As Exception
Throw New Exception("Error:" + intIdx.ToString().Trim())
End Try
End Sub

Private Sub ChangePdfDirection(ByVal strPath As String, ByVal strPdfFile As String, ByVal intMode As Integer)
If intMode = 1 Then '橫向轉換成縱向
If Not strPath.EndsWith("\") Then
strPath = strPath + "\"
End If
Dim strPdfFullName As String = strPath + strPdfFile
Dim strpdfTmpName As String = strPath + "tmp_" + strPdfFile
Dim doc As Document = Nothing
Dim reader As New PdfReader(strPdfFullName.Trim())
Dim intPageCnt As Integer = reader.NumberOfPages
Dim psize As Rectangle = reader.GetPageSize(1)
If psize.Width > psize.Height Then '當pdf的寬度大於高度,當作橫向pdf處理
doc = New Document(PageSize.A4.Rotate, 5, 5, 0, 0)
Else
Exit Sub
End If

Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(strpdfTmpName, FileMode.Create))
doc.Open()
Dim cb As PdfContentByte = writer.DirectContent
For intIdx As Integer = 1 To intPageCnt
doc.NewPage()
Dim page1 As PdfImportedPage = writer.GetImportedPage(reader, intIdx)
Dim truePageWidth As Double = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin
Dim truePageHeight As Double = doc.PageSize.Height - doc.TopMargin - doc.BottomMargin
cb.AddTemplate(page1, truePageWidth / page1.Width, 0, 0, truePageHeight / page1.Height, doc.LeftMargin, doc.TopMargin)
Next

doc.Close()
doc.Dispose()
writer.Close()
writer.Dispose()
reader.Close()

If File.Exists(strPdfFullName) Then
File.Delete(strPdfFullName)
End If
If File.Exists(strpdfTmpName) Then
File.Copy(strpdfTmpName, strPdfFullName)
File.Delete(strpdfTmpName)
End If
'If File.Exists(strpdfTmpName) Then
'End If

Else '縱向轉換成橫向

End If
End Sub
jhonsonzhang 2010-11-29
  • 打赏
  • 举报
回复
谢谢3楼的朋友,正在看这个pdfbox,看来很不错一个东西。如果朋友有相关资料请发到jhonsonzhang@sohu.com里面,行不?
jhonsonzhang 2010-11-29
  • 打赏
  • 举报
回复
问题最终还是解决了,但不是的用的itextsharp来解决的,结合另外一个第三方的类,把这个问题解决了,但始终差强人意,可控性太差了。比如无法在相应页添加标签。实在郁闷。现在只能是有所取舍了。
jhonsonzhang 2010-11-29
  • 打赏
  • 举报
回复
那个好像是vb6.0做的,是用打印机来解决的,不是很理想。
十八道胡同 2010-11-29
  • 打赏
  • 举报
回复
哦,刚才我还在查pdfbox ,要不你试试这个? 是开源的
jhonsonzhang 2010-11-29
  • 打赏
  • 举报
回复
谢谢如梦兄,但这是在一个document里面,如果要横向,就全部都横向了,而有些页又不需要横向。我郁闷极了,查了所有itextsharp的资料,始终没法解决。
wuyq11 2010-11-29
  • 打赏
  • 举报
回复
大多数情况下使用纵向页面,横向页面
Document document = new Document(PageSize.A4.rotate());

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧