我正在使用由单个 PdfTable 组成的 ITextSharp 创建 PDF。不幸的是,对于特定数据集,由于创建了大量 PdfPCell,我遇到了内存不足异常(我已经分析了内存使用情况 - 我有近 1/2 百万个单元格!)
在这种情况下,有没有办法减少内存使用?我试过在不同点(每行之后)冲洗和完全压缩
PdfWriter 基于 FileStream
代码看起来很像这样:
Document document = Document();
FileStream stream = new FileStream(fileName,FileMode.Create);
pdfWriter = PdfWriter.GetInstance(document, stream);
document.Open();
PdfPTable table = new PdfPTable(nbColumnToDisplay);
foreach (GridViewRow row in gridView.Rows)
{
j = 0;
for (int i = 0; i < gridView.HeaderRow.Cells.Count; i++)
{
PdfPCell cell = new PdfPCell( new Phrase( text) );
table.AddCell(cell);
}
}
document.Add(table);
document.Close();
Run Code Online (Sandbox Code Playgroud) 尝试使用以下代码编写PDF文档:
document = new Document();
PdfWriter writer = null; ;
try
{
writer = PdfWriter.GetInstance(document, new FileStream(@"E:\mergFiles", FileMode.Create));
}
catch (Exception xc)
{ }
Run Code Online (Sandbox Code Playgroud)
我得到一个例外:
{System.UnauthorizedAccessException: Access to the path 'E:\mergFiles' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) …
Run Code Online (Sandbox Code Playgroud) 以下是我的代码.我的目标是创建一个PDF,其中最终用户可以做任何他们想做的事情除了复制文本(选择文本和COPY到记事本).任何人都可以解释第18行应该有哪些代码?我允许打印但不允许ALLOW_COPY)
我的印象是,下面的代码足以限制用户这样做,但"事实上"他们能够复制所选文本并将内容粘贴到记事本中.
非常感谢!
package com.itext;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.IOException;
import com.itextpdf.text.DocumentException;
public class ProtectMePdf
{
public static void main(String[] args) throws IOException, DocumentException
{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/Users/adhg/protectMe.pdf"));
//LINE 18: what's wrong with this line? - if you run the code you will be able to copy the selected text.
writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
writer.createXmpMetadata();
document.open();
document.add(new Paragraph("Protect me! if you can do copy-paste of this message …
Run Code Online (Sandbox Code Playgroud)