增加PDF文档中的页面大小以适合条形码(itextsharp)

时间:2016-09-30 07:47:56

标签: vb.net itext

我正在使用vb.net构建一个工作流程,我处理大量PDF文件。我需要做的一件事就是在每个PDF文档的第一页左下角放置一个条形码。

我已经找到了放置条形码的代码,但问题是它可能会覆盖第一页上的现有内容。

我想增加页面大小,并在第一页底部添加大约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 个答案:

答案 0 :(得分:0)

感谢布鲁诺指出我正确的方向。我还没有进行过卷测试,但我设法让它在一个PDF样本上运行。只是更改媒体盒是不够的(我只能使页面尺寸更小)但是当我在同一时间更改庄稼时,我得到了我正在寻找的结果。

以下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