IT業界のすみっこ暮らし

ふと気がついたときの記録

VBでPDF作成

'Add Reference itextsharp.dll
'Imports iTextSharp.text
'Imports iTextSharp.text.pdf

Using pdfStream As MemoryStream = New MemoryStream()
    Dim document As iTextSharp.text.Document = New iTextSharp.text.Document()
    Dim writer As PdfWriter = PdfWriter.GetInstance(document, pdfStream)

   'フォントの準備
    FontFactory.RegisterDirectories()
   'フォント設定
    Dim baseFont As iTextSharp.text.Font = FontFactory.GetFont("MS Gothic", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED, 8.0F)

    document.Open()
    Dim PdfContentByte As PdfContentByte = writer.DirectContent
    PdfContentByte.BeginText()
    PdfContentByte.SetFontAndSize(baseFont.BaseFont, 8)
    PdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "TEST1", 515.0F, 800.0F, 0)
    PdfContentByte.EndText()

    document.NewPage()
    PdfContentByte.BeginText()
    PdfContentByte.SetFontAndSize(baseFont.BaseFont, 8)
    PdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "TEST2", 515.0F, 800.0F, 0)
    PdfContentByte.EndText()

    document.Close()
    Dim fileName As String = String.Format("{0:yyyyMMddHHmmssfff}", DateTime.Now)

   'ファイル作成
    File.WriteAllBytes("D:\" + String.Format("{0}.pdf", fileName), pdfStream.ToArray())

End Using
広告を非表示にする