PDF处理完成,没有错误,但无法手动打开PDF,使用iTextSharp和C#。

huangapple go评论70阅读模式
英文:

pdf processing don with no error but the pdf cannot open manually itextSharp c#

问题

我有一个模板PDF(1页),我需要:

  • 复制到另一个位置并进行编辑(放置图片)
  • 根据图像列表计数复制页面
  • 模板应该作为背景,而图像位于内容之上

-> 调试模式成功运行,但无法打开新的PDF文件,错误消息是图像:

以下是源代码:

//开始编辑PDF
using (Stream inputPdfStream = new FileStream(filepathsrc, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outputPdfStream = new FileStream(filepathOut, FileMode.Create, FileAccess.Write, FileShare.None))
{
    //读取模板
    PdfReader reader = new PdfReader(filepathsrc);
    //将模板复制到原始文件
    PdfStamper stamper = new PdfStamper(reader, outputPdfStream);

    //将模板设置为内容下方
    PdfImportedPage Page = stamper.GetImportedPage(reader, 1);
    PdfContentByte background = stamper.GetUnderContent(1);
    background.AddTemplate(Page, 0, 0);

    //创建页面
    int originalpages = imageBytes.Count;
    for (int page = 1; page <= originalpages; page++)
    {
        iTextSharp.text.Rectangle rect = reader.GetCropBox(1);
        stamper.InsertPage(page , rect);
    }
    //复制模板到第一页
    for (int page = 2; page <= originalpages; page++)
    {
        stamper.ReplacePage(reader, page - 1, page);
    }

    //放置图像
    for (int i = 0; i < imageBytes.Count; i++)
    {
        int pageNum = i + 1;
        PdfContentByte pdfContentByte = stamper.GetOverContent(pageNum);
        pdfContentByte.AddImage(images[i]);
    }
}

我是一名初级开发人员,感谢您解释我的代码问题。

英文:

I have a template pdf (1 page) that I need to

  • copy to another and editing (put images on it)
  • duplicate the page depending on image list count
  • the template should be background while the image over content

-> the debugging mode run successfully but the new pdf file cant open, error msg image:
PDF处理完成,没有错误,但无法手动打开PDF,使用iTextSharp和C#。

here the source code:

//start editing the pdf 
        using (Stream inputPdfStream = new FileStream(filepathsrc, FileMode.Open, FileAccess.Read, FileShare.Read))
        using (Stream outputPdfStream = new FileStream(filepathOut, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            //read the template
            PdfReader reader = new PdfReader(filepathsrc);
            //copy the template the original
            PdfStamper stamper = new PdfStamper(reader, outputPdfStream);

            //set template under content
            PdfImportedPage Page = stamper.GetImportedPage(reader, 1);
            PdfContentByte background = stamper.GetUnderContent(1);
            background.AddTemplate(Page, 0, 0);

            //create pages
            int originalpages = imageBytes.Count;
            for (int page = 1; page &lt;= originalpages; page++)
            {
                iTextSharp.text.Rectangle rect = reader.GetCropBox(1);
                stamper.InsertPage(page , rect);
            }
            //duplicate template for page one
            for (int page = 2; page &lt;= originalpages; page++)
            {
                stamper.ReplacePage(reader, page - 1, page);
            }

            //place images
            for (int i = 0; i &lt; imageBytes.Count; i++)
            {
                int pageNum = i + 1;
                PdfContentByte pdfContentByte = stamper.GetOverContent(pageNum);
                pdfContentByte.AddImage(images[i]);
                
            }

           
        }

I'm junior developer I will be thankful for explanation the issue on my code.

答案1

得分: 2

我忘记关闭pdfstamper,就像@mkl说的那样。

英文:

like what @mkl said, I forget to close pdfstamper .

huangapple
  • 本文由 发表于 2023年7月28日 00:06:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定