iText7이 글꼴, 색상, 스타일을 유지하지만 텍스트 크기는 변경하는 페이지에서 텍스트 이동

새긴 ​​금

iText7로 텍스트를 이동하고 싶습니다. 페이지 어딘가에있을 수있는 소스 경계 상자가 있고 고정 된 위치 (너비 및 높이 포함)가있는 대상 경계 상자가 있습니다. 나는 같은 페이지에 머물 것이다. 소스 및 대상 상자가 겹칠 수 있습니다. 소스 경계 상자는 대상 상자보다 클 수도 있습니다. 이 경우 글꼴 크기를 줄여야합니다. 텍스트는 글꼴, 색상 등을 유지해야합니다.

거기에있다 컷 예를 붙여 iText를 웹 사이트에. 그러나 결과 pdf 파일에서 새 위치와 이전 위치에서 텍스트를 선택할 수 있습니다 (일반 pdf 리더에서만 시도). 텍스트를 이전 위치에서 선택할 수있는 것을 원하지 않습니다.

텍스트를 선택하고 새 위치에 배치하고 이전 위치에서 제거 할 수 있다고 생각했습니다. 후자의 경우 pdfSweep이 필요하지만 괜찮습니다. 새 위치에 텍스트를 추가하는 것은 문제가되지 않습니다. 텍스트에 다른 글꼴, 크기 등이 있더라도. iText 웹 사이트에는 많은 예제가 있습니다. 텍스트를 선택하는 유일한 방법은이 예제 와 같습니다 . 이것은 나에게 텍스트만을 제공합니다. 그러나 동일한 글꼴, 색상 등으로 대상 위치에 배치하려면 이러한 모든 정보도 필요합니다. pdf는 편집 용이 아닙니다. 이것은 종종 StackOverflow의 답변에서 언급됩니다. iText7으로이 작업을 수행하는 방법이 있습니까?

mkl

iText에는 페이지 콘텐츠, 특히 일부 직사각형의 모든 콘텐츠이동할 수있는 고급 API가 없습니다 . 한 가지 이유는 일반적으로 이것은 단순한 움직임이 아니기 때문일 수 있습니다. PDF에는 종종 더 큰 영역에 영향을 미치는 구조가 포함되어 있으며 이러한 구조는 단순히 이동해야하는 것이 아니라 복사해야하며 각 사본은 해당 영역으로 제한됩니다.

그러나 OP가 이미 고려한 pdfSweep 모듈에서 찾은 잘라 내기 및 붙여 넣기 예제를 기존 위치에서 텍스트를 선택할 수 없도록하는 솔루션 에 결합하는 것이 실제로 가능합니다 . 예를 들면 다음과 같습니다.

public void moveCleanSection(PdfReader pdfReader, String targetFile, int page, Rectangle from, Rectangle to) throws IOException
{
    LicenseKey.loadLicenseFile("itextkey-multiple-products.xml");

    ByteArrayOutputStream interimMain = new ByteArrayOutputStream();
    ByteArrayOutputStream interimPage = new ByteArrayOutputStream();
    ByteArrayOutputStream interimSection = new ByteArrayOutputStream();

    try (   PdfDocument pdfMainDocument = new PdfDocument(pdfReader);
            PdfDocument pdfPageDocument = new PdfDocument(new PdfWriter(interimPage)) )
    {
        pdfMainDocument.setCloseReader(false);
        pdfMainDocument.copyPagesTo(page, page, pdfPageDocument);
    }

    try (   PdfDocument pdfMainDocument = new PdfDocument(pdfReader, new PdfWriter(interimMain));
            PdfDocument pdfSectionDocument = new PdfDocument(new PdfReader(new ByteArrayInputStream(interimPage.toByteArray())), 
            new PdfWriter(interimSection))  )
    {

        List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();
        cleanUpLocations.add(new PdfCleanUpLocation(page, from, null));
        cleanUpLocations.add(new PdfCleanUpLocation(page, to, null));

        PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfMainDocument, cleanUpLocations);
        cleaner.cleanUp();

        cleanUpLocations = new ArrayList<PdfCleanUpLocation>();
        Rectangle mediaBox = pdfSectionDocument.getPage(1).getMediaBox();

        if (from.getTop() < mediaBox.getTop())
            cleanUpLocations.add(new PdfCleanUpLocation(1, new Rectangle(mediaBox.getLeft(), from.getTop(), mediaBox.getWidth(), mediaBox.getTop() - from.getTop()), null));
        if (from.getBottom() > mediaBox.getBottom())
            cleanUpLocations.add(new PdfCleanUpLocation(1, new Rectangle(mediaBox.getLeft(), mediaBox.getBottom(), mediaBox.getWidth(), from.getBottom() -  mediaBox.getBottom()), null));
        if (from.getLeft() > mediaBox.getLeft())
            cleanUpLocations.add(new PdfCleanUpLocation(1, new Rectangle(mediaBox.getLeft(), mediaBox.getBottom(), from.getLeft() - mediaBox.getLeft(), mediaBox.getHeight()), null));
        if (from.getRight() < mediaBox.getRight())
            cleanUpLocations.add(new PdfCleanUpLocation(1, new Rectangle(from.getRight(), mediaBox.getBottom(), mediaBox.getRight() - from.getRight(), mediaBox.getHeight()), null));

        cleaner = new PdfCleanUpTool(pdfSectionDocument, cleanUpLocations);
        cleaner.cleanUp();
    }

    try (   PdfDocument pdfSectionDocument = new PdfDocument(new PdfReader(new ByteArrayInputStream(interimSection.toByteArray())));
            PdfDocument pdfMainDocument = new PdfDocument(new PdfReader(new ByteArrayInputStream(interimMain.toByteArray())), new PdfWriter(targetFile)) )
    {
        float scale = Math.min(to.getHeight() / from.getHeight(), to.getWidth() / from.getWidth());
        pdfSectionDocument.getPage(1).setMediaBox(from);
        PdfFormXObject pageXObject = pdfSectionDocument.getFirstPage().copyAsFormXObject(pdfMainDocument);
        PdfPage pdfPage = pdfMainDocument.getPage(page);
        PdfCanvas pdfCanvas = new PdfCanvas(pdfPage);
        pdfCanvas.addXObject(pageXObject, scale, 0, 0, scale, (to.getLeft() - from.getLeft() * scale), (to.getBottom() - from.getBottom() * scale));
    }
}

( MoveSectionCleanly.java에서 )

주의 : pdfSweep의 특성으로 인해 원본 영역의 테두리에있는 텍스트는 원본과 복사본 모두에서 제거됩니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관