2013-06-19 3 views
3

명령 줄에서 다중 페이지 PDF의 다른 페이지에 삽입 다른 텍스트, 예를 들어내가 여러 이름을 가진 텍스트 파일을

John Doe 
Jane Doe 
Mike Miller 
... 

및 텍스트 파일의 이름과 같은 많은 페이지를 PDF 파일 .

어떻게하면 첫 번째 페이지에 이름을 붙여 넣을 수 있고 두 번째 페이지에 두 번째 이름을 붙여 넣을 수 있습니까? Linux 서버의 명령 줄에서해야합니다.

답변

1

Ghostscript를 사용하여 PostScript로 프로그래밍하면됩니다. PDF 파일의 페이지 수나 텍스트 파일의 줄 수를 먼저 확인해야합니다. 동일한 지 확인하는 것이 좋습니다.

Ghostscript pdfwrite 장치를 사용하여 PDF 파일에서 페이지 설명을 실행 한 다음 텍스트 파일에서 텍스트를 읽습니다. 내용에 현재 위치를 올 바르고 적절한 글꼴과 크기를 선택하고 텍스트를 표시하십시오. 그런 다음 showpage를 실행하여 페이지를 렌더링하십시오.

모든 페이지가 포함 된 큰 PDF 파일 하나 또는 페이지 당 하나의 PDF 파일을 가져올 수 있습니다.

포스트 스크립트 프로그래밍에 익숙하지 않은 사용자에게는이 기능이 적용되지 않습니다.

+0

감사합니다.하지만이 예제 스크립트를 제공해 주시겠습니까? 이미 x 페이지가있는 pdf 파일과 x 줄이있는 텍스트 파일이 있습니다. 그래서 그냥 텍스트의 n 번째 줄을 위치 (a, b)의 pdf 파일의 n 번째 페이지에 놓아야합니다. – user2502041

+0

@ user2502041 : 적어도 지금 당신이 할 수있는 것은 켄 수사가 스크립트로 대답 한 것을 받아 들일 수 있으며, 두 답변을 모두 업로 드할 수 있습니다 :) –

1

이 프로그램은 저에게 효과적이지만 훌륭한 테스트를 수행하지 않았습니다. 2 개의 빈 페이지가있는 PDF 파일과 2 줄의 텍스트가있는 텍스트 파일을 사용했습니다. 올바른 위치로 이동하려면 InsertText 프로 시저를 수정해야합니다.

gs -sDEVICE=pdfwrite \ 
    -sOutputFile=out.pdf \ 
    -sPDF_File=test.pdf \ 
    -sText_File=test.txt \ 
    textinsert.ps 

이 완전히 고스트 특정 대상이므로주의 해주십시오 절대적으로 작동하지 않습니다 : 당신은 또한

가 여기에 사용 예입니다 (프로그램에 findfont 검색) 글꼴 선택 및 크기를 수정해야합니다 다른 PostScript/PDF 소비자.

%!PS 
%% Copyright (C) Ken Sharp and Artifex Software Inc All rights reserved. 
%% Permission granted to use, copy, modify and redistribute provided this 
%% copyright remains intact. No warranty express or implied. 
%% 
%% Process a PDF file and a text file where one line of text from the text 
%% file is drawn onto a page of the PDF file. 
%% Redefine showpage so that we can draw the PDF page 
%% without emitting it 
%%s 
/TextInsertDict 20 dict dup 3 1 roll def begin 

/InsertText { 
    %% Read a line of text from the text file 
    TFile 256 string readline pop 

    %% Move to the desired location and draw the text 
    100 100 moveto show 
} bind def 

/Textpdfshowpage{ 
    save /PDFSave exch store 
    /PDFdictstackcount countdictstack store 
    /PDFexecstackcount count 2 sub store 
    (before exec) VMDEBUG 

    % set up color space substitution (this must be inside the page save) 
    pdfshowpage_setcspacesub 

    .writepdfmarks { 

     % Copy the boxes. 
    { /CropBox /BleedBox /TrimBox /ArtBox } { 
     2 copy pget { 
     % .pdfshowpage_Install transforms the user space do same here with the boxes 
     oforce_elems 
     2 { Page pdf_cached_PDF2PS_matrix transform 4 2 roll } repeat 
     normrect_elems 4 index 5 1 roll fix_empty_rect_elems 4 array astore 
     mark 3 1 roll /PAGE pdfmark 
     } { 
     pop 
     } ifelse 
    } forall 

     % Copy annotations and links. 
    dup /Annots knownoget { 
     0 1 2 index length 1 sub 
     { 1 index exch oget 
     dup type /dicttype eq { 
      dup /Subtype oget annottypes exch .knownget { exec } { pop } ifelse 
     } { 
      pop 
     } ifelse 
     } 
     for pop 
    } if 

    } if  % end .writepdfmarks 

     % Display the actual page contents. 
    8 dict begin 
    /BXlevel 0 def 
    /BMClevel 0 def 
    /OFFlevels 0 dict def 
    /BGDefault currentblackgeneration def 
    /UCRDefault currentundercolorremoval def 
     %****** DOESN'T HANDLE COLOR TRANSFER YET ****** 
    /TRDefault currenttransfer def 
    matrix currentmatrix 2 dict 
    2 index /CropBox pget { 
    oforce_elems normrect_elems boxrect 
    4 array astore 1 index /ClipRect 3 -1 roll put 
    } if 
    dictbeginpage setmatrix 
    /DefaultQstate qstate store 

    count 1 sub /pdfemptycount exch store 
     % If the page uses any transparency features, show it within 
     % a transparency group. 
    dup pageusestransparency dup /PDFusingtransparency exch def { 
    % Show the page within a PDF 1.4 device filter. 
    0 .pushpdf14devicefilter { 
     /DefaultQstate qstate store  % device has changed -- reset DefaultQstate 
     % If the page has a Group, enclose contents in transparency group. 
     % (Adobe Tech Note 5407, sec 9.2) 
     dup /Group knownoget { 
     1 index /CropBox pget { 
      /CropBox exch 
     } { 
      1 index get_media_box pop /MediaBox exch 
     } ifelse 
     oforce_elems normrect_elems fix_empty_rect_elems 4 array astore .beginformgroup 
     showpagecontents 
     .endtransparencygroup 
     } { 
     showpagecontents 
     } ifelse 
    } stopped { 
     % todo: discard 
     .poppdf14devicefilter 
     /DefaultQstate qstate store % device has changed -- reset DefaultQstate 
     stop 
    } if .poppdf14devicefilter 
    /DefaultQstate qstate store % device has changed -- reset DefaultQstate 
    } { 
    showpagecontents 
    } ifelse 
    .free_page_resources 

    InsertText 

    % todo: mixing drawing ops outside the device filter could cause 
    % problems, for example with the pnga device. 
    endpage 
    end   % scratch dict 
    % Some PDF files don't have matching q/Q (gsave/grestore) so we need 
    % to clean up any left over dicts from the dictstack 

    PDFdictstackcount //false 
    { countdictstack 2 index le { exit } if 
    currentdict /n known not or 
    end 
    } loop { 
    StreamRunAborted not { 
     ( **** Warning: File has unbalanced q/Q operators \(too many q's\)\n) 
     pdfformaterror 
    } if 
    } if 
    pop 
    count PDFexecstackcount sub { pop } repeat 
    (after exec) VMDEBUG 
    Repaired  % pass Repaired state around the restore 
    PDFSave restore 
    /Repaired exch def 
} bind def 

%% Check both our arguments are defined leaves true on the 
%% stack if so, false otherwise 
%% 
{PDF_File} stopped 
{ 
    (No PDF_File defined\n) print 
    false 
} 
{ 
    {Text_File} stopped 
    { 
    pop (No Text-File defined\n) print 
    false 
    } 
    { 
    pop pop true 
    }ifelse 
}ifelse 

{ 

    %% First find the number of lines of text in the text file 
    %% 
    /TextLineCount 0 def 
    Text_File (r) file dup 
    { 
    dup 256 string readline 
    { 
     pop /TextLineCount TextLineCount 1 add def 
    } 
    { 
     pop exit 
    } 
    ifelse 
    } loop 
    closefile 

    %% First find the number of pages in the PDF file 
    %% 
    PDF_File (r) file 
    runpdfbegin pdfpagecount TextLineCount eq { 
    runpdfend true 
    } 
    { 
    runpdfend false 
    } ifelse 
    exch 
    closefile 

    %% If the number of pages is the same as the number of lines 
    %% the we process the files, otherwise warn and exiot 
    %% 
    { 
    %% Select font and size 
    %% 
    /Times-Roman findfont 20 scalefont setfont 

    %% Open the text file agaiin 
    %% 
    /TFile Text_File (r) file def 

    %% Open the PDF file and begin PDF processing 
    PDF_File (r) file 
    runpdfbegin 

    %% For each page.... 
    %% 
    1 1 TextLineCount { 
     %% draw the content of this page 
     %% 
     pdfgetpage dup /Page exch store 
     pdfshowpage_init 
     pdfshowpage_setpage 
     Textpdfshowpage 
    } for 

    %% Terminate PDF processing 
    %% 
    runpdfend 

    %% Close the text file 
    %% 
    TFile closefile 
    } 
    { 
    (Warning, Number of pages not equal to the number of text lines, aborting!\n) print flush 
    }ifelse 
} 
{ 
    (Incorrect usage\n) print 
    (Usage: \n) print 
    (gs -sDEVICE=pdfwrite -o <outputfile> -sPDF-File=<PDF file> -sText_File=<Text file> textinsert.ps\n) print 
    (NB all switches are case-sensitive\n) print 
} ifelse 
end 
관련 문제