2020年8月5日 星期三

vb.net 將iTextSharp所生成的pdf加入MetaData [Add MetaData into PDF]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Private Sub Export2PDF(pstrText As String)
        Dim bfnChinese As BaseFont = BaseFont.CreateFont("C:\Windows\Fonts\kaiu.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)

        Dim strFileName As String = "Modulo " & ConvertToRomanNumber(Val(TextBox4.Text)) & " Plana " & DateTime.Today.ToString("MM-dd") & ".pdf"
        Dim strFullPath As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, strFileName)

        Try
            Using stream As New FileStream(strFullPath, FileMode.Create)
                Dim pdfDoc As New Document(PageSize.LETTER.Rotate, 36.0F, 36.0F, 40.0F, 35.0F) '52.0F
                PdfWriter.GetInstance(pdfDoc, stream)

                Dim pdfHeader As New HeaderFooter(New Paragraph("姓名:________________________________________", New iTextSharp.text.Font(bfnChinese, 16.0F)), False)
                pdfHeader.Alignment = Element.ALIGN_LEFT
                pdfHeader.Border = 0
                pdfDoc.Header = pdfHeader

                Dim pdfFooter As New HeaderFooter(New Paragraph(DateTime.Today.ToString("yyyy-MM-dd"), New iTextSharp.text.Font(bfnChinese, 12.0F)), False)
                pdfFooter.Alignment = Element.ALIGN_RIGHT
                pdfFooter.Border = 0
                pdfDoc.Footer = pdfFooter


                pdfDoc.AddTitle(strFileName)
                pdfDoc.AddAuthor("SmallGCOk")
                pdfDoc.AddSubject("中文練習簿")
                pdfDoc.AddKeywords("SmallGCOk, 中文練習簿")
                pdfDoc.AddCreator("SmallGCOk")

                pdfDoc.Open()

                ProgressBar1.Value = 0
                ProgressBar1.Maximum = pstrText.Length

                For i As Integer = 0 To pstrText.Length - 1 Step 13
                    Dim intX As Single = 36
                    Dim strText As String = Mid(pstrText, i + 1, 13)

                    For j As Integer = 0 To strText.Length
                        Dim pdfPic As iTextSharp.text.Image
                        If j = 0 Then
                            pdfPic = iTextSharp.text.Image.GetInstance(strTitlePath)
                        Else
                            pdfPic = iTextSharp.text.Image.GetInstance(Path.Combine(strImagePath, Mid(strText, j, 1) & ".jpg"))
                        End If

                        pdfPic.ScaleToFit(100.0F, 509.0F)
                        pdfPic.SetAbsolutePosition(intX, 52.0F)
                        pdfDoc.Add(pdfPic)
                        intX += 51

                        ProgressBar1.Increment(1)
                    Next
                    pdfDoc.NewPage()
                Next

                pdfDoc.Close()
                stream.Close()

                Process.Start(strFullPath)
            End Using
        Catch ex As Exception
            MsgBox("發生不名錯誤。" & vbCrLf & "Error code: 0x0000000A" & vbCrLf & vbCrLf & "(請聯絡開發者)", 16, "錯誤")
        End Try
    End Sub

沒有留言:

張貼留言