2011-12-22 8 views
3

これを行うにはいくつかの方法を試みましたが、まだそれを得ることはできません。 iTextSharpは画像がテキストの上に現れるように2パスの状況が必要です。 メモリストリームを使用してこれを実行しようとしていますが、エラーが発生し続けます。PDF itextSharpを使用すると、PDF文書を作成中にテキストの上にイメージを置くことができます

Public Function createDoc(ByRef reqResponse As HttpResponse) As Boolean 

     Dim m As System.IO.MemoryStream = New System.IO.MemoryStream() 
     Dim document As Document = New Document() 
     Dim writer As PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(document, m) 
     document.Open() 
     document.Add(New Paragraph(DateTime.Now.ToString())) 
     document.Add(New Paragraph(DateTime.Now.ToString())) 
     document.Add(New Paragraph(DateTime.Now.ToString())) 
     document.Add(New Paragraph(DateTime.Now.ToString())) 
     document.Add(New Paragraph(DateTime.Now.ToString())) 
     document.Add(New Paragraph(DateTime.Now.ToString())) 
     document.Add(New Paragraph(DateTime.Now.ToString())) 
     document.Close() 
     writer.Flush() 
     writer.Flush() 
     'yes; I get the pdf if this is the last statement 
     'reqResponse.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length) 

     'this statment does not work it says the stream is closed 
     'm.Position = 0 
     Dim Reader As PdfReader = New PdfReader(m) 
     'Dim rm As MemoryStream = New MemoryStream(m.GetBuffer(), 0, m.GetBuffer().Length) 
     Dim PdfStamper As PdfStamper = New PdfStamper(Reader, reqResponse.OutputStream) 
     Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing 
     cb = PdfStamper.GetOverContent(1) 
     Dim locMyImage As System.Drawing.Image = System.Drawing.Image.FromStream(zproProduceWhiteImageToCovertBarCodeNumbers()) 
     Dim BImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(locMyImage, iTextSharp.text.BaseColor.CYAN) 
     Dim overContent As PdfContentByte = PdfStamper.GetOverContent(1) 
     BImage.SetAbsolutePosition(5, 5) 
     overContent.AddImage(BImage) 
     PdfStamper.FormFlattening = True 
     PdfStamper.Close() 

     'rm.Flush() 
     'rm.Close() 
     'Dim data As Byte() = rm.ToArray() 

     'reqResponse.Clear() 
     'Dim finalMs As MemoryStream = New MemoryStream(data) 
     'reqResponse.ContentType = "application/pdf" 
     'reqResponse.AddHeader("content-disposition", "attachment;filename=labtest.pdf") 
     'reqResponse.Buffer = True 
     'finalMs.WriteTo(reqResponse.OutputStream) 
     'reqResponse.End() 


     'Dim data As Byte() = rm.ToArray() 
     'reqResponse.OutputStream.Write(data, 0, data.Length) 

     ''Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length); 
     ''Response.OutputStream.Flush(); 
     ''Response.OutputStream.Close(); 
     ''Response.End(); 


     HttpContext.Current.ApplicationInstance.CompleteRequest() 
     Return True 
    End Function 

参照: Put text on top of an image?

さがすエンジン参照:上にitextpdf itextsharp画像と背景PDF 画像の重複と同じ色で画像を使用してPDF文書に 白飛びテキストトップ

おかげでテキスト itextpdf画像の上にテキストホワイトアウト itextsharpの場所の画像、の ダグLubeyルイジアナ

Example of my final product

+0

[PDF改訂](http://blogs.adobe.com/security/2009/12/how_to_properly_redact_pdf_fil.html)のように見えます。うまくいけばそれはあなたの意図ではない。 iText [Sharp]でさえこのようにして作成されたPDFからテキストを回復することができます。 [One link](http://www.mail-archive.com/[email protected]/msg58936.html)および[another](http://www.mail-archive.com/itext- [email protected]/msg57640.html)をメーリングリストから削除して、**適切な**修正作業のために別のツールを使用する方が簡単であることを説明しています。 (両方のスレッドで回答した人がAdobeのために働いていて、PDF **のエキスパート**です) – kuujinbo

答えて

8

あなたはかなり簡単にこれを行うことができます。 Documentオブジェクトは、PDFモデルの内部構造の多くを抽象化するヘルパーオブジェクトであり、ほとんどの場合、コンテンツを流したいと仮定し、そのテキストがイメージよりも上にあると想定しています。これを回避したい場合は、代わりにPdfWriterオブジェクトを直接話すことができます。 DirectContentDirectContentUnderの2つのプロパティがあり、どちらもイメージの絶対位置を設定するために使用できるAddImage()というメソッドがあります。 DirectContentは既存コンテンツの上にあり、DirectContentUnderはその下にあります。例のコードを参照してください:

ウェブ上でこれを行うように見えるので、使用しているストリームにこれを適応させる必要がありますが、それは非常に簡単です。

ワンノート、NEVERコールGetBuffer()常にMemoryStreamに使用ToArray()。前者のメソッドには、PDFを破損させる可能性のある未初期化バイトが含まれています。

''//File that we are creating 
    Dim OutputFile As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf") 
    ''//Image to place 
    Dim SampleImage As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "SampleImage.jpg") 

    ''//Standard PDF creation setup 
    Using FS As New FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None) 
     Using Doc As New Document(PageSize.LETTER) 
      Using writer = PdfWriter.GetInstance(Doc, FS) 

       ''//Open the document for writing 
       Doc.Open() 
       ''//Add a simple paragraph 
       Doc.Add(New Paragraph("Hello world")) 

       ''//Create an image object 
       Dim Img = iTextSharp.text.Image.GetInstance(SampleImage) 
       ''//Give it an absolute position in the top left corner of the document (remembering that 0,0 is bottom left, not top left) 
       Img.SetAbsolutePosition(0, Doc.PageSize.Height - Img.Height) 
       ''//Add it directly to the raw pdfwriter instead of the document helper. DirectContent is above and DirectContentUnder is below 
       writer.DirectContent.AddImage(Img) 

       ''//Close the document 
       Doc.Close() 
      End Using 
     End Using 
    End Using 
+0

ありがとうございました...ありがとうございました...ありがとうございます...サンタは早く来ました。 –

関連する問題