프로젝트가 바뀌고 있으며 요구 사항이 변경되고 있으며 변하지 않는 프로그래머는 항상 키보드에 입력하고 있습니다 ...
PDF가 생성 된 후에는 텍스트, 그림과 같은 PDF에 다른 콘텐츠를 추가해야합니다.
몇 차례 실패한 후, 나는 마침내 코드를 작성하는 올바른 방법을 얻었습니다.
이 기록과 요약은 다음에 변경되지 않고 변경 사항에 적응할 수 있도록 다음과 같이 이동하십시오 : PDF 전체 가이드 생성
pdfreader reader = new pdfreader ( "e : //a.pdf"); pdfstamper stamper = new PdfStamper (Reader, New FileOutputStream ( "e : //b.pdf")); pdfcontentByte OverContent = stamper.getOverContent (1);
위의 코드는 원래 PDF에 컨텐츠를 추가하는 핵심 코드로 간주됩니다. 특정 프로세스는 다음과 같습니다
• 독자가 충분히주의를 기울이면 코드는 원래 A.PDF를 읽은 다음 B.PDF에 작성한 다음 B.PDF를 작동시키는 것입니다.
• 일부 독자들은 A를 읽은 다음 A에 쓰는 것이 불가능하다고 말할 수 있습니다. A는로드되었으며 읽을 때 수정할 수 없습니다.
• PDF의 서버 경로, 이전 이름, 새 이름, 유형을 포함하여 원래 PDF의 정보가 이미 데이터베이스에 저장되어 있기 때문에 마음에 들지 않습니다.
• PDF 이름이 여기에서 변경되어야하고 사람들은 후속 요구 사항이 어떻게 변경 될지 알고 있기 때문에 추가 데이터베이스 변경 작업이 발생합니다.
• 여기에 PDF에 컨텐츠를 추가하는 것이 시급하며 다른 것은 변경되지 않으므로 코드를 약간 조정했습니다.
FileUtil.FileChannelCopy (A.PDF, A + "TMP".pdf)); pdfreader reader = new pdfreader (a + "tmp".pdf); pdfstamper stamper = new PdfStamper (Reader, New FileOutputStream (A.PDF)); pdfcontentByte OverContent = stamper.getOverContent (1);
코드 흐름은 이와 같이됩니다
여기에서 파이프 라인 복사 파일을 소개하고, 복사하고, 복사를 읽은 다음 원래 PDF A에 다시 쓸 수 있습니다. 물론 복사 파일을 삭제해야합니다.
이 시점에서 후속 요구 사항이 어떻게 변경 되더라도 PDF의 다른 특성은 변경되지 않으며 침착하게 직면 할 수 있습니다.
파이프 라인 사본 코드는 다음과 같습니다.
pubpc 정적 void filechannelCopy (파일 소스, 파일 dest) {try {fileInputStream inputStream = new FileInputStream (소스); fileoutputStream outputStream = 새 FileOutputStream (dest); filechannel filechannepn = inputStream.getChannel (); // 해당 파일 채널을 가져옵니다. filechannel filechannelout = outputStream.getChannel (); // 해당 파일 채널을 가져옵니다. filechannepn.transferto (0, filechannepn.size (), filechannelout); filechannepn.close (); outputStream.close (); filechannelout.close (); } catch (예외 e) {e.printstacktrace (); }}전체 PDF 기타 콘텐츠 코드는 다음과 같습니다.
fileutil.filechannelCopy (새 파일 ( "e : //a.pdf"), 새 파일 ( "e : // a+"tmp ".pdf")); pdfreader reader = new pdfreader ( "e : // a+"tmp ".pdf"); pdfstamper stamper = new PdfStamper (Reader, New FileOutputStream ( "e : //a.pdf")); pdfcontentByte OverContent = stamper.getOverContent (1); // 텍스트 추가 basefont font = basefont.createFont ( "stsong-pght", "unigb-ucs2-h", basefont.not_embedded); 오버 컨텐츠 .begintext (); Overcontent.setfontandsize (font, 10); Overcontent.settextmatrix (200, 200); OverContent.showTextApgned (element.apgn_Center, "추가 할 텍스트", 580,530,0); OverContent.endText (); // 이미지 추가 pdfdictionary pdfdictionary = reader.getPagen (1); pdfobject pdfobject = pdfdictionary.get (new pdfname ( "mediabox")); pdfarray pdfarray = (pdfarray) pdfobject; image image = image.getInstance ( "d : //1.jpg"); image.setabsoluteposition (100,100); 오버 컨텐츠 .adimage (이미지); // red circle overcontent.setrgbcolorstroke (0xff, 0x00, 0x00); Overcontent.setpnewidth (5f); 오버 컨텐츠. ELPPSE (250, 450, 350, 550); OverContent.stroke (); stamper.close ();
PDF를 생성하기위한 위의 전체 안내서는 기존 PDF에 컨텐츠를 추가하는 구현 방법입니다. 편집자는 모든 콘텐츠를 귀하와 공유합니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.