説明 : iFrame を介して動的に PDF を読み込む Web サイトがあります。PDF はサーバーに保存され、Web サイトのユーザーは Web サイトで PDF を表示できます。
問題 : 動的に作成されてサーバーに保存された PDF を印刷する印刷ボタンを Web サイトに導入します。
解決策:この問題の正確な解決策は見つかりませんでしたが、問題を解決した方法は次のとおりです-
- 要件に従って「印刷」を作成し、PDF のみを含む別のページにリダイレクトします。
- 以前の PDF をコピーし、JS で新しい PDF を作成します - this.print() を開くと、印刷ダイアログがユーザーに直接ポップアップします。
新しいページで -
if ("Location of PDF " != null)
{
sPdf = "Location of PDF ";
PdfReader pReader = new PdfReader(sPdf);
Document document = new Document
(pReader.GetPageSizeWithRotation(ApplicationConstants.INDEX_ONE));
int n = pReader.NumberOfPages;
FileStream fs = new FileStream
("New PDF location",
FileMode.Create, FileAccess.Write);
PdfCopy copy = new PdfCopy(document, fs);
// Write to pdf
document.Open();
for (int i = ApplicationConstants.INDEX_ONE; i <= n; i++)
{
PdfImportedPage page = copy.GetImportedPage(pReader, i);
copy.AddPage(page);
}
copy.AddJavaScript("this.print(true);", true);
document.Close();
pReader.Close();
inStr = File.OpenRead("New PDF location");
while ((bytecnt = inStr.Read
(buffer, ApplicationConstants.INDEX_ZERO, buffer.Length))
> ApplicationConstants.INDEX_ZERO)
{
if (Context.Response.IsClientConnected)
{
Context.Response.ContentType = "application/PDF";
Context.Response.OutputStream.Write(buffer,
ApplicationConstants.INDEX_ZERO, buffer.Length);
Context.Response.Flush();
}
}
}
itextsharp を使用して JS スクリプトを新しい PDF に挿入していることに注意してください。これが他の誰かに役立つことを願っています。itextsharp やその他の dll を使用せずに別の解決策を見つけようとしていますが、これは今のところ行う必要があります。