标签: itextsharp

糟糕的性能使用ITextSharp将tif转换为pdf

简介:如何减少使用tifs转换为pdf所需的时间itextsharp

背景:我正在使用C#将一些相当大的tif转换为pdf itextsharp,并且我的性能非常糟糕.tif文件大约是50kb,有些文件最多有150个单独的tif文件(每个文件代表一个页面).对于一个132页的文档(~6500 kb),转换大约需要13分钟.在转换过程中,它运行的单CPU服务器运行在100%,这让我相信该进程是CPU绑定的.输出pdf文件为3.5 MB.我的尺寸很好,但对我来说时间似乎有点高.

码:

private void CombineAndConvertTif(IList<FileInfo> inputFiles, FileInfo outputFile)
{
    using (FileStream fs = new FileStream(outputFile.FullName, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
    {
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        PdfWriter writer = PdfWriter.GetInstance(document, fs);
        document.Open();
        PdfContentByte cb = writer.DirectContent;

        foreach (FileInfo inputFile in inputFiles)
        {
            using (Bitmap bm = new Bitmap(inputFile.FullName))
            {
                int total = bm.GetFrameCount(FrameDimension.Page);

                for (int k = 0; k < total; ++k)
                {
                    bm.SelectActiveFrame(FrameDimension.Page, k);
                    //Testing shows that this …
Run Code Online (Sandbox Code Playgroud)

c# performance itextsharp

6
推荐指数
1
解决办法
1万
查看次数

使用iTextSharp创建图像时,将图像水印添加到Pdf

不知道这是否可能.在创建pdf并将其保存在磁盘后,在添加水印时看到很多帖子.但在创建文档时如何添加图像水印.我知道如何将图像添加到文档中.但是我如何定位它以使其出现在页面末尾.

c# pdf asp.net watermark itextsharp

6
推荐指数
2
解决办法
2万
查看次数

iTextSharp从现有PDF模板生成PDF

我正在研究使用C#和iTextSharp创建一些东西的可行性,它可以采用PDF模板并用从数据库中检索的实际值替换各种占位符值.基本上是PDF邮件合并.我有iText的动作书,但它涵盖了很多我不需要的东西,我很难找到任何与我想做的事情有关的东西.我很乐意使用PDF字段作为占位符,只要合并/展平的表单看起来不像它有字段,输出文档应该看起来像邮件合并字母而不是已填写的表单.一个理想的世界我只想搜索PDF的文本内容,然后用正确的字段值替换文本占位符和邮件合并.

任何人都可以告诉我最好的方法,并指出我要使用的最有用的iTextSharp类的方向,或者如果你知道iText in Action预订了一个指向最有用的部分供我阅读.

c# pdf itextsharp

6
推荐指数
1
解决办法
1万
查看次数

itextsharp:如果没有设置行的边框底部,如何显示具有属性HeaderRows = 1的表的底线?

我使用了最后一个版本的itextsharp.

我使用属性HeaderRows = 1,这样如果有一个分页符,标题行将再次出现在下一页.

然后我们有内容的行,边框样式没有底线,如下所示:

 PdfPCell cell1 = null;
 cell1 = new PdfPCell(new Phrase(string.Format("{0}", c1), fn));
 cell1.Border = Rectangle.RIGHT_BORDER | Rectangle.LEFT_BORDER;
Run Code Online (Sandbox Code Playgroud)

如果存在分页,则不会显示表格的行底部,这不符合逻辑.即使内容行没有底部/顶部边框,PdfPTable本身也应该有边框(事实上它在代码中没有).

有任何想法吗?谢谢.

.net c# itextsharp

6
推荐指数
1
解决办法
3521
查看次数

iTextSharp生成PDF:如何将pdf发送到客户端并添加提示?

我使用iTextSharp生成了一个pdf,当它创建时,它会自动保存在我的代码中提供的位置,而不是在客户端的服务器上,当然也没有告诉用户任何东西.

我需要将它发送给客户端,我需要提示一个对话框询问用户他想要保存他的pdf的位置.

我该怎么办?

这是我的pdf代码:

using (MemoryStream myMemoryStream = new MemoryStream())
{
    Document document = new Document();
    PdfWriter PDFWriter = PdfWriter.GetInstance(document, myMemoryStream);

    document.AddHeader("header1", "HEADER1");


    document.Open();

      //..........

    document.Close();

    byte[] content = myMemoryStream.ToArray();

    // Write out PDF from memory stream.
    using (FileStream fs =      File.Create(HttpContext.Current.Server.MapPath("~\\report.pdf")))
    {
        fs.Write(content, 0, (int)content.Length);
    }
Run Code Online (Sandbox Code Playgroud)

编辑

这是我想要http://examples.extjs.eu/?ex=download的结果示例

感谢您的回复,我修改了我的代码:

HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AppendHeader( "Content-Disposition", "attachment; filename=test.pdf");


using (MemoryStream myMemoryStream = new MemoryStream())
{    
Document document = new Document();    
PdfWriter PDFWriter = PdfWriter.GetInstance(document, myMemoryStream);

document.AddHeader("Content-Disposition", "attachment; filename=wissalReport.pdf");

document.Open(); …
Run Code Online (Sandbox Code Playgroud)

c# pdf extjs prompt itextsharp

6
推荐指数
2
解决办法
3万
查看次数

使用iTextSharp将数据写入PDF效果很好,但Acrobat Reader在关闭文件时询问"是否要保存更改"

我正在使用iTextSharp 5.3.2.0将信息添加到包含W-2表单的现有PDF文件中.一切都运行良好,PDF文件在写入浏览器的响应流时看起来很棒; 但是,当用户完成查看PDF时,他会被问到"你想在关闭之前将更改保存到'W2.pdf'吗?" 每次他从网页上查看文档.

在试图缩小问题范围时,我实际上已经删除了所有修改但问题仍在继续.这是我的代码的简单版本,我的数据写入调用已注释掉:

PdfReader pdfReader = new PdfReader(dataSource.ReportTemplate);

using(MemoryStream outputStream = new MemoryStream())
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream))
{
   //dataSource.DrawDataFields(pdfStamper);
   pdfStamper.FormFlattening = true;
   return outputStream;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,"空"PDF被写入浏览器并且看起来不错,但是当我关闭Acrobat窗口时,我仍然会被问到"你想保存吗".

在这一点上,我认为源PDF文件有问题.但是,当我将PDF文件的原始字节发送回浏览器时,在使用下面的代码时,我不会被问到"你想保存"的问题.

byte[] bytes = File.ReadAllBytes(dataSource.ReportTemplate);

using (MemoryStream outputStream = new MemoryStream())
{
    outputStream.Write(bytes, 0, bytes.Length);
    return outputStream;
}
Run Code Online (Sandbox Code Playgroud)

我的结论是iTextSharp在打开它并将字节写入流中的过程中对PDF做了一些"坏事",但我是iTextSharp的新手,很容易丢失一些东西.

FWIW,这是我们正在讨论的Acobat Reader 10.1.4.

编辑:用作模板的原始PDF大小约为80K.如果我查看通过浏览器流式传输的临时文件,iTextSharp编写的PDF文件大约为150K.但是,当我对Acrobat Reader提出的"保存更改"问题回答"是"时,生成的文件再次大约为80K.iTextSharp肯定会对这个文件做一些意想不到的事情.

c# pdf-generation itextsharp

6
推荐指数
1
解决办法
6064
查看次数

'对象引用未设置为HTMLWorker解析器的对象实例'

Document document = new Document(PageSize.LETTER, 10, 10, 10, 10);
StringReader reader = new StringReader(edittedHTML);
HTMLWorker worker = new HTMLWorker(document);
string fileName = "test.pdf";
PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
document.Open();
worker.Parse(reader);
worker.EndDocument();
worker.Close();
document.Close();
Run Code Online (Sandbox Code Playgroud)

当程序运行到worker.Parse时,它会抛出错误,就像标题所说的那样.

edtted HTML是HTML页面的HTML字符串.

任何人都知道如何解决这个问题,或者出了什么问题?

堆栈跟踪:

at iTextSharp.text.html.simpleparser.HTMLWorker.StartElement(String tag, IDictionary`2 attrs)
at iTextSharp.text.xml.simpleparser.SimpleXMLParser.ProcessTag(Boolean start)
at iTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader)
at iTextSharp.text.xml.simpleparser.SimpleXMLParser.Parse(ISimpleXMLDocHandler doc, ISimpleXMLDocHandlerComment comment, TextReader r, Boolean html)
at iTextSharp.text.html.simpleparser.HTMLWorker.Parse(TextReader reader)
at TestPdfApplication.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\TLiu\Documents\Visual Studio 2010\Projects\TestPdfApplication\TestPdfApplication\Form1.cs:line 68

c# itextsharp

6
推荐指数
1
解决办法
2423
查看次数

用unicode字符填写pdf表单

我试图用c#插入一些unicode字符(阿拉伯语)到PDF格式我使用iTextSharp库但是当我插入字符并在PDF文件中保存字符时,unicode字符不会显示,直到我双击字符的位置应该出现.

string pdfTemplate = @"c:\po.pdf";
string newFile = @"g:\test\completed_fw4.pdf";
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.SetField("position", TextBox1.Text);
pdfStamper.FormFlattening = false;
// close the pdf
pdfStamper.Close(); 
Run Code Online (Sandbox Code Playgroud)

c# unicode itextsharp

6
推荐指数
1
解决办法
5352
查看次数

iTextSharp 4.1.6和5.x版本之间的区别

我们正在开发一个与我们的系统一起使用的Pdf解析器.要求是这样的,我们将所有信息存储在任何pdf文档上,并且应该能够复制文档(与原始文档的更改很少).

我们做了一些谷歌搜索,发现iTextSharp是我们目的的最佳伴侣.我们正在使用.net开发我们的项目.

您可能已经猜到了我在标题中提到要求比较特定版本的iTextSharp(4.1.6 vs 5.x).我们知道4.1.6是具有LGPL/MPL许可证的iTextSharp的最后一个版本.5.x版本是AGPL.

我们希望在选择LGPL版本之前对版本进行很好的比较,或者我们购买AGPL的许可证(我们不想发布我们的代码).

我做了一些浏览iTextSharp中的修订更改,但我想知道是否存在任何内容,在版本之间进行了很好的比较.

提前致谢!

pdf licensing itext itextsharp pdf-parsing

6
推荐指数
1
解决办法
1万
查看次数

iTextSharp ShowTextAligned锚点

我目前正在使用iTextSharp的ShowTextAligned方法成功地将文本添加到PDF .该方法看起来像这样(C#):

public void ShowTextAligned(
    int alignment,
    string text,
    float x,
    float y,
    float rotation
)
Run Code Online (Sandbox Code Playgroud)

但是,目前还不清楚我们制作的文本的锚点在哪里.我们提供xy,但这些是否对应于文本矩形的左上角,左下角或其他?这也受线间距的影响?

我查看了这个网站上的文档,但它不是很清楚.请参阅PdfContentByte类/ PdfContentByte方法/ ShowTextAligned方法.

c# pdf pdf-generation itextsharp

6
推荐指数
1
解决办法
8344
查看次数