【问题标题】:Increase page size in PDF documents to fit barcode (itextsharp)增加 PDF 文档中的页面大小以适合条形码 (itextsharp)
【发布时间】:2016-09-30 07:47:56
【问题描述】:

我正在使用 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

【问题讨论】:

标签: vb.net itext


【解决方案1】:

感谢布鲁诺为我指明了正确的方向。我还没有进行批量测试,但我设法让它在一个 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

【讨论】:

  • 与其简单地将底部设置为 -40,不如从当前底部值中减去 40。通常它开始时是 0,但有时它会有所不同,然后您的代码可能会导致不想要的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 2011-05-06
相关资源
最近更新 更多