2016-09-30 83 views
0

我正在使用vb.net構建一個工作流程,用於處理大量PDF文件。我需要做的一件事是在每個PDF文檔的第一頁的左下角放置一個條形碼。在PDF文檔中增加頁面大小以適應條形碼(itextsharp)

我已經制定了放置條形碼的代碼,但問題是它可能會覆蓋第一頁上的現有內容。

我想增加頁面大小並在第一頁的底部添加約40個像素的空白區域,以便放置條形碼。但我不知道如何做到這一點!

這裏是現有代碼:

Public Sub addBarcodeToPdf(byval openPDFpath as string, byval savePDFpath as string, ByVal barcode As String) 

    Dim myPdf As PdfReader 

    Try 
     myPdf = New PdfReader(openPDFpath) 
    Catch ex As Exception 
     logEvent("LOAD PDF EXCEPTION " & ex.Message) 
    End Try 

    Dim stamper As PdfStamper = New PdfStamper(myPDF, New FileStream(savePDFpath, FileMode.Create)) 

    Dim over As PdfContentByte = stamper.GetOverContent(1) 

    Dim textFont As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED) 
    Dim BarcodeFont As BaseFont = BaseFont.CreateFont("c:\windows\fonts\FRE3OF9X.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED) 

    over.SetColorFill(BaseColor.BLACK) 
    over.BeginText() 
    over.SetFontAndSize(textFont, 15) 
    over.SetTextMatrix(30, 3) 
    over.ShowText(barcode) 
    over.EndText() 

    over.BeginText() 
    over.SetFontAndSize(BarcodeFont, 28) 
    over.SetTextMatrix(5, 16) 
    over.ShowText("*" & barcode & "*") 
    over.EndText() 

    stamper.Close() 
    myPdf.Close() 
End Sub 

預先感謝您! /M

+1

這是一個幾乎完全複製了Java問題[如何擴展PDF的頁面大小添加水印?] (http://stackoverflow.com/questions/29775893/how-to-extend-the-page-size-of-a-pdf-to-add-a-watermark)請將該示例的代碼轉換爲VB代碼並你有你的答案。這個例子也可以在[官方網站]上找到(http://developers.itextpdf.com/question/how-extend-page-size-pdf-add-watermark)。請務必先諮詢官方網站,然後詢問是否存在某些您不明白的問題。 –

回答

0

謝謝布魯諾指着我在正確的方向。我還沒有完成音量測試,但我設法讓它在一個PDF樣本上工作。只是改變媒體框是不夠的(我只能使頁面尺寸變小),但當更換相同的Thime時,我得到了我期待的結果。

代碼在VB低於參考

Dim myPdf As PdfReader 

    Try 
     myPdf = New PdfReader(openPDFpath) 
    Catch ex As Exception 
     logEvent("LOAD PDF EXCEPTION " & ex.Message) 
    End Try 

    Dim stamper As PdfStamper = New PdfStamper(myPdf, New FileStream(savePDFpath, FileMode.Create)) 

    Dim pageDict As PdfDictionary = myPdf.GetPageN(1) 
    Dim mediabox As PdfArray = pageDict.GetAsArray(PdfName.MEDIABOX) 
    Dim cropbox As PdfArray = pageDict.GetAsArray(PdfName.CROPBOX) 

    Dim pixelsToAdd As Integer = -40 

    mediabox.Set(1, New PdfNumber(pixelsToAdd)) 
    cropbox.Set(1, New PdfNumber(pixelsToAdd)) 

    stamper.Close() 
    myPdf.Close() 

感謝 /M

+1

而不是簡單地將底部設置爲-40,您最好從當前最低值減去40。通常它是0開始,但有時它不同,然後你的代碼可能會導致不需要的東西。 – mkl