如何使用 itextsharp 修改 pdf 文件的修剪框

发布于 2024-10-14 22:28:46 字数 792 浏览 2 评论 0原文

我有一个现成的 PDF,我需要使用 SetBoxSize 修改裁切框、出血框并使用 setPDFXConformance。有办法做到这一点吗?

我尝试过使用 stamper.Writer,但它并不关心我在

2011.02.01 那里设置的内容。
我们用 Acrobat Pro 对其进行了测试,结果显示修剪框未定义。看来压模作者的方法/属性不会影响生成的 pdf。以下是源文件和结果文件: http://stemaweb.hu/pdfs.zip

我的代码:

PdfReader reader = new PdfReader(@"c:\source.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileStream(@"c:\result.pdf", FileMode.Create));
stamper.Writer.SetPageSize(PageSize.A4);
stamper.Writer.PDFXConformance = PdfWriter.PDFX32002;
stamper.Writer.SetBoxSize("trim", new iTextSharp.text.Rectangle(20, 20, 100, 100));
PdfContentByte cb = stamper.GetOverContent(1);
/*drawing*/
stamper.Close();

因为这些框不可见,我尝试与作者一起修改页面大小,但也没有做任何事情。

I have a ready made PDF, and I would need to modify the trimbox, bleedbox with SetBoxSize and use the setPDFXConformance. Is there a way to do this?

I've tried with stamper.Writer, but it doesn't care about what I set there

2011.02.01.
We've tested it with Acrobat Pro, and it said that the trimbox was not defined. It seems the the stamper's writer's methods/properties don't effect the resulting pdf. Here are the source and result files: http://stemaweb.hu/pdfs.zip

my code:

PdfReader reader = new PdfReader(@"c:\source.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileStream(@"c:\result.pdf", FileMode.Create));
stamper.Writer.SetPageSize(PageSize.A4);
stamper.Writer.PDFXConformance = PdfWriter.PDFX32002;
stamper.Writer.SetBoxSize("trim", new iTextSharp.text.Rectangle(20, 20, 100, 100));
PdfContentByte cb = stamper.GetOverContent(1);
/*drawing*/
stamper.Close();

Because the boxes are not visible, I tried to modify the pagesize with the writer but that didn't do anything either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不爱素颜 2024-10-21 22:28:46

SetPDFXConformance 不会将“普通”PDF 转换为 PDF/X pdf。 SetPDFXConformance 实际上只是用于文档生成,如果您执行明显不符合规范的操作,则会导致 iText 抛出异常。

“它不关心我在那里设置的内容”。修剪框和出血框不是您在 Reader 中可以直观看到的内容。您如何测试它们?

您可以发布一些代码以及输出 PDF 的链接吗?


啊。您正在使用 stamper.Writer。在这种情况下,效果不太好。所有页面级别,通过 PdfStamper 的良好支持操作都将采用页码或页面的 PdfDictionary 作为参数。 SetBoxSize 只需要一个字符串 &一个矩形,这就是你的线索。

实际上,“在引擎盖下”默认返回 PdfWriter.setBoxSize...它仅用于创建 PDF,而不是修改现有页面。

所以:您需要使用低级 PDF 对象进行您想要的更改。没问题:

for (int i = 1; i <= myReader.getNumberOfPages(); ++i) {
  PdfDictionary pageDict = myREADER_YES_READER.getPageN(i);

  PdfRectangle newBox = new PdfRectangle( 20, 20, 100, 100 );
  pageDict.put(PdfName.TRIMBOX, newBox);

  newBox = new PdfRectangle( PageSize.A4 );
  pageDict.put(PdfName.MEDIABOX, newBox );
}

/* drawing */

stamper.close();

至于 PDFX32002 一致性,我认为您必须深入代码才能弄清楚到底需要什么。 Writer.PDFXConformanceWriter 的另一个方面,它仅在生成 PDF 时起作用,而不是修改现有 PDF。

好消息是 PdfXConformanceImp 是一个公共类。坏消息是它仅由 PdfWriter 和 PdfContentByte 在内部使用......嘿。您当前的代码的行为发生了一些变化(还不够)。具体来说,如果您尝试该 PdfContentByte 中不允许的操作,您将收到 PdfXConformanceException,其中包含描述您所违反的限制的消息。例如,尝试添加可选内容组(层)会抛出异常。

啊。那还不错。或许。试试这个:

PDFXConformanceImp pdfx = new PDFXConformanceImp();
pdfx.setConformance(PdfWriter.PDFX32002);

pdfx.commpleteInfoDictionary(stamper.Writer.getInfo());
pdfx.completeExtraCatalog(stamper.Writer.getExtraCatalog());

stamper.close();

如果您删除 stamper.Writer.PDFXConformance = PdfWriter.PDFX32002;,那么当您在 contentByte 中执行禁止的操作时,您将不会遇到异常。除此之外,我认为这并不重要。

嗯..这不是完整的解决方案。 extraCatalog 中的 OutputIntents 也合并到主目录中。也许这会起作用:

//replace the completeExtraCatalog call above with this
pdfx.completeExtraCatalog(myReader.getCatalog());

祝你好运。

SetPDFXConformance won't turn a "normal" PDF into a PDF/X pdf. SetPDFXConformance is really just for document generation, causing iText to throw an exception if you do something blatantly off spec.

"it doesn't care about what I set there". Trim and bleed boxes are not something you can see visually in Reader. How are you testing for them?

Could you post some code, and a link to your output PDF?


Ah. You're using stamper.Writer. In this case, that doesn't work out so well. All the page level, Well Supported Actions via PdfStamper will take a page number or page's PdfDictionary as an argument. SetBoxSize just takes a string & a rectangle, so that's youre clue.

Going "under the hood" as you are is actually defaulting back to PdfWriter.setBoxSize... which is only for creating PDFs, not modifying an existing page.

So: You need to use the low-level PDF objects make the changes you want. No Problemo:

for (int i = 1; i <= myReader.getNumberOfPages(); ++i) {
  PdfDictionary pageDict = myREADER_YES_READER.getPageN(i);

  PdfRectangle newBox = new PdfRectangle( 20, 20, 100, 100 );
  pageDict.put(PdfName.TRIMBOX, newBox);

  newBox = new PdfRectangle( PageSize.A4 );
  pageDict.put(PdfName.MEDIABOX, newBox );
}

/* drawing */

stamper.close();

As to the PDFX32002 conformance, I think you're going to have to go code diving to figure out exactly what is needed. Writer.PDFXConformance is another aspect of Writer that only works when generating a PDF, not modifying an existing one.

The good news is that PdfXConformanceImp is a public class. The bad news is that its only used internally by PdfWriter and PdfContentByte... hey. You are getting some changes in behavior with your present code (just not enough). Specifically, if you try something that isn't allowed within that PdfContentByte, you'll get a PdfXConformanceException with message describing the restriction you've violated. Trying to add an optional content group (layer) would throw for example.

Ah. That's not so bad. MAYBE. Try this:

PDFXConformanceImp pdfx = new PDFXConformanceImp();
pdfx.setConformance(PdfWriter.PDFX32002);

pdfx.commpleteInfoDictionary(stamper.Writer.getInfo());
pdfx.completeExtraCatalog(stamper.Writer.getExtraCatalog());

stamper.close();

If you drop stamper.Writer.PDFXConformance = PdfWriter.PDFX32002;, you won't get exceptions when you do something Forbidden in your contentByte. Other than that, I don't think it'll matter.

Hmm.. That's not the whole solution. The OutputIntents from the extraCatalog are merged into the main catalog as well. Perhaps this will work:

//replace the completeExtraCatalog call above with this
pdfx.completeExtraCatalog(myReader.getCatalog());

I wish you luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文