我正在使用iTextSharp和VB.Net將圖像印到PDF文檔上。 (因爲這不是我爲C#標記的特定語言)。我有兩個應用程序使用這個過程。PDF將文件流移動到文件流時,文件已損壞,無法修復
第一次使用來自存儲流的字節來在線顯示PDF 文檔。這件作品正在運作。
第二個使用相同的功能,但將PDF保存到 文件。這件作品會生成無效的PDF。
我已經看到一些類似的問題,但它們都是最初創建一個文檔並在代碼中有一個文檔對象。他們的記憶流從一開始就是腐敗的。我的代碼沒有文檔對象,我的原始內存流打開正常。
這裏是我的錯誤的地方:(我必須把緩衝區從M轉換成新的內存流,因爲在fillPDF功能默認爲關閉流壓模除非標記,否則)
Dim m As MemoryStream = PDFHelper.fillPDF(filename, Nothing, markers, "")
Dim m2 As New MemoryStream(m.GetBuffer, 0, m.GetBuffer.Length)
Dim f As FileStream = New FileStream("C:\temp.pdf", FileMode.Create)
m2.CopyTo(f, m.GetBuffer.Length)
m2.Close()
f.Close()
這是我在網站上成功使用它的方法之一。這一個不使用圖像,雖然一些其他類似的成功的地方在多個文檔上使用圖像,然後合併在一起。
Dim m As System.IO.MemoryStream = PDFHelper.fillPDF(filename, New Dictionary(Of String, String), New List(Of PDFHelper.PDfImage), "SAMPLE")
Dim data As Byte() = m.GetBuffer
Response.Clear()
//Send the file to the output stream
Response.Buffer = True
//Try and ensure the browser always opens the file and doesn’t just prompt to 「open/save」.
Response.AddHeader("Content-Length", data.Length.ToString())
Response.AddHeader("Content-Disposition", "inline; filename=" + "Sample")
Response.AddHeader("Expires", "0")
Response.AddHeader("Pragma", "cache")
Response.AddHeader("Cache-Control", "private")
//Set the output stream to the correct content type (PDF).
Response.ContentType = "application/pdf"
Response.AddHeader("Accept-Ranges", "bytes")
//Output the file
Response.BinaryWrite(data)
//Flushing the Response to display the serialized data to the client browser.
Response.Flush()
Try
Response.End()
Catch ex As Exception
Throw ex
End Try
下面是函數在我的公用事業類(PDFHelper.fillPDF)
Public Shared Function fillPDF(fileToFill As String, Optional fieldValues As Dictionary(Of String, String) = Nothing, Optional images As List(Of PDfImage) = Nothing, Optional watermarkText As String = "") As MemoryStream
Dim m As MemoryStream = New MemoryStream() // for storing the pdf
Dim reader As PdfReader = New PdfReader(fileToFill) // for reading the document
Dim outStamper As PdfStamper = New PdfStamper(reader, m) //for filling the document
If fieldValues IsNot Nothing Then
For Each kvp As KeyValuePair(Of String, String) In fieldValues
outStamper.AcroFields.SetField(kvp.Key, kvp.Value)
Next
End If
If images IsNot Nothing AndAlso images.Count > 0 Then //add all the images
For Each PDfImage In images
Dim img As iTextSharp.text.Image = Nothing //image to stamp
//set up the image (different for different cases
Select Case PDfImage.ImageType
//removed for brevity
End Select
Dim overContent As PdfContentByte = outStamper.GetOverContent(PDfImage.PageNumber) // specify page number for stamping
overContent.AddImage(img)
Next
End If
//add the water mark
If watermarkText <> "" Then
Dim underContent As iTextSharp.text.pdf.PdfContentByte = Nothing
Dim watermarkRect As iTextSharp.text.Rectangle = reader.GetPageSizeWithRotation(1)
//removed for brevity
End If
//flatten and close out
outStamper.FormFlattening = True
outStamper.SetFullCompression()
outStamper.Close()
reader.Close()
Return m
今天我會試試這個。謝謝! – user158017
這不是「完美」的答案,但我接受它,因爲它使我找到了正確的解決方案。我的問題是,我沒有意識到MemoryStream.GetBuffer和MemoryStream.ToArray之間的不同。根據你的代碼,我檢查了這個文檔:http://msdn.microsoft.com/en-us/library/system.io.memorystream.getbuffer.aspx,並意識到GetBuffer包含了無關的字節。我切換到File.WriteAllBytes(outputFile,m.ToArray),它工作完美!謝謝。 – user158017