开发者

How to modify a pdf file's trimbox with itextsharp

开发者 https://www.devze.com 2023-02-07 20:07 出处:网络
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 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开发者_JAVA百科 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消