0

I'm using vb.net to build a workflow where I'm processing a number of PDF files. One of the things I need to do is to place a barcode in the bottom left corner of the first page on each PDF document.

I already worked out the code to place the barcode but the problem is that it may cover existing content on the first page.

I want to increase the page size and add about 40 pixels of white space at the bottom of the first page where I can place the barcode. But I dont know how to do this!

Here is the existing code:

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

Thank you in advance! /M

Uzmark
  • 1
  • 1
    This is an almost exact duplicate of the Java question [How to extend the page size of a PDF to add a watermark?](http://stackoverflow.com/questions/29775893/how-to-extend-the-page-size-of-a-pdf-to-add-a-watermark) Please convert the code of that example to VB code and you have your answer. This example is also available on the [official web site](http://developers.itextpdf.com/question/how-extend-page-size-pdf-add-watermark). Always consult the official web site first, then ask a question if there's something you don't understand. – Bruno Lowagie Sep 30 '16 at 08:08

1 Answers1

0

Thank you Bruno for pointing me in the right direction. I haven't done volume testing yet but I managed to get it work on one PDF sample. Just changing the mediabox was not enough (I could only make the page size smaller) but when changing the cropbox at the same thime I got the results I was looking for.

Code in VB below for reference

    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()

Thanks /M

Uzmark
  • 1
  • 1
    Instead of simply setting the bottom to -40, you had better subtract 40 from the current bottom value. Often it is 0 to start with but sometimes it differs, and then your code may result in something not desired. – mkl Sep 30 '16 at 12:48