英文:
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:
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 <= originalpages; page++)
{
iTextSharp.text.Rectangle rect = reader.GetCropBox(1);
stamper.InsertPage(page , rect);
}
//duplicate template for page one
for (int page = 2; page <= originalpages; page++)
{
stamper.ReplacePage(reader, page - 1, page);
}
//place images
for (int i = 0; i < 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论