ページごとの向きが異なるPDFを印刷する際に向き情報が失われる

Curtis Chau
Curtis Chau
更新日: 2026年8月21日

IronPrint 2026.1.5は縦向きと横向きのページが混在するPDFを印刷する際、ページごとの向きを保持しません。 すべてのページは元の向きに関係なく横向きでプリンターに送信されます。

このバージョンには修正済みビルドは利用できません。 回避策として、文書を連続する同じ向きのページのバッチに分割し、各バッチを個別の印刷ジョブとしてプリンターに送信します。

この問題はWindows 10およびWindows Server 2022で動作する2026.1.5に影響し、解決済みのバージョンはありません。

解決策

PDFを単一のジョブではなく、向きが一致するバッチで印刷してください。

1. 各ページの向きを検出する

PDFを開き、各ページの幅と高さを比較します。 幅が高さより大きい場合は横向きです。 それ以外の場合は縦向きです。

2. 連続した同じ向きのページをグループ化する

ページを順番に歩き、向きが変わるたびに新しいバッチを開始し、各バッチを単一の向きを持つ連続したページの範囲にします。

3. 各バッチを別々のジョブとして印刷する

各範囲を独自に送信することで、プリンターはその範囲に正しい向きを適用でき、文書全体が横向きに強制されることはありません。

4. 複数のコピーを手動で処理する

文書が複数の印刷ジョブに分割されるため、各コピーごとに一度ループし、各回でフルバッチシーケンスを印刷して、すべてのコピーでページ順序が保持されるようにします。

完全な実装:

using IronPrint;
using IronPdf;
await PrintDocumentByOrientationBatchesAsync(
    documentPath: documentPath,
    printerName: printerName,
    numberOfCopies: effectiveCopies);
async Task PrintDocumentByOrientationBatchesAsync(
    string documentPath,
    string printerName,
    int numberOfCopies)
{
    var tempFolder = Path.Combine(
        Path.GetTempPath(),
        "ironprint-orientation-workaround",
        Guid.NewGuid().ToString("N"));
    Directory.CreateDirectory(tempFolder);
    try
    {
        using var pdf = PdfDocument.FromFile(documentPath);
        var batches = GetConsecutiveOrientationBatches(pdf);
        // Since the document is printed in multiple jobs, copies are handled manually
        // to preserve the full document order per copy.
        for (var copy = 1; copy <= numberOfCopies; copy++)
        {
            foreach (var batch in batches)
            {
                var batchPath = Path.Combine(
                    tempFolder,
                    $"copy_{copy}_pages_{batch.StartPageNumber}_{batch.EndPageNumber}_{batch.Orientation}.pdf");
                using var batchPdf = pdf.CopyPages(batch.StartIndex, batch.EndIndex);
                batchPdf.SaveAs(batchPath);
                var printSettings = new PrintSettings
                {
                    PrinterName = printerName,
                    NumberOfCopies = 1,
                    PaperOrientation = batch.Orientation == DetectedPageOrientation.Landscape
                        ? PaperOrientation.Landscape
                        : PaperOrientation.Portrait,
                    PaperSize = PaperSize.PrinterDefault
                };
                await Printer.PrintAsync(batchPath, printSettings);
                // Optional delay to help ensure print jobs are queued in order.
                await Task.Delay(500);
            }
        }
    }
    finally
    {
        try
        {
            Directory.Delete(tempFolder, recursive: true);
        }
        catch
        {
            // Ignore cleanup errors in case the print spooler is still accessing the files.
        }
    }
}
List<PageOrientationBatch> GetConsecutiveOrientationBatches(PdfDocument pdf)
{
    var batches = new List<PageOrientationBatch>();
    if (pdf.PageCount == 0)
    {
        return batches;
    }
    var currentOrientation = GetPageOrientation(pdf.Pages[0].Width, pdf.Pages[0].Height);
    var batchStartIndex = 0;
    for (var pageIndex = 1; pageIndex < pdf.PageCount; pageIndex++)
    {
        var page = pdf.Pages[pageIndex];
        var pageOrientation = GetPageOrientation(page.Width, page.Height);
        if (pageOrientation != currentOrientation)
        {
            batches.Add(new PageOrientationBatch(
                StartIndex: batchStartIndex,
                EndIndex: pageIndex - 1,
                Orientation: currentOrientation));
            batchStartIndex = pageIndex;
            currentOrientation = pageOrientation;
        }
    }
    batches.Add(new PageOrientationBatch(
        StartIndex: batchStartIndex,
        EndIndex: pdf.PageCount - 1,
        Orientation: currentOrientation));
    return batches;
}
DetectedPageOrientation GetPageOrientation(double width, double height)
{
    return width > height
        ? DetectedPageOrientation.Landscape
        : DetectedPageOrientation.Portrait;
}
record PageOrientationBatch(
    int StartIndex,
    int EndIndex,
    DetectedPageOrientation Orientation)
{
    public int StartPageNumber => StartIndex + 1;
    public int EndPageNumber => EndIndex + 1;
}
enum DetectedPageOrientation
{
    Portrait,
    Landscape
}

注意: この回避策では、各向きの範囲を個別の印刷ジョブとして送信します。 物理プリンターでは、ジョブを連続してキューに入れてください。 Microsoft Print to PDFまたは同様のバーチャルプリンターを使用する場合、各バッチが独自の出力ファイルを要求することがあります。

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

もっと読む

準備はできましたか?

NuGetダウンロード数 52,427バージョン:2026.11ちょうどリリースされました