2016-08-13 18 views
0

私は以下のコードをボタンクリックで得ることができます。既存の各.pdfにイメージを追加し、新しい.pdfをいくつか組み合わせて1つの.pdfを作成します。繰り返しますが、この部分は素晴らしいです。イメージとテキストを既存の.pdfに追加するVB.netでiTextを使用する

問題は、既存のコードを残しておき、画像が追加されるコード内の同じ場所にある各ページにテキストを追加することです。既存の.pdfにテキストを追加する方法の例をたくさん読んだことがありますが、私の未経験者のために、既存のコードでその例をどのように動作させるかを理解できません。 VB.netの使用。

"テキストの例"という単純な行を追加して、ページ(300,300)に配置したいと考えています。

ご協力いただければ幸いです。

Dim tempFilename = IO.Path.GetTempFileName() 
    Dim tempFile As New IO.FileStream(tempFilename, IO.FileMode.Create) 

    ' Set up iTextSharp document to hold merged PDF 
    Dim mergedDocument As New iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER) 
    Dim copier As New iTextSharp.text.pdf.PdfCopy(mergedDocument, tempFile) 
    mergedDocument.Open() 

    Dim pic1 As String = "C:\xxx\xxxx\xxx.png" 

      Using inputPdfStream As IO.Stream = New IO.FileStream(Server.MapPath(".") + "/xxx.pdf", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read) 
       Using inputImageStream As IO.Stream = New IO.FileStream(pic1, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read) 
        Using outputPdfStream As IO.Stream = New IO.FileStream(Server.MapPath(".") + "/xxx2.pdf", IO.FileMode.Create, IO.FileAccess.ReadWrite, IO.FileShare.None) 
         Dim reader1 = New iTextSharp.text.pdf.PdfReader(inputPdfStream) 
         Dim stamper = New iTextSharp.text.pdf.PdfStamper(reader1, outputPdfStream) 
         Dim pdfContentByte = stamper.GetOverContent(1) 

         Dim image__1 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(inputImageStream) 
         image__1.SetAbsolutePosition(527, 710) 
         image__1.ScaleAbsolute(60, 60) 
         pdfContentByte.AddImage(image__1) 
         stamper.Close() 
         reader1.Close() 
         outputPdfStream.Close() 
         inputImageStream.Close() 
         inputPdfStream.Close() 
         outputPdfStream.Dispose() 
        End Using 
       End Using 
      End Using 

      Dim reader As New iTextSharp.text.pdf.PdfReader(New iTextSharp.text.pdf.RandomAccessFileOrArray(Server.MapPath(".") + "/yyy/xxx3".pdf", True), Nothing) 

      For pageNum = 1 To reader.NumberOfPages 
       copier.AddPage(copier.GetImportedPage(reader, pageNum)) 
      Next 

      PTime = PTime + 1 
     Loop 

     mergedDocument.Close() 
     tempFile.Dispose() 

答えて

0

多くの試行錯誤の後、私は次のコードを追加することによって動作するようになった。

Dim bf As iTextSharp.text.pdf.BaseFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED) 
         pdfContentByte.SetColorFill(iTextSharp.text.BaseColor.DARK_GRAY) 
         pdfContentByte.SetFontAndSize(bf, 8) 
         pdfContentByte.BeginText() 
         Dim strX As String = "Here" 
         pdfContentByte.ShowTextAligned(1, strX, 500, 500, 0) 
         pdfContentByte.EndText() 
関連する問題