第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 IText 7 從 html 轉(zhuǎn)換生成一個(gè)包含多個(gè)頁面的 pdf 文檔

使用 IText 7 從 html 轉(zhuǎn)換生成一個(gè)包含多個(gè)頁面的 pdf 文檔

C#
犯罪嫌疑人X 2023-08-20 10:08:11
我正在使用IText 7,我已經(jīng)能夠獲取一個(gè) html 頁面并為該頁面生成 pdf,但我需要從多個(gè) html 頁面生成一個(gè) pdf 文檔并按頁面分隔。例如:我有Page1.html、Page2.html和Page3.html。我需要一個(gè) 3 頁的 pdf 文檔,第一頁的內(nèi)容為Page1.html,第二頁的內(nèi)容為Page2.html等等...這是我的代碼,它適用于一個(gè) html 頁面:ConverterProperties properties = new ConverterProperties();              PdfWriter writer = new PdfWriter(pdfRoot, new WriterProperties().SetFullCompressionMode(true));PdfDocument pdfDocument = new PdfDocument(writer);pdfDocument.AddEventHandler(PdfDocumentEvent.END_PAGE, new HeaderPdfEventHandler());HtmlConverter.ConvertToPdf(htmlContent, pdfDocument, properties);是否可以循環(huán)多個(gè) html 頁面,為每個(gè) html 頁面添加一個(gè)新頁面到 PdfDocument,然后僅生成一個(gè) pdf,每個(gè) html 頁面一頁?更新我一直在關(guān)注這個(gè)示例并嘗試將其從 Java 轉(zhuǎn)換為 C#,我嘗試使用PdfMerger并循環(huán) html 頁面...但我Cannot access a closed stream在這一行收到 Exception :temp = new PdfDocument(                    new PdfReader(new RandomAccessSourceFactory().CreateSource(baos), rp));看起來和ByteArrayOutputStream baos實(shí)例有關(guān)。有什么建議么?這是我當(dāng)前的代碼:foreach (var html in htmlList){    ByteArrayOutputStream baos = new ByteArrayOutputStream();    PdfDocument temp = new PdfDocument(new PdfWriter(baos));    HtmlConverter.ConvertToPdf(html, temp, properties);                  ReaderProperties rp = new ReaderProperties();    temp = new PdfDocument(        new PdfReader(new RandomAccessSourceFactory().CreateSource(baos), rp));    merger.Merge(temp, 1, temp.GetNumberOfPages());    temp.Close();}pdfDocument.Close();
查看完整描述

2 回答

?
搖曳的薔薇

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊

您正在使用RandomAccessSourceFactory并傳遞一個(gè)封閉的流,您在其中寫入了 PDF 文檔。RandomAccessSourceFactory需要一個(gè)已準(zhǔn)備好讀取的輸入流。


首先,您應(yīng)該使用MemoryStream.NET 世界原生的。ByteArrayOutputStream是出于內(nèi)部目的從 Java 移植的類(盡管它MemoryStream也擴(kuò)展了)。其次,你不必使用RandomAccessSourceFactory——有一個(gè)更簡單的方法。


您可以使用以下行MemoryStream從用于創(chuàng)建臨時(shí) PDF 的字節(jié)創(chuàng)建一個(gè)新實(shí)例:MemoryStream


baos = new MemoryStream(baos.ToArray());

作為補(bǔ)充說明,最好PdfMerger直接關(guān)閉實(shí)例而不是關(guān)閉文檔 - 關(guān)閉PdfMerger也會(huì)關(guān)閉基礎(chǔ)文檔。


總而言之,我們得到了以下有效的代碼:


foreach (var html in htmlList)

{

    MemoryStream baos = new MemoryStream();

    PdfDocument temp = new PdfDocument(new PdfWriter(baos));

    HtmlConverter.ConvertToPdf(html, temp, properties);              

    ReaderProperties rp = new ReaderProperties();

    baos = new MemoryStream(baos.ToArray());

    temp = new PdfDocument(new PdfReader(baos, rp));

    pdfMerger.Merge(temp, 1, temp.GetNumberOfPages());

    temp.Close();

}

pdfMerger.Close();


查看完整回答
反對(duì) 回復(fù) 2023-08-20
?
呼如林

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊

也許不是那么簡潔。我用“使用”。

private byte[] CreatePDF(string html)

? ? {

? ? ? ? byte[] binData;


? ? ? ? using (var workStream = new MemoryStream())

? ? ? ? {

? ? ? ? ? ? using (var pdfWriter = new PdfWriter(workStream))

? ? ? ? ? ? {

? ? ? ? ? ? ? ? //Create one pdf document

? ? ? ? ? ? ? ? using (var pdfDoc = new PdfDocument(pdfWriter))

? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ? ? pdfDoc.SetDefaultPageSize(iText.Kernel.Geom.PageSize.A4.Rotate());

? ? ? ? ? ? ? ? ? ? //Create one pdf merger

? ? ? ? ? ? ? ? ? ? var pdfMerger = new PdfMerger(pdfDoc);

? ? ? ? ? ? ? ? ? ? //Create two identical pdfs

? ? ? ? ? ? ? ? ? ? for (int i = 0; i < 2; i++)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? using (var newStream = new MemoryStream(CreateDocument(html)))

? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ReaderProperties rp = new ReaderProperties();

? ? ? ? ? ? ? ? ? ? ? ? ? ? using (var newPdf = new PdfDocument(new PdfReader(newStream, rp)))

? ? ? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pdfMerger.Merge(newPdf, 1, newPdf.GetNumberOfPages());

? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? binData = workStream.ToArray();

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return binData;

? ? }

創(chuàng)建 PDF


private byte[] CreateDocument(string html)

? ? {

? ? ? ? byte[] binData;


? ? ? ? using (var workStream = new MemoryStream())

? ? ? ? {

? ? ? ? ? ? using (var pdfWriter = new PdfWriter(workStream))

? ? ? ? ? ? {

? ? ? ? ? ? ? ? using (var pdfDoc = new PdfDocument(pdfWriter))

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? pdfDoc.SetDefaultPageSize(iText.Kernel.Geom.PageSize.A4.Rotate());


? ? ? ? ? ? ? ? ? ? ConverterProperties props = new ConverterProperties();

? ? ? ? ? ? ? ? ? ? using (var document = HtmlConverter.ConvertToDocument(html, pdfDoc, props))

? ? ? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? binData = workStream.ToArray();

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return binData;

? ? }


查看完整回答
反對(duì) 回復(fù) 2023-08-20
  • 2 回答
  • 0 關(guān)注
  • 786 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)