We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copy pages from one Tiff to a new Tiff

I’m trying to create a tiff made from a subset of pages of another. Getting page 1 is getting the entire document. Is there an easier way to do this? I’m posting my code in hopes you see the issue.


public bool GetTiffPages(string origin, string destination, string pages)
{
bool success = false;
TiffImage destImage = null;
TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.TiffCcittFax4) { RowsPerStrip = 1 };
//Create an instance of TiffImage and load the copied destination image
using (var image = (TiffImage)Aspose.Imaging.Image.Load(origin))
{
//Create an instance of int to keep track of frames in TiffImage
int pageCount = image.Frames.Length;
var pageList = ParsePageNumbers(pages, 0, pageCount);
foreach (var pg in pageList)
{
//if any of the page numbers are greater than the available frames
if (pg >= pageCount)
{
success = false;
break;
}
          <span style="color:#2b91af;">TiffFrame</span> tiffFrame = image.Frames[pg - 1];
          <span style="color:blue;">if</span> (destImage == <span style="color:blue;">null</span>)
          {
              destImage = <span style="color:blue;">new</span> <span style="color:#2b91af;">TiffImage</span>(<span style="color:#2b91af;">TiffFrame</span>.CopyFrame(tiffFrame));
          }
          <span style="color:blue;">else</span>
          {
              destImage.AddFrame(<span style="color:#2b91af;">TiffFrame</span>.CopyFrame(tiffFrame));
          }
          success = <span style="color:blue;">true</span>;
      }
      <span style="color:blue;">if</span> (success)
      {
          destImage.Save(destination, tiffOptions);
          destImage.Dispose();
      }
  }
  <span style="color:blue;">return</span> success;

}

Hi Tom,


I have observed your comments. Can you please share source file, generated file and desired pages string that you are in interested in processing so that we may test your sample code to reproduce the issue. Can you please share definition of ParsePageNumbers method as well. Please share the requested information so that we may try to reproduce issue shared by you.

Best Regards,
I found the solution. You have to save after every frame add. Below is the corrected code.

The image used was irrelevant. This was happening for any multi-page tiff.

ParsePageNumbers is posted below. It's a way to take a printer type of formatted number string and create an innumerable integer list. In full disclosure I found the ParsePageNumbers on the internet, don't recall where.

public void GetTiffPages(string origin, string pages)
 {
     TiffImage destImage = null;
     TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.TiffCcittFax4)
     {
         ResolutionSettings = new ResolutionSetting(200, 200),
         Photometric = TiffPhotometrics.MinIsWhite
     };
     //Create an instance of TiffImage and load the copied destination image
     using (var image = (TiffImage)Aspose.Imaging.Image.Load(origin))
     {
         //Create an instance of int to keep track of frames in TiffImage
         int pageCount = image.Frames.Length;
         var pageList = ParsePageNumbers(pages, 0, pageCount);
         foreach (var pg in pageList)
         {
             //if any of the page numbers are greater than the available frames
             if (pg >= pageCount)
             {
                 break;
             }
         </span><span style="background-color: white; color: rgb(43, 145, 175);">TiffFrame</span><span style="background-color: white;"> tiffFrame = image.Frames[pg - 1];
         </span><span style="background-color: white; color: blue;">if</span><span style="background-color: white;"> (destImage == </span><span style="background-color: white; color: blue;">null</span><span style="background-color: white;">)
         {
             destImage = </span><span style="background-color: white; color: blue;">new</span><span style="background-color: white;"> </span><span style="background-color: white; color: rgb(43, 145, 175);">TiffImage</span><span style="background-color: white;">(</span><span style="background-color: white; color: rgb(43, 145, 175);">TiffFrame</span><span style="background-color: white;">.CopyFrame(tiffFrame));
         }
         </span><span style="background-color: white; color: blue;">else</span><span style="background-color: white;">
         {
             destImage.AddFrame(</span><span style="background-color: white; color: rgb(43, 145, 175);">TiffFrame</span><span style="background-color: white;">.CopyFrame(tiffFrame));
         }
         </span><b style="background-color: rgb(255, 255, 0);">destImage.Save(origin + <span style="color: rgb(163, 21, 21);">".pages"</span>, tiffOptions);</b><span style="background-color: white;">
     }
 }</span></pre><pre style="font-family: Consolas; font-size: 13px; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><pre style="font-family: Consolas; background: white;"><span style="color:green;">     //moves the new selected frames to the original file</span>

if (File.Exists(origin + “.pages”))
{
File.Delete(origin);
File.Move(origin + “.pages”, origin);
}

}


private IEnumerable<Int32> ParsePageNumbers(String s, Int32 firstPage, Int32 lastPage)
{
String[] parts = s.Split(’ ', ‘;’, ‘,’);
Regex reRange = new Regex(@"^\s*((?<from>\d+)|(?<from>\d+)(?<sep>(-|..))(?<to>\d+)|(?<sep>(-|..))(?<to>\d+)|(?<from>\d+)(?<sep>(-|..)))\s*$");
foreach (String part in parts)
{
Match maRange = reRange.Match(part);
if (maRange.Success)
{
Group gFrom = maRange.Groups[“from”];
Group gTo = maRange.Groups[“to”];
Group gSep = maRange.Groups[“sep”];
         <span style="color:blue;">if</span> (gSep.Success)
         {
             <span style="color:#2b91af;">Int32</span> from = firstPage;
             <span style="color:#2b91af;">Int32</span> to = lastPage;
             <span style="color:blue;">if</span> (gFrom.Success)
                 from = <span style="color:#2b91af;">Int32</span>.Parse(gFrom.Value);
             <span style="color:blue;">if</span> (gTo.Success)
                 to = <span style="color:#2b91af;">Int32</span>.Parse(gTo.Value);
             <span style="color:blue;">for</span> (<span style="color:#2b91af;">Int32</span> page = from; page <= to; page++)
                 <span style="color:blue;">yield</span> <span style="color:blue;">return</span> page;
         }
         <span style="color:blue;">else</span>
             <span style="color:blue;">yield</span> <span style="color:blue;">return</span> <span style="color:#2b91af;">Int32</span>.Parse(gFrom.Value);
     }
 }

}





Hi Tom,

It’s good to know things have worked on your end. Please share, if we may help you further in this regard.

Many Thanks,